Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(client): track in-flight asynchronous snapshots in SpacedUpdate
Elian Doran85a0a71cf941
diff --git a/apps/client/src/services/spaced_update.spec.ts b/apps/client/src/services/spaced_update.spec.tsindex 9dbe58b810..91c1b6ea39 100644--- a/apps/client/src/services/spaced_update.spec.ts+++ b/apps/client/src/services/spaced_update.spec.ts@@ -325,6 +325,34 @@ describe("SpacedUpdate", () => {expect(commits).toEqual(["v2"]);});+ it("does not report all-saved while an asynchronous snapshot from rebind is still being captured", async () => {+ let resolvePrepare: (value: string) => void = () => {};+ const commits: { key: string; data: string }[] = [];+ const spacedUpdate = new SpacedUpdate<string>({+ key: "A",+ prepare: () => new Promise<string>((resolve) => {+ resolvePrepare = resolve;+ }),+ commit: async (data) => {+ commits.push({ key: "A", data });+ }+ }, 50);++ spacedUpdate.scheduleUpdate();+ spacedUpdate.rebind("B", () => "B content", async (data) => {+ commits.push({ key: "B", data });+ });++ // The old binding's snapshot is still being captured, so nothing may be+ // reported as saved yet (relevant for the beforeunload warning).+ expect(spacedUpdate.isAllSavedAndTriggerUpdate()).toBe(false);++ resolvePrepare("typed into A");+ await spacedUpdate.updateNowIfNecessary();++ expect(commits).toEqual([{ key: "A", data: "typed into A" }]);+ });+it("supports an asynchronous prepare", async () => {const commits: string[] = [];const spacedUpdate = new SpacedUpdate<string>({diff --git a/apps/client/src/services/spaced_update.ts b/apps/client/src/services/spaced_update.tsindex b011bdeb03..6c4cb597ea 100644--- a/apps/client/src/services/spaced_update.ts+++ b/apps/client/src/services/spaced_update.ts@@ -49,6 +49,8 @@ export default class SpacedUpdate<T = void> {* commits and are retried (with backoff) until they land — they are never dropped.*/private pendingCommits = new Map<string | null, PendingCommit<T>>();+ /** Snapshots whose asynchronous `prepare` is still running — captured but not yet queued. */+ private pendingSnapshots = new Set<Promise<void>>();private drainPromise: Promise<void> | null = null;private debounceTimer?: ReturnType<typeof setTimeout>;private retryTimer?: ReturnType<typeof setTimeout>;@@ -115,7 +117,10 @@ export default class SpacedUpdate<T = void> {}isAllSavedAndTriggerUpdate() {- const allSaved = !this.changed && this.pendingCommits.size === 0 && !this.drainPromise;+ const allSaved = !this.changed+ && this.pendingCommits.size === 0+ && this.pendingSnapshots.size === 0+ && !this.drainPromise;this.updateNowIfNecessary().catch(() => {// Failures are logged in runDrain and retried.@@ -213,12 +218,20 @@ export default class SpacedUpdate<T = void> {}if (result instanceof Promise) {- return result.then(- (data) => {- this.pendingCommits.set(bindingKey, { data, commit });- },- restoreOnError- );+ // Track the in-flight snapshot so drain() and isAllSavedAndTriggerUpdate() don't+ // consider everything saved before the snapshot has been queued for commit.+ const snapshot: Promise<void> = result+ .then(+ (data) => {+ this.pendingCommits.set(bindingKey, { data, commit });+ },+ restoreOnError+ )+ .finally(() => {+ this.pendingSnapshots.delete(snapshot);+ });+ this.pendingSnapshots.add(snapshot);+ return snapshot;}this.pendingCommits.set(bindingKey, { data: result, commit });@@ -229,7 +242,7 @@ export default class SpacedUpdate<T = void> {return this.drainPromise;}- if (!this.changed && this.pendingCommits.size === 0) {+ if (!this.changed && this.pendingCommits.size === 0 && this.pendingSnapshots.size === 0) {return Promise.resolve();}@@ -242,6 +255,12 @@ export default class SpacedUpdate<T = void> {private async runDrain(): Promise<void> {while (true) {+ // Wait for snapshots that were started elsewhere (e.g. by rebind) to be queued.+ // Their failures restore the `changed` flag and are handled by the fold below.+ while (this.pendingSnapshots.size > 0) {+ await Promise.allSettled([ ...this.pendingSnapshots ]);+ }+// Fold any pending change into the queue first, so a queued snapshot and a newer// pending change for the same key result in a single save, not two.const snapshotted = this.snapshotPending();