@vue-skuilder/db 0.2.36 → 0.2.37
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/core/index.cjs +91 -70
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/navigators/generators/prescribed.mjs +29 -11
- package/dist/core/navigators/generators/prescribed.mjs.map +1 -1
- package/dist/core/versionStamps.mjs +1 -1
- package/dist/impl/common/BaseUserDB.mjs +14 -2
- package/dist/impl/common/BaseUserDB.mjs.map +1 -1
- package/dist/impl/couch/CourseSyncService.mjs +88 -10
- package/dist/impl/couch/CourseSyncService.mjs.map +1 -1
- package/dist/impl/couch/PouchDataLayerProvider.mjs +16 -1
- package/dist/impl/couch/PouchDataLayerProvider.mjs.map +1 -1
- package/dist/impl/couch/courseAPI.mjs +24 -10
- package/dist/impl/couch/courseAPI.mjs.map +1 -1
- package/dist/impl/couch/courseDB.mjs +15 -9
- package/dist/impl/couch/courseDB.mjs.map +1 -1
- package/dist/impl/couch/index.cjs +163 -62
- package/dist/impl/couch/index.cjs.map +1 -1
- package/dist/impl/couch/index.d.ts +22 -1
- package/dist/impl/couch/index.mjs +20 -3
- package/dist/impl/couch/index.mjs.map +1 -1
- package/dist/impl/static/index.cjs +39 -15
- package/dist/impl/static/index.cjs.map +1 -1
- package/dist/index.cjs +202 -92
- package/dist/index.cjs.map +1 -1
- package/package.json +3 -3
- package/src/core/navigators/generators/prescribed.ts +42 -16
- package/src/impl/common/BaseUserDB.ts +15 -2
- package/src/impl/couch/CourseSyncService.ts +130 -20
- package/src/impl/couch/PouchDataLayerProvider.ts +14 -1
- package/src/impl/couch/courseAPI.ts +26 -12
- package/src/impl/couch/courseDB.ts +20 -12
- package/src/impl/couch/index.ts +16 -5
- package/tests/impl/courseSync.test.ts +139 -0
package/dist/index.cjs
CHANGED
|
@@ -1582,6 +1582,12 @@ var init_prescribed = __esm({
|
|
|
1582
1582
|
PrescribedCardsGenerator = class extends ContentNavigator {
|
|
1583
1583
|
name;
|
|
1584
1584
|
config;
|
|
1585
|
+
/**
|
|
1586
|
+
* Course-wide reads that don't change within a navigator's lifetime (the
|
|
1587
|
+
* navigator is cached per CourseDB, minutes), loaded once, not per run.
|
|
1588
|
+
*/
|
|
1589
|
+
_cardsByTag = null;
|
|
1590
|
+
_hierarchyConfigs = null;
|
|
1585
1591
|
constructor(user, course, strategyData) {
|
|
1586
1592
|
super(user, course, strategyData);
|
|
1587
1593
|
this.name = strategyData.name || "Prescribed Cards";
|
|
@@ -1614,20 +1620,7 @@ var init_prescribed = __esm({
|
|
|
1614
1620
|
const allSupportIds = dedupe(this.config.groups.flatMap((g) => g.supportCardIds ?? []));
|
|
1615
1621
|
const allRelevantIds = dedupe([...allTargetIds, ...allSupportIds]);
|
|
1616
1622
|
const tagsByCard = allRelevantIds.length > 0 ? await this.course.getAppliedTagsBatch(allRelevantIds) : /* @__PURE__ */ new Map();
|
|
1617
|
-
const
|
|
1618
|
-
() => ({
|
|
1619
|
-
rows: [],
|
|
1620
|
-
offset: 0,
|
|
1621
|
-
total_rows: 0
|
|
1622
|
-
})
|
|
1623
|
-
);
|
|
1624
|
-
const cardsByTag = /* @__PURE__ */ new Map();
|
|
1625
|
-
for (const row of courseTagDocs.rows ?? []) {
|
|
1626
|
-
const tagDoc = row.doc;
|
|
1627
|
-
if (tagDoc?.name && Array.isArray(tagDoc.taggedCards)) {
|
|
1628
|
-
cardsByTag.set(tagDoc.name, [...tagDoc.taggedCards]);
|
|
1629
|
-
}
|
|
1630
|
-
}
|
|
1623
|
+
const cardsByTag = await this.loadCardsByTag();
|
|
1631
1624
|
const nextState = {
|
|
1632
1625
|
updatedAt: isoNow(),
|
|
1633
1626
|
groups: {}
|
|
@@ -1867,7 +1860,33 @@ var init_prescribed = __esm({
|
|
|
1867
1860
|
return { groups: [] };
|
|
1868
1861
|
}
|
|
1869
1862
|
}
|
|
1870
|
-
|
|
1863
|
+
/** Tag name → tagged card IDs, from the course's tag docs (once per instance). */
|
|
1864
|
+
loadCardsByTag() {
|
|
1865
|
+
if (!this._cardsByTag) {
|
|
1866
|
+
this._cardsByTag = this.course.getCourseTagStubs().then((docs) => {
|
|
1867
|
+
const cardsByTag = /* @__PURE__ */ new Map();
|
|
1868
|
+
for (const row of docs.rows ?? []) {
|
|
1869
|
+
const tagDoc = row.doc;
|
|
1870
|
+
if (tagDoc?.name && Array.isArray(tagDoc.taggedCards)) {
|
|
1871
|
+
cardsByTag.set(tagDoc.name, [...tagDoc.taggedCards]);
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
return cardsByTag;
|
|
1875
|
+
}).catch((e) => {
|
|
1876
|
+
this._cardsByTag = null;
|
|
1877
|
+
logger.debug(`[Prescribed] Failed to load tag docs: ${e}`);
|
|
1878
|
+
return /* @__PURE__ */ new Map();
|
|
1879
|
+
});
|
|
1880
|
+
}
|
|
1881
|
+
return this._cardsByTag;
|
|
1882
|
+
}
|
|
1883
|
+
loadHierarchyConfigs() {
|
|
1884
|
+
if (!this._hierarchyConfigs) {
|
|
1885
|
+
this._hierarchyConfigs = this.fetchHierarchyConfigs();
|
|
1886
|
+
}
|
|
1887
|
+
return this._hierarchyConfigs;
|
|
1888
|
+
}
|
|
1889
|
+
async fetchHierarchyConfigs() {
|
|
1871
1890
|
try {
|
|
1872
1891
|
const strategies = await this.course.getAllNavigationStrategies();
|
|
1873
1892
|
return strategies.filter((s) => s.implementingClass === "hierarchyDefinition").map((s) => {
|
|
@@ -1881,6 +1900,7 @@ var init_prescribed = __esm({
|
|
|
1881
1900
|
}
|
|
1882
1901
|
});
|
|
1883
1902
|
} catch (e) {
|
|
1903
|
+
this._hierarchyConfigs = null;
|
|
1884
1904
|
logger.debug(`[Prescribed] Failed to load hierarchy configs: ${e}`);
|
|
1885
1905
|
return [];
|
|
1886
1906
|
}
|
|
@@ -3604,7 +3624,7 @@ var FRAMEWORK_VERSION, hostAppVersion, DEFAULT_STRATEGY_HASH;
|
|
|
3604
3624
|
var init_versionStamps = __esm({
|
|
3605
3625
|
"src/core/versionStamps.ts"() {
|
|
3606
3626
|
"use strict";
|
|
3607
|
-
FRAMEWORK_VERSION = true ? "0.2.
|
|
3627
|
+
FRAMEWORK_VERSION = true ? "0.2.37" : "dev";
|
|
3608
3628
|
DEFAULT_STRATEGY_HASH = "default";
|
|
3609
3629
|
}
|
|
3610
3630
|
});
|
|
@@ -4707,9 +4727,9 @@ var init_CardHydrationService = __esm({
|
|
|
4707
4727
|
import_common10 = require("@vue-skuilder/common");
|
|
4708
4728
|
init_logger();
|
|
4709
4729
|
CardHydrationService = class {
|
|
4710
|
-
constructor(getViewComponent,
|
|
4730
|
+
constructor(getViewComponent, getCourseDB2, getItemsToHydrate) {
|
|
4711
4731
|
this.getViewComponent = getViewComponent;
|
|
4712
|
-
this.getCourseDB =
|
|
4732
|
+
this.getCourseDB = getCourseDB2;
|
|
4713
4733
|
this.getItemsToHydrate = getItemsToHydrate;
|
|
4714
4734
|
}
|
|
4715
4735
|
hydratedCards = /* @__PURE__ */ new Map();
|
|
@@ -10871,6 +10891,18 @@ Currently logged-in as ${this._username}.`
|
|
|
10871
10891
|
update(id, update) {
|
|
10872
10892
|
return this.updateQueue.update(id, update);
|
|
10873
10893
|
}
|
|
10894
|
+
/**
|
|
10895
|
+
* Create a default doc after a 404. Concurrent first reads (e.g. two
|
|
10896
|
+
* components on a brand-new user) both see the 404 and both create; the
|
|
10897
|
+
* loser's conflict just means the doc now exists, which is what it wanted.
|
|
10898
|
+
*/
|
|
10899
|
+
async putDefaultUnlessCreated(doc) {
|
|
10900
|
+
try {
|
|
10901
|
+
await this.localDB.put(doc);
|
|
10902
|
+
} catch (e) {
|
|
10903
|
+
if (e.status !== 409) throw e;
|
|
10904
|
+
}
|
|
10905
|
+
}
|
|
10874
10906
|
async getCourseRegistrationsDoc() {
|
|
10875
10907
|
logger.debug(`Fetching courseRegistrations for ${this.getUsername()}`);
|
|
10876
10908
|
let ret;
|
|
@@ -10887,7 +10919,7 @@ Currently logged-in as ${this._username}.`
|
|
|
10887
10919
|
`Cannot read course registrations for ${this._username}: the local mirror is unavailable (hydration state '${this._hydration.state}'). Refusing to create an empty registration doc \u2014 that would report an existing user as unregistered, and then lose to their real document once sync resumes.`
|
|
10888
10920
|
);
|
|
10889
10921
|
}
|
|
10890
|
-
await this.
|
|
10922
|
+
await this.putDefaultUnlessCreated({
|
|
10891
10923
|
_id: _BaseUser.DOC_IDS.COURSE_REGISTRATIONS,
|
|
10892
10924
|
courses: [],
|
|
10893
10925
|
studyWeight: {}
|
|
@@ -11134,7 +11166,7 @@ Currently logged-in as ${this._username}.`
|
|
|
11134
11166
|
`Cannot read config for ${this._username}: the local mirror is unavailable (hydration state '${this._hydration.state}'). Refusing to write a default config over what may be an existing one.`
|
|
11135
11167
|
);
|
|
11136
11168
|
}
|
|
11137
|
-
await this.
|
|
11169
|
+
await this.putDefaultUnlessCreated(defaultConfig);
|
|
11138
11170
|
return this.getConfig();
|
|
11139
11171
|
} else {
|
|
11140
11172
|
logger.error(`Error setting user default config:`, e);
|
|
@@ -11841,14 +11873,26 @@ async function addCard(courseID, course, id_displayable_data, id_view, elo, tags
|
|
|
11841
11873
|
}
|
|
11842
11874
|
return card;
|
|
11843
11875
|
}
|
|
11876
|
+
function invalidateCourseConfigCache(courseID) {
|
|
11877
|
+
courseConfigCache.delete(courseID);
|
|
11878
|
+
}
|
|
11844
11879
|
async function getCredentialledCourseConfig(courseID) {
|
|
11845
|
-
|
|
11880
|
+
const hit = courseConfigCache.get(courseID);
|
|
11881
|
+
if (hit && Date.now() - hit.at < COURSE_CONFIG_TTL_MS) {
|
|
11882
|
+
return structuredClone(await hit.config);
|
|
11883
|
+
}
|
|
11884
|
+
const config = (async () => {
|
|
11846
11885
|
const db = getCourseDB(courseID);
|
|
11847
11886
|
const ret = await db.get("CourseConfig");
|
|
11848
11887
|
ret.courseID = courseID;
|
|
11849
|
-
logger.
|
|
11888
|
+
logger.debug(`Fetched course config for ${courseID}`);
|
|
11850
11889
|
return ret;
|
|
11890
|
+
})();
|
|
11891
|
+
courseConfigCache.set(courseID, { at: Date.now(), config });
|
|
11892
|
+
try {
|
|
11893
|
+
return structuredClone(await config);
|
|
11851
11894
|
} catch (e) {
|
|
11895
|
+
courseConfigCache.delete(courseID);
|
|
11852
11896
|
logger.error(`Error fetching config for ${courseID}:`, e);
|
|
11853
11897
|
throw e;
|
|
11854
11898
|
}
|
|
@@ -11913,20 +11957,11 @@ function getTagID(tagName) {
|
|
|
11913
11957
|
return tagPrefix + tagName;
|
|
11914
11958
|
}
|
|
11915
11959
|
}
|
|
11916
|
-
|
|
11917
|
-
const dbName = `coursedb-${courseID}`;
|
|
11918
|
-
return new pouchdb_setup_default(
|
|
11919
|
-
ENV.COUCHDB_SERVER_PROTOCOL + "://" + ENV.COUCHDB_SERVER_URL + dbName,
|
|
11920
|
-
createPouchDBConfig()
|
|
11921
|
-
);
|
|
11922
|
-
}
|
|
11923
|
-
var import_common14, import_common15, import_common16, import_uuid, AlreadyTaggedErr;
|
|
11960
|
+
var import_common14, import_common15, import_common16, import_uuid, COURSE_CONFIG_TTL_MS, courseConfigCache, AlreadyTaggedErr;
|
|
11924
11961
|
var init_courseAPI = __esm({
|
|
11925
11962
|
"src/impl/couch/courseAPI.ts"() {
|
|
11926
11963
|
"use strict";
|
|
11927
|
-
init_pouchdb_setup();
|
|
11928
11964
|
init_couch();
|
|
11929
|
-
init_factory();
|
|
11930
11965
|
import_common14 = require("@vue-skuilder/common");
|
|
11931
11966
|
import_common15 = require("@vue-skuilder/common");
|
|
11932
11967
|
init_courseDB();
|
|
@@ -11935,6 +11970,8 @@ var init_courseAPI = __esm({
|
|
|
11935
11970
|
init_common();
|
|
11936
11971
|
init_logger();
|
|
11937
11972
|
import_uuid = require("uuid");
|
|
11973
|
+
COURSE_CONFIG_TTL_MS = 5 * 60 * 1e3;
|
|
11974
|
+
courseConfigCache = /* @__PURE__ */ new Map();
|
|
11938
11975
|
AlreadyTaggedErr = class extends Error {
|
|
11939
11976
|
constructor(message) {
|
|
11940
11977
|
super(message);
|
|
@@ -12662,21 +12699,10 @@ var init_PipelineAssembler = __esm({
|
|
|
12662
12699
|
function randIntWeightedTowardZero(n) {
|
|
12663
12700
|
return Math.floor(Math.random() * Math.random() * Math.random() * n);
|
|
12664
12701
|
}
|
|
12665
|
-
async function getCourseTagStubs(courseID) {
|
|
12666
|
-
logger.debug(`Getting tag stubs for course: ${courseID}`);
|
|
12667
|
-
const stubs = await filterAllDocsByPrefix2(
|
|
12668
|
-
getCourseDB2(courseID),
|
|
12669
|
-
"TAG" /* TAG */.valueOf() + "-"
|
|
12670
|
-
);
|
|
12671
|
-
stubs.rows.forEach((row) => {
|
|
12672
|
-
logger.debug(` Tag stub for doc: ${row.id}`);
|
|
12673
|
-
});
|
|
12674
|
-
return stubs;
|
|
12675
|
-
}
|
|
12676
12702
|
async function createTag(courseID, tagName, author) {
|
|
12677
12703
|
logger.debug(`Creating tag: ${tagName}...`);
|
|
12678
12704
|
const tagID = getTagID(tagName);
|
|
12679
|
-
const courseDB =
|
|
12705
|
+
const courseDB = getCourseDB(courseID);
|
|
12680
12706
|
const resp = await courseDB.put({
|
|
12681
12707
|
course: courseID,
|
|
12682
12708
|
docType: "TAG" /* TAG */,
|
|
@@ -12691,45 +12717,41 @@ async function createTag(courseID, tagName, author) {
|
|
|
12691
12717
|
}
|
|
12692
12718
|
async function updateTag(tag) {
|
|
12693
12719
|
const prior = await getTag(tag.course, tag.name);
|
|
12694
|
-
return await
|
|
12720
|
+
return await getCourseDB(tag.course).put({
|
|
12695
12721
|
...tag,
|
|
12696
12722
|
_rev: prior._rev
|
|
12697
12723
|
});
|
|
12698
12724
|
}
|
|
12699
12725
|
async function getTag(courseID, tagName) {
|
|
12700
12726
|
const tagID = getTagID(tagName);
|
|
12701
|
-
const courseDB =
|
|
12727
|
+
const courseDB = getCourseDB(courseID);
|
|
12702
12728
|
return courseDB.get(tagID);
|
|
12703
12729
|
}
|
|
12704
12730
|
async function removeTagFromCard(courseID, cardID, tagID) {
|
|
12705
12731
|
tagID = getTagID(tagID);
|
|
12706
|
-
const courseDB =
|
|
12732
|
+
const courseDB = getCourseDB(courseID);
|
|
12707
12733
|
const tag = await courseDB.get(tagID);
|
|
12708
12734
|
tag.taggedCards = tag.taggedCards.filter((taggedID) => {
|
|
12709
12735
|
return cardID !== taggedID;
|
|
12710
12736
|
});
|
|
12711
12737
|
return courseDB.put(tag);
|
|
12712
12738
|
}
|
|
12713
|
-
async function getAppliedTags(id_course, id_card) {
|
|
12714
|
-
const db = getCourseDB2(id_course);
|
|
12715
|
-
const result = await db.query("getTags", {
|
|
12716
|
-
startkey: id_card,
|
|
12717
|
-
endkey: id_card
|
|
12718
|
-
// include_docs: true
|
|
12719
|
-
});
|
|
12720
|
-
return result;
|
|
12721
|
-
}
|
|
12722
12739
|
async function updateCredentialledCourseConfig(courseID, config) {
|
|
12723
12740
|
logger.debug(`Updating course config:
|
|
12724
12741
|
|
|
12725
12742
|
${JSON.stringify(config)}
|
|
12726
12743
|
`);
|
|
12727
|
-
const db =
|
|
12744
|
+
const db = getCourseDB(courseID);
|
|
12745
|
+
invalidateCourseConfigCache(courseID);
|
|
12728
12746
|
const old = await getCredentialledCourseConfig(courseID);
|
|
12729
|
-
|
|
12730
|
-
|
|
12731
|
-
|
|
12732
|
-
|
|
12747
|
+
try {
|
|
12748
|
+
return await db.put({
|
|
12749
|
+
...config,
|
|
12750
|
+
_rev: old._rev
|
|
12751
|
+
});
|
|
12752
|
+
} finally {
|
|
12753
|
+
invalidateCourseConfigCache(courseID);
|
|
12754
|
+
}
|
|
12733
12755
|
}
|
|
12734
12756
|
function isSuccessRow(row) {
|
|
12735
12757
|
return "doc" in row && row.doc !== null && row.doc !== void 0;
|
|
@@ -12828,7 +12850,7 @@ var init_courseDB = __esm({
|
|
|
12828
12850
|
*/
|
|
12829
12851
|
constructor(id, userLookup, localDB) {
|
|
12830
12852
|
this.id = id;
|
|
12831
|
-
const remote =
|
|
12853
|
+
const remote = getCourseDB(this.id);
|
|
12832
12854
|
this.remoteDB = remote;
|
|
12833
12855
|
this.db = localDB ?? remote;
|
|
12834
12856
|
this._getCurrentUser = userLookup;
|
|
@@ -13032,12 +13054,7 @@ ${above.rows.map((r) => ` ${r.id}-${r.key}
|
|
|
13032
13054
|
}
|
|
13033
13055
|
}
|
|
13034
13056
|
async getAppliedTags(cardId) {
|
|
13035
|
-
|
|
13036
|
-
if (ret) {
|
|
13037
|
-
return ret;
|
|
13038
|
-
} else {
|
|
13039
|
-
throw new Error(`Failed to find tags for card ${this.id}-${cardId}`);
|
|
13040
|
-
}
|
|
13057
|
+
return this.db.query("getTags", { startkey: cardId, endkey: cardId });
|
|
13041
13058
|
}
|
|
13042
13059
|
async getAppliedTagsBatch(cardIds) {
|
|
13043
13060
|
if (cardIds.length === 0) {
|
|
@@ -13094,7 +13111,7 @@ ${above.rows.map((r) => ` ${r.id}-${r.key}
|
|
|
13094
13111
|
return await updateTag(tag);
|
|
13095
13112
|
}
|
|
13096
13113
|
async getCourseTagStubs() {
|
|
13097
|
-
return
|
|
13114
|
+
return filterAllDocsByPrefix2(this.db, "TAG" /* TAG */.valueOf() + "-");
|
|
13098
13115
|
}
|
|
13099
13116
|
async addNote(codeCourse, shape, data, author, tags, uploads, elo = (0, import_common18.blankCourseElo)()) {
|
|
13100
13117
|
try {
|
|
@@ -14198,11 +14215,14 @@ function createPouchDBConfig() {
|
|
|
14198
14215
|
}
|
|
14199
14216
|
return pouchDBincludeCredentialsConfig;
|
|
14200
14217
|
}
|
|
14201
|
-
function
|
|
14202
|
-
|
|
14203
|
-
|
|
14204
|
-
|
|
14205
|
-
|
|
14218
|
+
function getCourseDB(courseID) {
|
|
14219
|
+
const url = ENV.COUCHDB_SERVER_PROTOCOL + "://" + ENV.COUCHDB_SERVER_URL + "coursedb-" + courseID;
|
|
14220
|
+
let db = courseDBHandles.get(url);
|
|
14221
|
+
if (!db) {
|
|
14222
|
+
db = new pouchdb_setup_default(url, { ...createPouchDBConfig(), skip_setup: true });
|
|
14223
|
+
courseDBHandles.set(url, db);
|
|
14224
|
+
}
|
|
14225
|
+
return db;
|
|
14206
14226
|
}
|
|
14207
14227
|
function filterAllDocsByPrefix2(db, prefix, opts) {
|
|
14208
14228
|
const options = {
|
|
@@ -14221,7 +14241,7 @@ function getStartAndEndKeys2(key) {
|
|
|
14221
14241
|
endkey: key + "\uFFF0"
|
|
14222
14242
|
};
|
|
14223
14243
|
}
|
|
14224
|
-
var import_cross_fetch2, import_moment9, GUEST_LOCAL_DB, localUserDB, pouchDBincludeCredentialsConfig, REVIEW_TIME_FORMAT2;
|
|
14244
|
+
var import_cross_fetch2, import_moment9, GUEST_LOCAL_DB, localUserDB, pouchDBincludeCredentialsConfig, courseDBHandles, REVIEW_TIME_FORMAT2;
|
|
14225
14245
|
var init_couch = __esm({
|
|
14226
14246
|
"src/impl/couch/index.ts"() {
|
|
14227
14247
|
"use strict";
|
|
@@ -14245,6 +14265,7 @@ var init_couch = __esm({
|
|
|
14245
14265
|
return pouchdb_setup_default.fetch(url, opts);
|
|
14246
14266
|
}
|
|
14247
14267
|
};
|
|
14268
|
+
courseDBHandles = /* @__PURE__ */ new Map();
|
|
14248
14269
|
REVIEW_TIME_FORMAT2 = "YYYY-MM-DD--kk:mm:ss-SSS";
|
|
14249
14270
|
}
|
|
14250
14271
|
});
|
|
@@ -14254,13 +14275,14 @@ var CourseSyncService_exports = {};
|
|
|
14254
14275
|
__export(CourseSyncService_exports, {
|
|
14255
14276
|
CourseSyncService: () => CourseSyncService
|
|
14256
14277
|
});
|
|
14257
|
-
var DEFAULT_REPLICATION, CourseSyncService;
|
|
14278
|
+
var SYNC_MARKER_ID, DEFAULT_REPLICATION, CourseSyncService;
|
|
14258
14279
|
var init_CourseSyncService = __esm({
|
|
14259
14280
|
"src/impl/couch/CourseSyncService.ts"() {
|
|
14260
14281
|
"use strict";
|
|
14261
14282
|
init_pouchdb_setup();
|
|
14262
14283
|
init_couch();
|
|
14263
14284
|
init_logger();
|
|
14285
|
+
SYNC_MARKER_ID = "_local/sk-course-sync-complete";
|
|
14264
14286
|
DEFAULT_REPLICATION = {
|
|
14265
14287
|
batchSize: 1e3,
|
|
14266
14288
|
batchesLimit: 5
|
|
@@ -14331,19 +14353,7 @@ var init_CourseSyncService = __esm({
|
|
|
14331
14353
|
async ensureSynced(courseId, forceEnabled) {
|
|
14332
14354
|
const existing = this.entries.get(courseId);
|
|
14333
14355
|
if (existing?.status.state === "ready" && existing.localDB) {
|
|
14334
|
-
|
|
14335
|
-
if (!stale) {
|
|
14336
|
-
return;
|
|
14337
|
-
}
|
|
14338
|
-
logger.info(
|
|
14339
|
-
`[CourseSyncService] Remote DB epoch changed for course ${courseId} \u2014 destroying stale local replica`
|
|
14340
|
-
);
|
|
14341
|
-
try {
|
|
14342
|
-
await existing.localDB.destroy();
|
|
14343
|
-
} catch {
|
|
14344
|
-
}
|
|
14345
|
-
existing.localDB = null;
|
|
14346
|
-
existing.readyPromise = null;
|
|
14356
|
+
return;
|
|
14347
14357
|
}
|
|
14348
14358
|
if (existing?.status.state === "disabled") {
|
|
14349
14359
|
return;
|
|
@@ -14357,9 +14367,97 @@ var init_CourseSyncService = __esm({
|
|
|
14357
14367
|
readyPromise: null
|
|
14358
14368
|
};
|
|
14359
14369
|
this.entries.set(courseId, entry);
|
|
14360
|
-
entry.readyPromise = this.
|
|
14370
|
+
entry.readyPromise = this.startSync(courseId, entry, forceEnabled);
|
|
14361
14371
|
return entry.readyPromise;
|
|
14362
14372
|
}
|
|
14373
|
+
/**
|
|
14374
|
+
* A replica completed on an earlier page load is ready now, refreshed in the
|
|
14375
|
+
* background; anything else goes through a full performSync.
|
|
14376
|
+
*/
|
|
14377
|
+
async startSync(courseId, entry, forceEnabled) {
|
|
14378
|
+
const localDB = new pouchdb_setup_default(this.localDBName(courseId));
|
|
14379
|
+
if (await this.hasSyncMarker(localDB)) {
|
|
14380
|
+
entry.localDB = localDB;
|
|
14381
|
+
entry.status = { state: "ready" };
|
|
14382
|
+
entry.readyPromise = null;
|
|
14383
|
+
logger.info(
|
|
14384
|
+
`[CourseSyncService] Using this device's replica of course ${courseId}; refreshing in the background`
|
|
14385
|
+
);
|
|
14386
|
+
void this.refreshInBackground(courseId, entry, forceEnabled);
|
|
14387
|
+
return;
|
|
14388
|
+
}
|
|
14389
|
+
return this.performSync(courseId, entry, forceEnabled);
|
|
14390
|
+
}
|
|
14391
|
+
/**
|
|
14392
|
+
* Bring a ready replica up to date without taking it away from readers.
|
|
14393
|
+
* Failures leave it ready (and outdated).
|
|
14394
|
+
*/
|
|
14395
|
+
async refreshInBackground(courseId, entry, forceEnabled) {
|
|
14396
|
+
const localDB = entry.localDB;
|
|
14397
|
+
try {
|
|
14398
|
+
if (!forceEnabled && !await this.checkLocalSyncEnabled(courseId)) {
|
|
14399
|
+
await this.clearSyncMarker(localDB);
|
|
14400
|
+
logger.info(
|
|
14401
|
+
`[CourseSyncService] Local sync no longer enabled for course ${courseId}; replica kept for this page only`
|
|
14402
|
+
);
|
|
14403
|
+
return;
|
|
14404
|
+
}
|
|
14405
|
+
if (await this.isLocalEpochStale(courseId, localDB)) {
|
|
14406
|
+
await this.clearSyncMarker(localDB);
|
|
14407
|
+
logger.info(
|
|
14408
|
+
`[CourseSyncService] Remote DB recreated for course ${courseId}: keeping the outdated replica for this page; it is rebuilt on the next page load`
|
|
14409
|
+
);
|
|
14410
|
+
return;
|
|
14411
|
+
}
|
|
14412
|
+
const start = Date.now();
|
|
14413
|
+
const result = await this.replicate(this.getRemoteDB(courseId), localDB);
|
|
14414
|
+
const syncTimeMs = Date.now() - start;
|
|
14415
|
+
const warmStart = Date.now();
|
|
14416
|
+
await this.warmViewIndices(localDB);
|
|
14417
|
+
const viewWarmTimeMs = Date.now() - warmStart;
|
|
14418
|
+
await this.writeSyncMarker(localDB);
|
|
14419
|
+
entry.status = {
|
|
14420
|
+
state: "ready",
|
|
14421
|
+
docsReplicated: result.docs_written,
|
|
14422
|
+
syncTimeMs,
|
|
14423
|
+
viewWarmTimeMs
|
|
14424
|
+
};
|
|
14425
|
+
logger.info(
|
|
14426
|
+
`[CourseSyncService] Background refresh of course ${courseId}: ${result.docs_written} docs in ${syncTimeMs}ms, views ${viewWarmTimeMs}ms`
|
|
14427
|
+
);
|
|
14428
|
+
} catch (e) {
|
|
14429
|
+
logger.warn(
|
|
14430
|
+
`[CourseSyncService] Background refresh of course ${courseId} failed; replica kept as is: ${e}`
|
|
14431
|
+
);
|
|
14432
|
+
}
|
|
14433
|
+
}
|
|
14434
|
+
async hasSyncMarker(localDB) {
|
|
14435
|
+
try {
|
|
14436
|
+
await localDB.get(SYNC_MARKER_ID);
|
|
14437
|
+
return true;
|
|
14438
|
+
} catch {
|
|
14439
|
+
return false;
|
|
14440
|
+
}
|
|
14441
|
+
}
|
|
14442
|
+
async writeSyncMarker(localDB) {
|
|
14443
|
+
try {
|
|
14444
|
+
let _rev;
|
|
14445
|
+
try {
|
|
14446
|
+
_rev = (await localDB.get(SYNC_MARKER_ID))._rev;
|
|
14447
|
+
} catch {
|
|
14448
|
+
}
|
|
14449
|
+
await localDB.put({ _id: SYNC_MARKER_ID, _rev, completedAt: (/* @__PURE__ */ new Date()).toISOString() });
|
|
14450
|
+
} catch (e) {
|
|
14451
|
+
logger.warn(`[CourseSyncService] Could not write sync marker: ${e}`);
|
|
14452
|
+
}
|
|
14453
|
+
}
|
|
14454
|
+
async clearSyncMarker(localDB) {
|
|
14455
|
+
try {
|
|
14456
|
+
const marker = await localDB.get(SYNC_MARKER_ID);
|
|
14457
|
+
await localDB.remove(marker);
|
|
14458
|
+
} catch {
|
|
14459
|
+
}
|
|
14460
|
+
}
|
|
14363
14461
|
/**
|
|
14364
14462
|
* Get the local PouchDB for a course, or null if not available.
|
|
14365
14463
|
*
|
|
@@ -14434,6 +14532,7 @@ var init_CourseSyncService = __esm({
|
|
|
14434
14532
|
logger.info(
|
|
14435
14533
|
`[CourseSyncService] View indices warmed for course ${courseId} in ${viewWarmTimeMs}ms`
|
|
14436
14534
|
);
|
|
14535
|
+
await this.writeSyncMarker(localDB);
|
|
14437
14536
|
entry.status = {
|
|
14438
14537
|
state: "ready",
|
|
14439
14538
|
docsReplicated: result.docs_written,
|
|
@@ -14538,7 +14637,7 @@ var init_CourseSyncService = __esm({
|
|
|
14538
14637
|
* Get a remote PouchDB handle for a course.
|
|
14539
14638
|
*/
|
|
14540
14639
|
getRemoteDB(courseId) {
|
|
14541
|
-
return
|
|
14640
|
+
return getCourseDB(courseId);
|
|
14542
14641
|
}
|
|
14543
14642
|
/**
|
|
14544
14643
|
* Local DB naming convention.
|
|
@@ -14574,6 +14673,13 @@ var init_PouchDataLayerProvider = __esm({
|
|
|
14574
14673
|
// the scoped list of courseIDs for a UI focused on a specific course
|
|
14575
14674
|
// or group of courses
|
|
14576
14675
|
_courseIDs = [];
|
|
14676
|
+
/**
|
|
14677
|
+
* One CourseDB per course, so its caches (navigator, ELO pool, tags) last
|
|
14678
|
+
* across sessions and card hydration. Rebuilt when the local replica it reads
|
|
14679
|
+
* from changes (becomes ready, or is replaced); a session keeps the instance
|
|
14680
|
+
* it started with.
|
|
14681
|
+
*/
|
|
14682
|
+
_courseDBs = /* @__PURE__ */ new Map();
|
|
14577
14683
|
constructor(coursIDs) {
|
|
14578
14684
|
if (coursIDs) {
|
|
14579
14685
|
this._courseIDs = coursIDs;
|
|
@@ -14605,7 +14711,11 @@ var init_PouchDataLayerProvider = __esm({
|
|
|
14605
14711
|
}
|
|
14606
14712
|
getCourseDB(courseId) {
|
|
14607
14713
|
const localDB = CourseSyncService.getInstance().getLocalDB(courseId);
|
|
14608
|
-
|
|
14714
|
+
const cached = this._courseDBs.get(courseId);
|
|
14715
|
+
if (cached && cached.localDB === localDB) return cached.db;
|
|
14716
|
+
const db = new CourseDB(courseId, async () => this.getUserDB(), localDB ?? void 0);
|
|
14717
|
+
this._courseDBs.set(courseId, { db, localDB });
|
|
14718
|
+
return db;
|
|
14609
14719
|
}
|
|
14610
14720
|
/**
|
|
14611
14721
|
* Trigger local sync for a course. Call during app initialization or
|