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
|
import { computeNextSchedule } from "../../shared/fsrs";
import { db, type LocalCard, type RatingType } from "../db";
import {
localCardRepository,
localDeckRepository,
localReviewLogRepository,
} from "../db/repositories";
import { syncQueue } from "./queue";
export interface SubmitReviewResult {
/** The card after the review is applied. */
card: LocalCard;
/** Snapshot of the card before the review — used by undo. */
prevCard: LocalCard;
/** The newly created review log id — used by undo. */
reviewLogId: string;
}
/**
* Submit a review locally: update card scheduling and create a review log
* in IndexedDB. The sync engine will pick up the changes via _synced=false.
*/
export async function submitReviewLocal(params: {
cardId: string;
rating: RatingType;
durationMs: number;
now?: Date;
}): Promise<SubmitReviewResult> {
const { cardId, rating, durationMs } = params;
const now = params.now ?? new Date();
const card = await localCardRepository.findById(cardId);
if (!card) {
throw new Error(`Card not found in local database: ${cardId}`);
}
const deck = await localDeckRepository.findById(card.deckId);
if (!deck) {
throw new Error(`Deck not found in local database: ${card.deckId}`);
}
const prevCard = card;
const previousState = card.state;
const next = computeNextSchedule(card, rating, now);
const updatedCard = await localCardRepository.updateScheduling(cardId, {
state: next.state as LocalCard["state"],
due: next.due,
stability: next.stability,
difficulty: next.difficulty,
elapsedDays: next.elapsedDays,
scheduledDays: next.scheduledDays,
reps: next.reps,
lapses: next.lapses,
lastReview: next.lastReview,
});
if (!updatedCard) {
throw new Error(`Failed to update card: ${cardId}`);
}
const reviewLog = await localReviewLogRepository.create({
cardId,
userId: deck.userId,
rating,
state: previousState,
scheduledDays: next.scheduledDays,
elapsedDays: next.reviewElapsedDays,
reviewedAt: now,
durationMs,
});
await syncQueue.notifyChanged();
return { card: updatedCard, prevCard, reviewLogId: reviewLog.id };
}
/**
* Undo a recent review: restore the previous card state and remove the
* just-created review log. Best-effort — if a sync has already pushed the
* review, the server still has it.
*/
export async function undoReviewLocal(params: {
prevCard: LocalCard;
reviewLogId: string;
}): Promise<void> {
await db.cards.put({ ...params.prevCard });
await localReviewLogRepository.delete(params.reviewLogId);
await syncQueue.notifyChanged();
}
/**
* Server-shaped study card. Includes all FSRS fields needed to reconstruct
* a LocalCard so we can submit reviews offline.
*/
export interface ServerStudyCard {
id: string;
deckId: string;
noteId: string;
isReversed: boolean;
front: string;
back: string;
state: number;
due: string;
stability: number;
difficulty: number;
elapsedDays: number;
scheduledDays: number;
reps: number;
lapses: number;
lastReview: string | null;
createdAt: string;
updatedAt: string;
deletedAt: string | null;
syncVersion: number;
}
/**
* Cache study cards into IndexedDB so the scheduler can submit reviews
* even when the network drops mid-session. Only cards are cached here —
* note types / fields / values come through the regular sync pull.
*/
export async function cacheStudyCards(cards: ServerStudyCard[]): Promise<void> {
for (const c of cards) {
const local: LocalCard = {
id: c.id,
deckId: c.deckId,
noteId: c.noteId,
isReversed: c.isReversed,
front: c.front,
back: c.back,
state: c.state as LocalCard["state"],
due: new Date(c.due),
stability: c.stability,
difficulty: c.difficulty,
elapsedDays: c.elapsedDays,
scheduledDays: c.scheduledDays,
reps: c.reps,
lapses: c.lapses,
lastReview: c.lastReview ? new Date(c.lastReview) : null,
createdAt: new Date(c.createdAt),
updatedAt: new Date(c.updatedAt),
deletedAt: c.deletedAt ? new Date(c.deletedAt) : null,
syncVersion: c.syncVersion,
_synced: true,
};
// Don't clobber pending local edits (e.g., a review that hasn't
// been pushed yet). If the local copy has unsynced changes, skip.
const existing = await localCardRepository.findById(c.id);
if (existing && !existing._synced) continue;
await localCardRepository.upsertFromServer(local);
}
}
|