Skip to content

Commit

Permalink
feat: batch store key records
Browse files Browse the repository at this point in the history
  • Loading branch information
andy23512 committed May 22, 2024
1 parent 6eaa8fc commit 8b6d954
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/app/services/key-record.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { KeyRecordService } from './key-record.service';

describe('KeyRecordService', () => {
let service: KeyRecordService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(KeyRecordService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
24 changes: 24 additions & 0 deletions src/app/services/key-record.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Injectable } from '@angular/core';
import { Subject, bufferTime } from 'rxjs';
import { db } from '../db';
import { KeyRecord } from '../models/key-record.models';

@Injectable({
providedIn: 'root',
})
export class KeyRecordService {
private queueSubject = new Subject<KeyRecord>();

constructor() {
this.queueSubject
.asObservable()
.pipe(bufferTime(2000))
.subscribe((keyRecords) => {
db.keyRecords.bulkAdd(keyRecords);
});
}

public pushIntoQueue(keyRecord: KeyRecord) {
this.queueSubject.next(keyRecord);
}
}
10 changes: 5 additions & 5 deletions src/app/stores/lesson.store.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { withDevtools } from '@angular-architects/ngrx-toolkit';
import { computed } from '@angular/core';
import { computed, inject } from '@angular/core';
import {
patchState,
signalStore,
withComputed,
withMethods,
withState,
} from '@ngrx/signals';
import { db } from '../db';
import { Lesson } from '../models/topic.models';
import { KeyRecordService } from '../services/key-record.service';
import { pickRandomItem, pickRandomItemNTimes } from '../utils/random.utils';

const QUEUE_SIZE = 20;
Expand Down Expand Up @@ -40,7 +40,7 @@ const initialState: LessonState = {
export const LessonStore = signalStore(
withDevtools('lesson'),
withState(initialState),
withMethods((store) => ({
withMethods((store, keyRecordService = inject(KeyRecordService)) => ({
setLesson(lesson: Lesson) {
patchState(store, () => ({
topicId: lesson.topic.id,
Expand Down Expand Up @@ -72,7 +72,7 @@ export const LessonStore = signalStore(
inputKey: component,
};
if (component !== state.queue[0]) {
db.keyRecords.add({
keyRecordService.pushIntoQueue({
...commonKeyRecord,
isCorrect: false,
intervalToPreviousCorrectKey: keyInterval,
Expand All @@ -81,7 +81,7 @@ export const LessonStore = signalStore(
});
return { error: true, combo: 0 };
}
db.keyRecords.add({
keyRecordService.pushIntoQueue({
...commonKeyRecord,
isCorrect: true,
intervalToPreviousCorrectKey: keyInterval,
Expand Down

0 comments on commit 8b6d954

Please sign in to comment.