Repository navigation
Expand file tree
/
Copy pathanswerStore.ts
More file actions
261 lines (251 loc) · 9.64 KB
/
Copy pathanswerStore.ts
File metadata and controls
261 lines (251 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// SPDX-FileCopyrightText: 2026 Anton Wiklund and Solid Memo contributors
//
// SPDX-License-Identifier: MIT
import type { Deck, StudyDirection } from "@solid-memo/domain/deck";
import type { StudyPreferences } from "@solid-memo/domain/preferences";
import type {
DueState,
FsrsState,
ReviewQuality,
ReviewState,
ReviewStates,
} from "@solid-memo/domain/review";
import { studyDayOf } from "@solid-memo/domain/scheduling";
import { effectiveStates } from "@solid-memo/domain/fsrs";
import { gradePrompt, type Scheduling } from "@solid-memo/domain/grading";
import {
answerIdOf,
recordedResponseTimes,
utcOffsetMinutesAt,
type Answer,
type AnswerMode,
type ResponseTimes,
} from "@solid-memo/domain/answer";
import type { ReviewStateRepository, AnswerLog } from "./ports";
import type { GradeBasis, RecordedReview } from "./study";
import type { PreferenceStore } from "./preferenceStore";
/** What sets each prompt's due day, by the scheduler the preferences name (effectiveStates). */
export function dueStatesOf(
states: ReviewStates,
prefs: Pick<StudyPreferences, "scheduler" | "dayBoundaryHour">,
) {
return effectiveStates(states.sm2, states.fsrs, prefs.scheduler, prefs.dayBoundaryHour);
}
/** What sets one prompt's due day, of its SM-2 state and its FSRS-6 state (null or absent: none). */
export function promptDueOf(
sm2: ReviewState,
fsrs: FsrsState | null | undefined,
prefs: Pick<StudyPreferences, "scheduler" | "dayBoundaryHour">,
): DueState {
return dueStatesOf({ sm2: [sm2], fsrs: fsrs == null ? [] : [fsrs] }, prefs)[0]!;
}
/**
* What the instance grades and schedules with: the scheduler its
* preferences name, the retention FSRS-6 aims at and the day boundary.
*/
function schedulingOf(prefs: StudyPreferences): Scheduling {
return {
scheduler: prefs.scheduler,
desiredRetention: prefs.desiredRetention,
dayBoundaryHour: prefs.dayBoundaryHour,
};
}
/**
* The answer log as the use cases share it: each answer graded queued
* for the log, added after its review is saved, and each month read
* kept with its version, read again only once it changed.
*/
export function createAnswerStore({
answerLog,
reviewStateRepository,
preferenceStore,
newId,
}: {
answerLog: AnswerLog;
reviewStateRepository: ReviewStateRepository;
preferenceStore: Pick<PreferenceStore, "get">;
newId: () => string;
}) {
/**
* Answers given but not yet in the log, oldest first. An answer is
* added after its review is saved, without holding up study; one that
* fails to be added waits here, and the ones after it with it, until
* the next answer, the end of the session or the statistics try again.
*/
const unlogged: { instanceUrl: string; answer: Answer }[] = [];
let logging: Promise<void> = Promise.resolve();
function logAnswers(): Promise<void> {
logging = logging.then(async () => {
while (unlogged.length > 0) {
const [next] = unlogged;
try {
await answerLog.append(next!.instanceUrl, next!.answer);
} catch {
return;
}
logWrites += 1;
unlogged.shift();
}
});
return logging;
}
/** Each instance's answer log as last read, by month: its answers, and the version they were read at. */
const answerMonths = new Map<string, Map<string, { version: string; answers: Answer[] }>>();
/**
* The reads of a month under way, by instance, month and the writes to
* the logs made before they began (logWrites): a read begun before a
* write may miss it, so only one begun since is shared.
*/
const monthReads = new Map<string, Promise<Answer[]>>();
let logWrites = 0;
/**
* The answers of the log's months that `wanted` keeps, of `listed` (every
* month the log has): each month read only when it changed since last
* read, and kept with its version. What is kept is added to, never
* replaced, so a read of some months and one of all do not undo each
* other; a month no longer listed is dropped. A month already being
* read since the last write to the logs is not read twice: the
* statistics and the cards' progress ask at once.
*/
async function readLogMonths(
instanceUrl: string,
listed: readonly string[],
wanted: (month: string) => boolean,
): Promise<Answer[]> {
const known =
answerMonths.get(instanceUrl) ?? new Map<string, { version: string; answers: Answer[] }>();
answerMonths.set(instanceUrl, known);
const still = new Set(listed);
for (const month of known.keys()) if (!still.has(month)) known.delete(month);
const answers = await Promise.all(
listed.filter(wanted).map((month) => {
const key = `${instanceUrl}\n${month}\n${logWrites}`;
const under = monthReads.get(key);
if (under !== undefined) return under;
const read = (async () => {
const kept = known.get(month);
const since = await answerLog.readMonthSince(instanceUrl, month, kept?.version);
if (since.unchanged) return kept!.answers;
if (since.version === null) known.delete(month);
else known.set(month, { version: since.version, answers: since.value });
return since.value;
})().finally(() => monthReads.delete(key));
monthReads.set(key, read);
return read;
}),
);
return answers.flat();
}
async function loadAnswerLog(instanceUrl: string): Promise<Answer[]> {
await logAnswers();
return readLogMonths(instanceUrl, await answerLog.months(instanceUrl), () => true);
}
/**
* Apply one grade to a prompt of the deck: transition its state (or the
* initial one) by SM-2 (gradePrompt), persist it, and queue its answer
* for the log, saying how it was given (`how`; a study session says
* nothing: recall) and how long it took (`times`), the day it was due
* and the device's offset from UTC. The grade is applied to the prompt's state as the
* reviews document holds it when written (gradeReviewState), so it
* builds on a grade made elsewhere meanwhile; the instance's preferences
* are read unless `known` brings them. Resolves to the new state, with
* what undoing the answer takes: the state the grade was applied to.
*/
async function applyGrade(
instanceUrl: string,
deck: Deck,
prompt: { cardId: string; cardUrl: string; direction: StudyDirection },
quality: ReviewQuality,
now: Date,
how: { mode?: AnswerMode; chosenDistractor?: string; times: ResponseTimes },
known?: GradeBasis,
): Promise<RecordedReview> {
const key = { cardId: prompt.cardId, direction: prompt.direction };
const prefs = known?.prefs ?? (await preferenceStore.get(instanceUrl));
const scheduling = schedulingOf(prefs);
const graded = await reviewStateRepository.gradeReviewState(deck, key, (current) =>
gradePrompt(
key,
current,
{ grade: quality, ...(how.mode === undefined ? {} : { mode: how.mode }) },
now,
scheduling,
),
);
const { sm2: state, fsrs } = graded.state;
const { sm2: current } = graded.current;
// The log's intervals and due day are those of what set the due day: under FSRS-6, its state's.
const prior = current === null ? null : promptDueOf(current, graded.current.fsrs, prefs);
const next = promptDueOf(state, fsrs, prefs);
const id = answerIdOf(state.lastReviewedAt, newId().slice(0, 8));
const studyDay = studyDayOf(now, prefs.dayBoundaryHour);
unlogged.push({
instanceUrl,
answer: {
id,
deckUrl: deck.url,
cardUrl: prompt.cardUrl,
direction: prompt.direction,
grade: quality,
answeredAt: state.lastReviewedAt,
studyDay,
...(prior === null ? {} : { priorIntervalDays: prior.intervalDays }),
nextIntervalDays: next.intervalDays,
...(how.mode === undefined ? {} : { mode: how.mode }),
...(how.chosenDistractor === undefined ? {} : { chosenDistractor: how.chosenDistractor }),
...recordedResponseTimes(how.times),
...(prior === null ? {} : { priorDue: prior.due }),
utcOffsetMinutes: utcOffsetMinutesAt(now),
},
});
void logAnswers();
return {
state,
...(fsrs === undefined ? {} : { fsrs }),
undo: {
key,
before: current,
...(fsrs === undefined ? {} : { fsrs: { before: graded.current.fsrs } }),
answer: { id, studyDay },
},
};
}
/**
* Take an answer back (UseCases.undoReview): off the answers on their
* way to the log, else out of it. Answers being added are waited for;
* any added later start only after this goes on.
*/
async function withdraw(instanceUrl: string, answer: { id: string; studyDay: string }) {
await logging;
const waiting = unlogged.findIndex(
(each) => each.instanceUrl === instanceUrl && each.answer.id === answer.id,
);
if (waiting === -1) {
await answerLog.removeAnswer(instanceUrl, answer.studyDay, answer.id);
logWrites += 1;
} else {
unlogged.splice(waiting, 1);
}
}
/** Remove a deck's answers of a study day from the log, those still on their way first. */
async function removeDay(instanceUrl: string, deckUrl: string, studyDay: string) {
await logAnswers();
await answerLog.removeDay(instanceUrl, deckUrl, studyDay);
logWrites += 1;
}
/** Add answers to the instance's log at once. */
async function appendAll(instanceUrl: string, answers: readonly Answer[]) {
await answerLog.appendAll(instanceUrl, answers);
logWrites += 1;
}
return {
flush: logAnswers,
readMonths: readLogMonths,
loadAll: loadAnswerLog,
grade: applyGrade,
withdraw,
removeDay,
appendAll,
};
}
export type AnswerStore = ReturnType<typeof createAnswerStore>;