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 an issue
Adorian Dorancd94400ffe00
diff --git a/apps/client/src/services/options.spec.ts b/apps/client/src/services/options.spec.tsindex e48e293682..c0638695ae 100644--- a/apps/client/src/services/options.spec.ts+++ b/apps/client/src/services/options.spec.ts@@ -123,6 +123,41 @@ describe("Options service", () => {expect(options.get(k("str"))).toBe("fast");});+ it("save() leaves a repeated value alone when an earlier save of it fails after a later one", async () => {+ options.load({ str: "before" });+ let failTheFirst = (_: Error) => {};+ let requests = 0;+ server.put = vi.fn(() => {+ requests++;+ return requests === 1+ ? new Promise((_resolve, reject) => { failTheFirst = reject; })+ : Promise.resolve({});+ }) as unknown as typeof server.put;++ // Both write the same value, so the value alone cannot say which save the cache belongs to.+ const first = options.save(k("str"), "same");+ await options.save(k("str"), "same");+ failTheFirst(new Error("HTTP 500"));++ await expect(first).rejects.toThrow("HTTP 500");+ expect(options.get(k("str"))).toBe("same");+ });++ it("save() leaves a value the server pushed alone when an in-flight save then fails", async () => {+ options.load({ str: "before" });+ let fail = (_: Error) => {};+ server.put = vi.fn(() =>+ new Promise((_resolve, reject) => { fail = reject; })) as unknown as typeof server.put;++ const saving = options.save(k("str"), "mine");+ // What froca_updater does when the server reports the option changed elsewhere.+ options.set(k("str"), "from the server");+ fail(new Error("HTTP 500"));++ await expect(saving).rejects.toThrow("HTTP 500");+ expect(options.get(k("str"))).toBe("from the server");+ });+it("saveMany() PUTs the whole record verbatim", async () => {const put = (server.put = vi.fn(async () => ({})) as typeof server.put);const payload = { a: "1", b: "2" } as unknown as Record<OptionNames, string>;diff --git a/apps/client/src/services/options.ts b/apps/client/src/services/options.tsindex 28ab8ecd49..1d466bceae 100644--- a/apps/client/src/services/options.ts+++ b/apps/client/src/services/options.ts@@ -8,6 +8,8 @@ export type OptionValue = number | string;class Options {initializedPromise: Promise<void>;private arr!: Record<string, OptionValue>;+ /** Which save was the most recent for a key, so a failing one knows whether it still speaks for it. */+ private latestSave: Record<string, number> = {};constructor() {// Don't fetch on the share view, nor on the login / set-password pre-auth screens, where an@@ -73,6 +75,8 @@ class Options {async save(key: OptionNames, value: OptionValue) {const previous = this.arr?.[key];+ const save = (this.latestSave[key] ?? 0) + 1;+ this.latestSave[key] = save;this.set(key, value);const payload: Record<string, OptionValue> = {};@@ -85,9 +89,14 @@ class Options {// back when the server refuses it: a cache that keeps reporting a value which was never// stored makes the failure invisible to whoever asks afterwards.//- // Only while this call's own value is still the one held, so that a save which fails- // slowly cannot undo a later one that has already succeeded.- if (this.arr?.[key] === value) {+ // Only when this is still the newest save for the key *and* what it wrote is still what+ // is held. Either alone is not enough: a later save of the same value would otherwise be+ // undone by this one failing, and so would a value the server pushed here meanwhile.+ //+ // Two saves of one key failing together still leave the cache on the older one's value+ // rather than on what the server holds, which is what it did before any of this and is+ // as far as a single record without a known-persisted value can get.+ if (this.latestSave[key] === save && this.arr?.[key] === value) {this.set(key, previous as OptionValue);}throw e;