@vue-skuilder/db 0.2.37 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{contentSource-D_E7JaxR.d.ts → contentSource-2zVmcjKA.d.ts} +6 -1
- package/dist/core/index.cjs +155 -100
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.ts +5 -5
- package/dist/core/navigators/Pipeline.mjs +8 -0
- package/dist/core/navigators/Pipeline.mjs.map +1 -1
- package/dist/core/versionStamps.mjs +1 -1
- package/dist/{dataLayerProvider-BDshhSp_.d.ts → dataLayerProvider-rGyLQ93v.d.ts} +1 -1
- package/dist/diagnostics/index.d.ts +3 -3
- package/dist/impl/common/BaseUserDB.mjs +61 -34
- package/dist/impl/common/BaseUserDB.mjs.map +1 -1
- package/dist/impl/couch/CourseSyncService.mjs +23 -1
- package/dist/impl/couch/CourseSyncService.mjs.map +1 -1
- package/dist/impl/couch/courseDB.mjs +4 -0
- package/dist/impl/couch/courseDB.mjs.map +1 -1
- package/dist/impl/couch/index.cjs +177 -101
- package/dist/impl/couch/index.cjs.map +1 -1
- package/dist/impl/couch/index.d.ts +5 -3
- package/dist/impl/couch/updateQueue.mjs +69 -46
- package/dist/impl/couch/updateQueue.mjs.map +1 -1
- package/dist/impl/static/index.cjs +151 -100
- package/dist/impl/static/index.cjs.map +1 -1
- package/dist/impl/static/index.d.ts +4 -4
- package/dist/{index-BLDpFYG3.d.ts → index-C745YB_6.d.ts} +1 -1
- package/dist/index.cjs +187 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +23 -10
- package/dist/index.mjs +2 -1
- package/dist/study/SessionController.mjs +4 -0
- package/dist/study/SessionController.mjs.map +1 -1
- package/dist/{types-DChCz22D.d.ts → types-DF0JuPbs.d.ts} +1 -1
- package/dist/{types-legacy-fT5Y1Wuy.d.ts → types-legacy-DdVjg81H.d.ts} +1 -1
- package/dist/util/packer/index.d.ts +3 -3
- package/dist/util/perfMarks.mjs +30 -0
- package/dist/util/perfMarks.mjs.map +1 -0
- package/docs/session-lifecycle-and-replan.md +71 -58
- package/package.json +3 -3
- package/src/core/interfaces/courseDB.ts +6 -0
- package/src/core/navigators/Pipeline.ts +8 -0
- package/src/impl/common/BaseUserDB.ts +74 -46
- package/src/impl/couch/CourseSyncService.ts +19 -2
- package/src/impl/couch/courseDB.ts +5 -0
- package/src/impl/couch/updateQueue.ts +86 -82
- package/src/study/SessionController.ts +4 -0
- package/src/util/index.ts +1 -0
- package/src/util/perfMarks.ts +35 -0
- package/tests/impl/updateQueue.test.ts +57 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { T as TagStub, a as Tag, S as SkuilderCourseData, Q as QualifiedCardID } from '../../types-legacy-
|
|
1
|
+
import { T as TagStub, a as Tag, S as SkuilderCourseData, Q as QualifiedCardID } from '../../types-legacy-DdVjg81H.js';
|
|
2
2
|
import { Moment } from 'moment';
|
|
3
3
|
export { h as hexEncode } from '../../userDBKeys-ne1g4Xuq.js';
|
|
4
|
-
import { o as StudyContentSource, U as UserDBInterface, G as GeneratorResult, a as CoursesDBInterface, C as CourseDBInterface, b as CourseInfo, D as DataLayerResult, c as ContentNavigationStrategyData, d as ContentNavigator, R as ReplanHints, S as StudySessionItem, f as ScheduledCard } from '../../contentSource-
|
|
5
|
-
export { v as ContentSourceID, p as StudySessionFailedItem, q as StudySessionFailedNewItem, r as StudySessionFailedReviewItem, s as StudySessionNewItem, t as StudySessionReviewItem, w as getStudySource, u as isReview } from '../../contentSource-
|
|
4
|
+
import { o as StudyContentSource, U as UserDBInterface, G as GeneratorResult, a as CoursesDBInterface, C as CourseDBInterface, b as CourseInfo, D as DataLayerResult, c as ContentNavigationStrategyData, d as ContentNavigator, R as ReplanHints, S as StudySessionItem, f as ScheduledCard } from '../../contentSource-2zVmcjKA.js';
|
|
5
|
+
export { v as ContentSourceID, p as StudySessionFailedItem, q as StudySessionFailedNewItem, r as StudySessionFailedReviewItem, s as StudySessionNewItem, t as StudySessionReviewItem, w as getStudySource, u as isReview } from '../../contentSource-2zVmcjKA.js';
|
|
6
6
|
import { A as AdminDBInterface, a as AssignedContent, S as StudentClassroomDBInterface, T as TeacherClassroomDBInterface } from '../../classroomDB-DLTtLZct.js';
|
|
7
7
|
import * as _vue_skuilder_common from '@vue-skuilder/common';
|
|
8
8
|
import { ClassroomConfig, DataShape, CourseElo, CourseConfig as CourseConfig$1 } from '@vue-skuilder/common';
|
|
@@ -224,6 +224,8 @@ declare class CourseDB implements CourseDBInterface {
|
|
|
224
224
|
* values may be stale, so read-modify-write cycles must go through
|
|
225
225
|
* the remote DB to avoid conflicts).
|
|
226
226
|
*/
|
|
227
|
+
/** Reads go to this device's replica (perf marks report it as the session's binding). */
|
|
228
|
+
get readsLocal(): boolean;
|
|
227
229
|
constructor(id: string, userLookup: () => Promise<UserDBInterface>, localDB?: PouchDB.Database);
|
|
228
230
|
getCourseID(): string;
|
|
229
231
|
getCourseInfo(): Promise<CourseInfo>;
|
|
@@ -2,10 +2,17 @@ import "../../_virtual/_rolldown/runtime.mjs";
|
|
|
2
2
|
import { logger } from "../../util/logger.mjs";
|
|
3
3
|
import { Loggable } from "../../util/Loggable.mjs";
|
|
4
4
|
//#region src/impl/couch/updateQueue.ts
|
|
5
|
+
/**
|
|
6
|
+
* Serialises updates per document. Updates queued while a write is in flight
|
|
7
|
+
* go out together in the next write; each one settles exactly once, with the
|
|
8
|
+
* outcome of the write that carried it, and is never applied again.
|
|
9
|
+
*/
|
|
5
10
|
var UpdateQueue = class extends Loggable {
|
|
6
11
|
_className = "UpdateQueue";
|
|
7
|
-
|
|
8
|
-
|
|
12
|
+
/** Updates not yet taken by a write, per doc. */
|
|
13
|
+
pending = /* @__PURE__ */ new Map();
|
|
14
|
+
/** Docs with a drain loop running. */
|
|
15
|
+
draining = /* @__PURE__ */ new Set();
|
|
9
16
|
readDB;
|
|
10
17
|
writeDB;
|
|
11
18
|
/**
|
|
@@ -21,6 +28,9 @@ var UpdateQueue = class extends Loggable {
|
|
|
21
28
|
* **Error Handling Pattern:**
|
|
22
29
|
* - This method does NOT create documents if they don't exist
|
|
23
30
|
* - Callers are responsible for handling 404 errors and creating documents
|
|
31
|
+
* - A rejected update is dropped from the queue: a caller that creates the
|
|
32
|
+
* doc after a 404 has already applied its change and must not see it
|
|
33
|
+
* re-applied by a later update to the same doc
|
|
24
34
|
* - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)
|
|
25
35
|
*
|
|
26
36
|
* @example
|
|
@@ -37,9 +47,16 @@ var UpdateQueue = class extends Loggable {
|
|
|
37
47
|
*/
|
|
38
48
|
update(id, update) {
|
|
39
49
|
logger.debug(`Update requested on doc: ${id}`);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
50
|
+
return new Promise((resolve, reject) => {
|
|
51
|
+
const queue = this.pending.get(id) ?? [];
|
|
52
|
+
queue.push({
|
|
53
|
+
update,
|
|
54
|
+
resolve,
|
|
55
|
+
reject
|
|
56
|
+
});
|
|
57
|
+
this.pending.set(id, queue);
|
|
58
|
+
if (!this.draining.has(id)) this.drain(id);
|
|
59
|
+
});
|
|
43
60
|
}
|
|
44
61
|
constructor(readDB, writeDB) {
|
|
45
62
|
super();
|
|
@@ -50,49 +67,55 @@ var UpdateQueue = class extends Loggable {
|
|
|
50
67
|
logger.debug(`db info: ${JSON.stringify(i)}`);
|
|
51
68
|
});
|
|
52
69
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
...updatedDoc,
|
|
66
|
-
...update(updatedDoc)
|
|
67
|
-
};
|
|
68
|
-
else updatedDoc = {
|
|
69
|
-
...updatedDoc,
|
|
70
|
-
...update
|
|
71
|
-
};
|
|
72
|
-
await this.writeDB.put(updatedDoc);
|
|
73
|
-
this.pendingUpdates[id].splice(0, updatesToApply.length);
|
|
74
|
-
if (this.pendingUpdates[id].length === 0) {
|
|
75
|
-
this.inprogressUpdates[id] = false;
|
|
76
|
-
delete this.inprogressUpdates[id];
|
|
77
|
-
} else return this.applyUpdates(id);
|
|
78
|
-
return updatedDoc;
|
|
79
|
-
} catch (e) {
|
|
80
|
-
if (e.name === "conflict" && i < MAX_RETRIES - 1) {
|
|
81
|
-
logger.warn(`Conflict on update for doc ${id}, retry #${i + 1}`);
|
|
82
|
-
await new Promise((res) => setTimeout(res, 50 * Math.random()));
|
|
83
|
-
} else if (e.name === "not_found" && i === 0) {
|
|
84
|
-
logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);
|
|
85
|
-
delete this.inprogressUpdates[id];
|
|
86
|
-
throw e;
|
|
87
|
-
} else {
|
|
88
|
-
delete this.inprogressUpdates[id];
|
|
89
|
-
if (this.pendingUpdates[id]) delete this.pendingUpdates[id];
|
|
90
|
-
logger.error(`Error on attemped update (retry ${i}): ${JSON.stringify(e)}`);
|
|
91
|
-
throw e;
|
|
70
|
+
/** Writes the doc's queued updates, batch by batch, until none are left. */
|
|
71
|
+
async drain(id) {
|
|
72
|
+
this.draining.add(id);
|
|
73
|
+
try {
|
|
74
|
+
let batch;
|
|
75
|
+
while ((batch = this.pending.get(id)) && batch.length > 0) {
|
|
76
|
+
this.pending.delete(id);
|
|
77
|
+
try {
|
|
78
|
+
const doc = await this.write(id, batch.map((p) => p.update));
|
|
79
|
+
batch.forEach((p) => p.resolve(doc));
|
|
80
|
+
} catch (e) {
|
|
81
|
+
batch.forEach((p) => p.reject(e));
|
|
92
82
|
}
|
|
93
83
|
}
|
|
94
|
-
|
|
95
|
-
|
|
84
|
+
} finally {
|
|
85
|
+
this.draining.delete(id);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** One read-apply-put of `updates`, retried on conflict against a fresh read. */
|
|
89
|
+
async write(id, updates) {
|
|
90
|
+
logger.debug(`Applying ${updates.length} update(s) on doc: ${id}`);
|
|
91
|
+
const MAX_RETRIES = 5;
|
|
92
|
+
for (let i = 0; i < MAX_RETRIES; i++) try {
|
|
93
|
+
let updatedDoc = { ...await this.readDB.get(id) };
|
|
94
|
+
for (const update of updates) updatedDoc = typeof update === "function" ? {
|
|
95
|
+
...updatedDoc,
|
|
96
|
+
...update(updatedDoc)
|
|
97
|
+
} : {
|
|
98
|
+
...updatedDoc,
|
|
99
|
+
...update
|
|
100
|
+
};
|
|
101
|
+
const res = await this.writeDB.put(updatedDoc);
|
|
102
|
+
return {
|
|
103
|
+
...updatedDoc,
|
|
104
|
+
_rev: res.rev
|
|
105
|
+
};
|
|
106
|
+
} catch (e) {
|
|
107
|
+
if (e.name === "conflict" && i < MAX_RETRIES - 1) {
|
|
108
|
+
logger.warn(`Conflict on update for doc ${id}, retry #${i + 1}`);
|
|
109
|
+
await new Promise((res) => setTimeout(res, 50 * Math.random()));
|
|
110
|
+
} else if (e.name === "not_found" && i === 0) {
|
|
111
|
+
logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);
|
|
112
|
+
throw e;
|
|
113
|
+
} else {
|
|
114
|
+
logger.error(`Error on attemped update (retry ${i}): ${JSON.stringify(e)}`);
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
throw new Error(`UpdateQueue failed for doc ${id} after ${MAX_RETRIES} retries.`);
|
|
96
119
|
}
|
|
97
120
|
};
|
|
98
121
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateQueue.mjs","names":[],"sources":["../../../src/impl/couch/updateQueue.ts"],"sourcesContent":["import { Loggable } from '../../util/Loggable';\nimport { logger } from '../../util/logger';\n\nexport type Update<T> = Partial<T> | ((x: T) => T);\n\nexport default class UpdateQueue extends Loggable {\n _className: string = 'UpdateQueue';\n
|
|
1
|
+
{"version":3,"file":"updateQueue.mjs","names":[],"sources":["../../../src/impl/couch/updateQueue.ts"],"sourcesContent":["import { Loggable } from '../../util/Loggable';\nimport { logger } from '../../util/logger';\n\nexport type Update<T> = Partial<T> | ((x: T) => T);\n\ntype Applied<T> = T & PouchDB.Core.GetMeta & PouchDB.Core.RevisionIdMeta;\n\n/** A queued update and the caller waiting on it. */\ninterface Pending {\n update: Update<unknown>;\n resolve: (doc: unknown) => void;\n reject: (e: unknown) => void;\n}\n\n/**\n * Serialises updates per document. Updates queued while a write is in flight\n * go out together in the next write; each one settles exactly once, with the\n * outcome of the write that carried it, and is never applied again.\n */\nexport default class UpdateQueue extends Loggable {\n _className: string = 'UpdateQueue';\n /** Updates not yet taken by a write, per doc. */\n private pending: Map<string, Pending[]> = new Map();\n /** Docs with a drain loop running. */\n private draining: Set<string> = new Set();\n\n private readDB: PouchDB.Database; // Database for read operations\n private writeDB: PouchDB.Database; // Database for write operations (local-first)\n\n /**\n * Queues an update for a document and applies it with conflict resolution.\n *\n * @param id - Document ID to update\n * @param update - Partial object or function that transforms the document\n * @returns Promise resolving to the updated document\n *\n * @throws {PouchError} with status 404 if document doesn't exist\n *\n * @remarks\n * **Error Handling Pattern:**\n * - This method does NOT create documents if they don't exist\n * - Callers are responsible for handling 404 errors and creating documents\n * - A rejected update is dropped from the queue: a caller that creates the\n * doc after a 404 has already applied its change and must not see it\n * re-applied by a later update to the same doc\n * - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)\n *\n * @example\n * ```typescript\n * try {\n * await updateQueue.update(docId, (doc) => ({ ...doc, field: newValue }));\n * } catch (e) {\n * if ((e as PouchError).status === 404) {\n * // Create the document with initial values\n * await db.put({ _id: docId, field: newValue, ...initialFields });\n * }\n * }\n * ```\n */\n public update<T extends PouchDB.Core.Document<object>>(\n id: PouchDB.Core.DocumentId,\n update: Update<T>\n ): Promise<Applied<T>> {\n logger.debug(`Update requested on doc: ${id}`);\n return new Promise<Applied<T>>((resolve, reject) => {\n const queue = this.pending.get(id) ?? [];\n queue.push({\n update: update as Update<unknown>,\n resolve: resolve as (doc: unknown) => void,\n reject,\n });\n this.pending.set(id, queue);\n if (!this.draining.has(id)) void this.drain(id);\n });\n }\n\n constructor(readDB: PouchDB.Database, writeDB?: PouchDB.Database) {\n super();\n // PouchDB.debug.enable('*');\n this.readDB = readDB;\n this.writeDB = writeDB || readDB; // Default to readDB if writeDB not provided\n logger.debug(`UpdateQ initialized...`);\n void this.readDB.info().then((i) => {\n logger.debug(`db info: ${JSON.stringify(i)}`);\n });\n }\n\n /** Writes the doc's queued updates, batch by batch, until none are left. */\n private async drain(id: string): Promise<void> {\n this.draining.add(id);\n try {\n let batch: Pending[] | undefined;\n while ((batch = this.pending.get(id)) && batch.length > 0) {\n // Take the batch: anything queued from here on goes in the next write.\n this.pending.delete(id);\n try {\n const doc = await this.write(\n id,\n batch.map((p) => p.update)\n );\n batch.forEach((p) => p.resolve(doc));\n } catch (e) {\n batch.forEach((p) => p.reject(e));\n }\n }\n } finally {\n this.draining.delete(id);\n }\n }\n\n /** One read-apply-put of `updates`, retried on conflict against a fresh read. */\n private async write(id: string, updates: Update<unknown>[]): Promise<unknown> {\n logger.debug(`Applying ${updates.length} update(s) on doc: ${id}`);\n const MAX_RETRIES = 5;\n for (let i = 0; i < MAX_RETRIES; i++) {\n try {\n const doc = await this.readDB.get<object>(id);\n\n // Applied to a fresh copy each attempt, so function updates see the\n // doc as read on this attempt.\n let updatedDoc: PouchDB.Core.Document<object> & Partial<PouchDB.Core.RevisionIdMeta> = {\n ...doc,\n };\n for (const update of updates) {\n updatedDoc =\n typeof update === 'function'\n ? { ...updatedDoc, ...(update as (x: unknown) => object)(updatedDoc) }\n : { ...updatedDoc, ...(update as object) };\n }\n\n const res = await this.writeDB.put(updatedDoc);\n return { ...updatedDoc, _rev: res.rev };\n } catch (e: any) {\n if (e.name === 'conflict' && i < MAX_RETRIES - 1) {\n logger.warn(`Conflict on update for doc ${id}, retry #${i + 1}`);\n await new Promise((res) => setTimeout(res, 50 * Math.random()));\n } else if (e.name === 'not_found' && i === 0) {\n // Document not present - throw to caller for initialization\n logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);\n throw e;\n } else {\n logger.error(`Error on attemped update (retry ${i}): ${JSON.stringify(e)}`);\n throw e;\n }\n }\n }\n // Unreachable: the last attempt returns or throws.\n throw new Error(`UpdateQueue failed for doc ${id} after ${MAX_RETRIES} retries.`);\n }\n}\n"],"mappings":";;;;;;;;;AAmBA,IAAqB,cAArB,cAAyC,SAAS;CAChD,aAAqB;;CAErB,0BAA0C,IAAI,KAAK;;CAEnD,2BAAgC,IAAI,KAAK;CAEzC;CACA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCA,OACE,IACA,QACqB;AACrB,SAAO,MAAM,4BAA4B,KAAK;AAC9C,SAAO,IAAI,SAAqB,SAAS,WAAW;GAClD,MAAM,QAAQ,KAAK,QAAQ,IAAI,GAAG,IAAI,EAAE;AACxC,SAAM,KAAK;IACD;IACC;IACT;IACD,CAAC;AACF,QAAK,QAAQ,IAAI,IAAI,MAAM;AAC3B,OAAI,CAAC,KAAK,SAAS,IAAI,GAAG,CAAO,MAAK,MAAM,GAAG;IAC/C;;CAGJ,YAAY,QAA0B,SAA4B;AAChE,SAAO;AAEP,OAAK,SAAS;AACd,OAAK,UAAU,WAAW;AAC1B,SAAO,MAAM,yBAAyB;AACjC,OAAK,OAAO,MAAM,CAAC,MAAM,MAAM;AAClC,UAAO,MAAM,YAAY,KAAK,UAAU,EAAE,GAAG;IAC7C;;;CAIJ,MAAc,MAAM,IAA2B;AAC7C,OAAK,SAAS,IAAI,GAAG;AACrB,MAAI;GACF,IAAI;AACJ,WAAQ,QAAQ,KAAK,QAAQ,IAAI,GAAG,KAAK,MAAM,SAAS,GAAG;AAEzD,SAAK,QAAQ,OAAO,GAAG;AACvB,QAAI;KACF,MAAM,MAAM,MAAM,KAAK,MACrB,IACA,MAAM,KAAK,MAAM,EAAE,OAAO,CAC3B;AACD,WAAM,SAAS,MAAM,EAAE,QAAQ,IAAI,CAAC;aAC7B,GAAG;AACV,WAAM,SAAS,MAAM,EAAE,OAAO,EAAE,CAAC;;;YAG7B;AACR,QAAK,SAAS,OAAO,GAAG;;;;CAK5B,MAAc,MAAM,IAAY,SAA8C;AAC5E,SAAO,MAAM,YAAY,QAAQ,OAAO,qBAAqB,KAAK;EAClE,MAAM,cAAc;AACpB,OAAK,IAAI,IAAI,GAAG,IAAI,aAAa,IAC/B,KAAI;GAKF,IAAI,aAAmF,EACrF,GALU,MAAM,KAAK,OAAO,IAAY,GAAG,EAM5C;AACD,QAAK,MAAM,UAAU,QACnB,cACE,OAAO,WAAW,aACd;IAAE,GAAG;IAAY,GAAI,OAAkC,WAAW;IAAE,GACpE;IAAE,GAAG;IAAY,GAAI;IAAmB;GAGhD,MAAM,MAAM,MAAM,KAAK,QAAQ,IAAI,WAAW;AAC9C,UAAO;IAAE,GAAG;IAAY,MAAM,IAAI;IAAK;WAChC,GAAQ;AACf,OAAI,EAAE,SAAS,cAAc,IAAI,cAAc,GAAG;AAChD,WAAO,KAAK,8BAA8B,GAAG,WAAW,IAAI,IAAI;AAChE,UAAM,IAAI,SAAS,QAAQ,WAAW,KAAK,KAAK,KAAK,QAAQ,CAAC,CAAC;cACtD,EAAE,SAAS,eAAe,MAAM,GAAG;AAE5C,WAAO,KAAK,qBAAqB,GAAG,wCAAwC;AAC5E,UAAM;UACD;AACL,WAAO,MAAM,mCAAmC,EAAE,KAAK,KAAK,UAAU,EAAE,GAAG;AAC3E,UAAM;;;AAKZ,QAAM,IAAI,MAAM,8BAA8B,GAAG,SAAS,YAAY,WAAW"}
|
|
@@ -1473,8 +1473,10 @@ var init_updateQueue = __esm({
|
|
|
1473
1473
|
init_logger();
|
|
1474
1474
|
UpdateQueue = class extends Loggable {
|
|
1475
1475
|
_className = "UpdateQueue";
|
|
1476
|
-
|
|
1477
|
-
|
|
1476
|
+
/** Updates not yet taken by a write, per doc. */
|
|
1477
|
+
pending = /* @__PURE__ */ new Map();
|
|
1478
|
+
/** Docs with a drain loop running. */
|
|
1479
|
+
draining = /* @__PURE__ */ new Set();
|
|
1478
1480
|
readDB;
|
|
1479
1481
|
// Database for read operations
|
|
1480
1482
|
writeDB;
|
|
@@ -1492,6 +1494,9 @@ var init_updateQueue = __esm({
|
|
|
1492
1494
|
* **Error Handling Pattern:**
|
|
1493
1495
|
* - This method does NOT create documents if they don't exist
|
|
1494
1496
|
* - Callers are responsible for handling 404 errors and creating documents
|
|
1497
|
+
* - A rejected update is dropped from the queue: a caller that creates the
|
|
1498
|
+
* doc after a 404 has already applied its change and must not see it
|
|
1499
|
+
* re-applied by a later update to the same doc
|
|
1495
1500
|
* - This design maintains separation of concerns (UpdateQueue handles conflicts, callers handle lifecycle)
|
|
1496
1501
|
*
|
|
1497
1502
|
* @example
|
|
@@ -1508,12 +1513,16 @@ var init_updateQueue = __esm({
|
|
|
1508
1513
|
*/
|
|
1509
1514
|
update(id, update) {
|
|
1510
1515
|
logger.debug(`Update requested on doc: ${id}`);
|
|
1511
|
-
|
|
1512
|
-
this.
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1516
|
+
return new Promise((resolve, reject) => {
|
|
1517
|
+
const queue = this.pending.get(id) ?? [];
|
|
1518
|
+
queue.push({
|
|
1519
|
+
update,
|
|
1520
|
+
resolve,
|
|
1521
|
+
reject
|
|
1522
|
+
});
|
|
1523
|
+
this.pending.set(id, queue);
|
|
1524
|
+
if (!this.draining.has(id)) void this.drain(id);
|
|
1525
|
+
});
|
|
1517
1526
|
}
|
|
1518
1527
|
constructor(readDB, writeDB) {
|
|
1519
1528
|
super();
|
|
@@ -1524,64 +1533,56 @@ var init_updateQueue = __esm({
|
|
|
1524
1533
|
logger.debug(`db info: ${JSON.stringify(i)}`);
|
|
1525
1534
|
});
|
|
1526
1535
|
}
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1536
|
+
/** Writes the doc's queued updates, batch by batch, until none are left. */
|
|
1537
|
+
async drain(id) {
|
|
1538
|
+
this.draining.add(id);
|
|
1539
|
+
try {
|
|
1540
|
+
let batch;
|
|
1541
|
+
while ((batch = this.pending.get(id)) && batch.length > 0) {
|
|
1542
|
+
this.pending.delete(id);
|
|
1543
|
+
try {
|
|
1544
|
+
const doc = await this.write(
|
|
1545
|
+
id,
|
|
1546
|
+
batch.map((p) => p.update)
|
|
1547
|
+
);
|
|
1548
|
+
batch.forEach((p) => p.resolve(doc));
|
|
1549
|
+
} catch (e) {
|
|
1550
|
+
batch.forEach((p) => p.reject(e));
|
|
1551
|
+
}
|
|
1532
1552
|
}
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
} catch (e) {
|
|
1563
|
-
if (e.name === "conflict" && i < MAX_RETRIES - 1) {
|
|
1564
|
-
logger.warn(`Conflict on update for doc ${id}, retry #${i + 1}`);
|
|
1565
|
-
await new Promise((res) => setTimeout(res, 50 * Math.random()));
|
|
1566
|
-
} else if (e.name === "not_found" && i === 0) {
|
|
1567
|
-
logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);
|
|
1568
|
-
delete this.inprogressUpdates[id];
|
|
1569
|
-
throw e;
|
|
1570
|
-
} else {
|
|
1571
|
-
delete this.inprogressUpdates[id];
|
|
1572
|
-
if (this.pendingUpdates[id]) {
|
|
1573
|
-
delete this.pendingUpdates[id];
|
|
1574
|
-
}
|
|
1575
|
-
logger.error(`Error on attemped update (retry ${i}): ${JSON.stringify(e)}`);
|
|
1576
|
-
throw e;
|
|
1577
|
-
}
|
|
1578
|
-
}
|
|
1553
|
+
} finally {
|
|
1554
|
+
this.draining.delete(id);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
/** One read-apply-put of `updates`, retried on conflict against a fresh read. */
|
|
1558
|
+
async write(id, updates) {
|
|
1559
|
+
logger.debug(`Applying ${updates.length} update(s) on doc: ${id}`);
|
|
1560
|
+
const MAX_RETRIES = 5;
|
|
1561
|
+
for (let i = 0; i < MAX_RETRIES; i++) {
|
|
1562
|
+
try {
|
|
1563
|
+
const doc = await this.readDB.get(id);
|
|
1564
|
+
let updatedDoc = {
|
|
1565
|
+
...doc
|
|
1566
|
+
};
|
|
1567
|
+
for (const update of updates) {
|
|
1568
|
+
updatedDoc = typeof update === "function" ? { ...updatedDoc, ...update(updatedDoc) } : { ...updatedDoc, ...update };
|
|
1569
|
+
}
|
|
1570
|
+
const res = await this.writeDB.put(updatedDoc);
|
|
1571
|
+
return { ...updatedDoc, _rev: res.rev };
|
|
1572
|
+
} catch (e) {
|
|
1573
|
+
if (e.name === "conflict" && i < MAX_RETRIES - 1) {
|
|
1574
|
+
logger.warn(`Conflict on update for doc ${id}, retry #${i + 1}`);
|
|
1575
|
+
await new Promise((res) => setTimeout(res, 50 * Math.random()));
|
|
1576
|
+
} else if (e.name === "not_found" && i === 0) {
|
|
1577
|
+
logger.warn(`Update failed for ${id} - does not exist. Throwing to caller.`);
|
|
1578
|
+
throw e;
|
|
1579
|
+
} else {
|
|
1580
|
+
logger.error(`Error on attemped update (retry ${i}): ${JSON.stringify(e)}`);
|
|
1581
|
+
throw e;
|
|
1579
1582
|
}
|
|
1580
|
-
throw new Error(`UpdateQueue failed for doc ${id} after ${MAX_RETRIES} retries.`);
|
|
1581
|
-
} else {
|
|
1582
|
-
throw new Error(`Empty Updates Queue Triggered`);
|
|
1583
1583
|
}
|
|
1584
1584
|
}
|
|
1585
|
+
throw new Error(`UpdateQueue failed for doc ${id} after ${MAX_RETRIES} retries.`);
|
|
1585
1586
|
}
|
|
1586
1587
|
};
|
|
1587
1588
|
}
|
|
@@ -1681,6 +1682,23 @@ var init_WeightedFilter = __esm({
|
|
|
1681
1682
|
}
|
|
1682
1683
|
});
|
|
1683
1684
|
|
|
1685
|
+
// src/util/perfMarks.ts
|
|
1686
|
+
function perfMeasure(name, start, end, detail) {
|
|
1687
|
+
try {
|
|
1688
|
+
globalThis.performance?.measure(name, {
|
|
1689
|
+
start,
|
|
1690
|
+
...end !== void 0 ? { end } : {},
|
|
1691
|
+
...detail ? { detail } : {}
|
|
1692
|
+
});
|
|
1693
|
+
} catch {
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
var init_perfMarks = __esm({
|
|
1697
|
+
"src/util/perfMarks.ts"() {
|
|
1698
|
+
"use strict";
|
|
1699
|
+
}
|
|
1700
|
+
});
|
|
1701
|
+
|
|
1684
1702
|
// src/core/orchestration/index.ts
|
|
1685
1703
|
function fnv1a(str) {
|
|
1686
1704
|
let hash = 2166136261;
|
|
@@ -1967,6 +1985,7 @@ var init_Pipeline = __esm({
|
|
|
1967
1985
|
import_common = require("@vue-skuilder/common");
|
|
1968
1986
|
init_navigators();
|
|
1969
1987
|
init_logger();
|
|
1988
|
+
init_perfMarks();
|
|
1970
1989
|
init_orchestration();
|
|
1971
1990
|
init_PipelineDebugger();
|
|
1972
1991
|
init_diversityRerank();
|
|
@@ -2170,6 +2189,13 @@ var init_Pipeline = __esm({
|
|
|
2170
2189
|
logger.info(
|
|
2171
2190
|
`[Pipeline:timing] total=${(tFilter - t0).toFixed(0)}ms (context=${(tContext - t0).toFixed(0)} generate=${(tGenerate - tContext).toFixed(0)} hydrate=${(tHydrate - tGenerate).toFixed(0)} filter=${(tFilter - tHydrate).toFixed(0)})`
|
|
2172
2191
|
);
|
|
2192
|
+
perfMeasure("sk:pipeline-run", t0, tFilter, {
|
|
2193
|
+
context: Math.round(tContext - t0),
|
|
2194
|
+
generate: Math.round(tGenerate - tContext),
|
|
2195
|
+
hydrate: Math.round(tHydrate - tGenerate),
|
|
2196
|
+
filter: Math.round(tFilter - tHydrate),
|
|
2197
|
+
binding: this.course?.readsLocal === void 0 ? "unknown" : this.course.readsLocal ? "local" : "remote"
|
|
2198
|
+
});
|
|
2173
2199
|
const topScores = result.slice(0, 3).map((c) => c.score);
|
|
2174
2200
|
logExecutionSummary(
|
|
2175
2201
|
this.generator.name,
|
|
@@ -4907,6 +4933,9 @@ var init_BaseUserDB = __esm({
|
|
|
4907
4933
|
// Database to use for write operations (local-first approach)
|
|
4908
4934
|
updateQueue;
|
|
4909
4935
|
_hydration = { state: "not-required" };
|
|
4936
|
+
/** See scanScheduledCards. Bumped by every write to scheduled reviews. */
|
|
4937
|
+
_scheduledScan = null;
|
|
4938
|
+
_scheduledGeneration = 0;
|
|
4910
4939
|
/** In-flight hydration, so concurrent callers can wait for a real answer. */
|
|
4911
4940
|
_hydrationPromise = null;
|
|
4912
4941
|
/**
|
|
@@ -5151,18 +5180,38 @@ Currently logged-in as ${this._username}.`
|
|
|
5151
5180
|
*
|
|
5152
5181
|
*/
|
|
5153
5182
|
async getActiveCards() {
|
|
5183
|
+
return (await this.scanScheduledCards()).map((doc) => ({
|
|
5184
|
+
courseID: doc.courseId,
|
|
5185
|
+
cardID: doc.cardId
|
|
5186
|
+
}));
|
|
5187
|
+
}
|
|
5188
|
+
/**
|
|
5189
|
+
* The user's scheduled reviews (`card_review_*`), all of them: one `allDocs`
|
|
5190
|
+
* scan shared by concurrent readers. A pipeline run's generators start
|
|
5191
|
+
* together (`CompositeGenerator`), and each used to scan the range itself
|
|
5192
|
+
* (`getActiveCards` from ELO and Prescribed, `getReviewstoDate` from SRS).
|
|
5193
|
+
* Sharing only an in-flight scan keeps reads as fresh as before; a schedule
|
|
5194
|
+
* or removal of a review starts a new scan for anyone reading after it.
|
|
5195
|
+
* Docs are shared between callers: copy before mutating.
|
|
5196
|
+
*/
|
|
5197
|
+
scanScheduledCards() {
|
|
5198
|
+
const inFlight = this._scheduledScan;
|
|
5199
|
+
if (inFlight && inFlight.generation === this._scheduledGeneration) return inFlight.scan;
|
|
5154
5200
|
const keys = getStartAndEndKeys(DocTypePrefixes["SCHEDULED_CARD" /* SCHEDULED_CARD */]);
|
|
5155
|
-
const
|
|
5156
|
-
|
|
5157
|
-
|
|
5158
|
-
|
|
5159
|
-
|
|
5160
|
-
|
|
5161
|
-
|
|
5162
|
-
|
|
5163
|
-
|
|
5164
|
-
|
|
5165
|
-
|
|
5201
|
+
const entry = {
|
|
5202
|
+
generation: this._scheduledGeneration,
|
|
5203
|
+
scan: this.remoteDB.allDocs({
|
|
5204
|
+
startkey: keys.startkey,
|
|
5205
|
+
endkey: keys.endkey,
|
|
5206
|
+
include_docs: true
|
|
5207
|
+
}).then((res) => res.rows.map((row) => row.doc))
|
|
5208
|
+
};
|
|
5209
|
+
this._scheduledScan = entry;
|
|
5210
|
+
const done = () => {
|
|
5211
|
+
if (this._scheduledScan === entry) this._scheduledScan = null;
|
|
5212
|
+
};
|
|
5213
|
+
entry.scan.then(done, done);
|
|
5214
|
+
return entry.scan;
|
|
5166
5215
|
}
|
|
5167
5216
|
async getActivityRecords() {
|
|
5168
5217
|
try {
|
|
@@ -5228,28 +5277,23 @@ Currently logged-in as ${this._username}.`
|
|
|
5228
5277
|
}
|
|
5229
5278
|
}
|
|
5230
5279
|
async getReviewstoDate(targetDate, course_id) {
|
|
5231
|
-
const
|
|
5232
|
-
const reviews = await this.remoteDB.allDocs({
|
|
5233
|
-
startkey: keys.startkey,
|
|
5234
|
-
endkey: keys.endkey,
|
|
5235
|
-
include_docs: true
|
|
5236
|
-
});
|
|
5280
|
+
const reviews = await this.scanScheduledCards();
|
|
5237
5281
|
log2(
|
|
5238
5282
|
`Fetching ${this._username}'s scheduled reviews${course_id ? ` for course ${course_id}` : ""}.`
|
|
5239
5283
|
);
|
|
5240
|
-
return reviews.
|
|
5241
|
-
if (
|
|
5284
|
+
return reviews.filter((doc) => {
|
|
5285
|
+
if (doc._id.startsWith(DocTypePrefixes["SCHEDULED_CARD" /* SCHEDULED_CARD */])) {
|
|
5242
5286
|
const date = import_moment6.default.utc(
|
|
5243
|
-
|
|
5287
|
+
doc._id.substr(DocTypePrefixes["SCHEDULED_CARD" /* SCHEDULED_CARD */].length),
|
|
5244
5288
|
REVIEW_TIME_FORMAT
|
|
5245
5289
|
);
|
|
5246
5290
|
if (targetDate.isAfter(date)) {
|
|
5247
|
-
if (course_id === void 0 ||
|
|
5291
|
+
if (course_id === void 0 || doc.courseId === course_id) {
|
|
5248
5292
|
return true;
|
|
5249
5293
|
}
|
|
5250
5294
|
}
|
|
5251
5295
|
}
|
|
5252
|
-
}).map((
|
|
5296
|
+
}).map((doc) => ({ ...doc }));
|
|
5253
5297
|
}
|
|
5254
5298
|
async getReviewsForcast(daysCount, course_id) {
|
|
5255
5299
|
const time = import_moment6.default.utc().add(daysCount, "days");
|
|
@@ -5633,22 +5677,20 @@ Currently logged-in as ${this._username}.`
|
|
|
5633
5677
|
async putCardRecord(record) {
|
|
5634
5678
|
const cardHistoryID = getCardHistoryID(record.courseID, record.cardID);
|
|
5635
5679
|
record.timeStamp = import_moment6.default.utc(record.timeStamp).toString();
|
|
5636
|
-
|
|
5637
|
-
|
|
5638
|
-
|
|
5639
|
-
|
|
5640
|
-
|
|
5641
|
-
|
|
5642
|
-
|
|
5643
|
-
|
|
5644
|
-
|
|
5645
|
-
|
|
5646
|
-
|
|
5647
|
-
|
|
5648
|
-
|
|
5649
|
-
|
|
5650
|
-
}
|
|
5651
|
-
);
|
|
5680
|
+
const append = (h) => {
|
|
5681
|
+
h.records.push(record);
|
|
5682
|
+
h.bestInterval = h.bestInterval || 0;
|
|
5683
|
+
if (areQuestionRecords(h)) {
|
|
5684
|
+
h.lapses = getLapses(h.records);
|
|
5685
|
+
h.streak = getStreak(h.records);
|
|
5686
|
+
} else {
|
|
5687
|
+
h.lapses = h.lapses || 0;
|
|
5688
|
+
h.streak = h.streak || 0;
|
|
5689
|
+
}
|
|
5690
|
+
return h;
|
|
5691
|
+
};
|
|
5692
|
+
const appendToExisting = async () => {
|
|
5693
|
+
const cardHistory = await this.update(cardHistoryID, append);
|
|
5652
5694
|
cardHistory.records = cardHistory.records.map((record2) => {
|
|
5653
5695
|
const ret = {
|
|
5654
5696
|
...record2
|
|
@@ -5657,6 +5699,9 @@ Currently logged-in as ${this._username}.`
|
|
|
5657
5699
|
return ret;
|
|
5658
5700
|
});
|
|
5659
5701
|
return cardHistory;
|
|
5702
|
+
};
|
|
5703
|
+
try {
|
|
5704
|
+
return await appendToExisting();
|
|
5660
5705
|
} catch (e) {
|
|
5661
5706
|
const reason = e;
|
|
5662
5707
|
if (reason.status === 404) {
|
|
@@ -5673,6 +5718,9 @@ Currently logged-in as ${this._username}.`
|
|
|
5673
5718
|
const putResult = await this.writeDB.put(initCardHistory);
|
|
5674
5719
|
return { ...initCardHistory, _rev: putResult.rev };
|
|
5675
5720
|
} catch (creationError) {
|
|
5721
|
+
if (creationError.name === "conflict") {
|
|
5722
|
+
return appendToExisting();
|
|
5723
|
+
}
|
|
5676
5724
|
throw new Error(
|
|
5677
5725
|
`Failed to create CardHistory for ${cardHistoryID}. Reason: ${creationError}`
|
|
5678
5726
|
);
|
|
@@ -5716,6 +5764,7 @@ Currently logged-in as ${this._username}.`
|
|
|
5716
5764
|
const deletePromises = duplicateDocIds.map(async (docId) => {
|
|
5717
5765
|
try {
|
|
5718
5766
|
const doc = await this.remoteDB.get(docId);
|
|
5767
|
+
this._scheduledGeneration++;
|
|
5719
5768
|
await this.writeDB.remove(doc);
|
|
5720
5769
|
log2(`Successfully removed duplicate review: ${docId}`);
|
|
5721
5770
|
} catch (error) {
|
|
@@ -5861,9 +5910,11 @@ Currently logged-in as ${this._username}.`
|
|
|
5861
5910
|
}
|
|
5862
5911
|
}
|
|
5863
5912
|
async scheduleCardReview(review) {
|
|
5913
|
+
this._scheduledGeneration++;
|
|
5864
5914
|
return scheduleCardReviewLocal(this.writeDB, review);
|
|
5865
5915
|
}
|
|
5866
5916
|
async removeScheduledCardReview(reviewId) {
|
|
5917
|
+
this._scheduledGeneration++;
|
|
5867
5918
|
return removeScheduledCardReviewLocal(this.writeDB, reviewId);
|
|
5868
5919
|
}
|
|
5869
5920
|
async registerForClassroom(_classId, _registerAs) {
|