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
client/collections: fix an issue
Adorian Doran415d357af6f3
diff --git a/apps/client/src/widgets/collections/view_mode_storage.spec.ts b/apps/client/src/widgets/collections/view_mode_storage.spec.tsindex 7d239a2d48..c4213d2068 100644--- a/apps/client/src/widgets/collections/view_mode_storage.spec.ts+++ b/apps/client/src/widgets/collections/view_mode_storage.spec.ts@@ -73,6 +73,52 @@ describe("ViewModeStorage", () => {expect(server.post).toHaveBeenCalledTimes(1);});+ /**+ * Each write carries the whole config, so two of them in flight can arrive in either order and+ * the earlier one landing last puts back the config it was built before. A board switching its+ * grouping while a column change is still being written would lose that grouping's columns.+ */+ it("sends one write at a time, in the order they were asked for", async () => {+ const storage = new ViewModeStorage<Config>(noteWithStoredConfig(undefined), "geoMap");+ const sent: string[] = [];+ let releaseFirst = () => {};+ vi.mocked(server.post)+ .mockImplementationOnce(async () => {+ await new Promise<void>((resolve) => { releaseFirst = resolve; });+ });++ const first = storage.store({ view: { zoom: 1 } });+ const second = storage.store({ view: { zoom: 2 } });+ // Lets the queue reach the first request, which is held open below.+ await new Promise((resolve) => setTimeout(resolve));++ // The second is held back rather than raced against the first.+ expect(server.post).toHaveBeenCalledTimes(1);++ releaseFirst();+ await Promise.all([ first, second ]);++ expect(server.post).toHaveBeenCalledTimes(2);+ for (const [ , payload ] of vi.mocked(server.post).mock.calls) {+ sent.push((payload as { content: string }).content);+ }+ expect(sent).toEqual([+ JSON.stringify({ view: { zoom: 1 } }),+ JSON.stringify({ view: { zoom: 2 } })+ ]);+ });++ /** One write failing is not a reason to drop the next: the config it carries is still wanted. */+ it("keeps writing after one is refused", async () => {+ const storage = new ViewModeStorage<Config>(noteWithStoredConfig(undefined), "geoMap");+ vi.mocked(server.post).mockRejectedValueOnce(new Error("offline"));++ await expect(storage.store({ view: { zoom: 1 } })).rejects.toThrow("offline");+ await storage.store({ view: { zoom: 2 } });++ expect(server.post).toHaveBeenCalledTimes(2);+ });+it("reports an external change once, and not its own writes", async () => {const stored = JSON.stringify({ view: { zoom: 8 } });const note = noteWithStoredConfig(stored);diff --git a/apps/client/src/widgets/collections/view_mode_storage.ts b/apps/client/src/widgets/collections/view_mode_storage.tsindex ed1a4efd84..c5a248de75 100644--- a/apps/client/src/widgets/collections/view_mode_storage.ts+++ b/apps/client/src/widgets/collections/view_mode_storage.ts@@ -12,6 +12,8 @@ export default class ViewModeStorage<T extends object> {readonly attachmentName: string;/** The serialized content last stored or restored, used to tell our own echoes apart from external changes. */private lastKnownContent?: string;+ /** The write in flight, which the next one is queued behind. See {@link store}. */+ private lastWrite: Promise<unknown> = Promise.resolve();constructor(note: FNote, viewType: ViewModeStorageType) {this.note = note;@@ -28,6 +30,10 @@ export default class ViewModeStorage<T extends object> {* announces it to every client, each of which then fetches the attachment back to see what* changed. Nothing did. Opened in a dozen tabs, a view could raise a dozen such writes and a* fetch of each of them per tab, all for a config nobody touched.+ *+ * Writes are queued rather than sent as they come. Each one carries the whole config, and two+ * requests can arrive in either order: the earlier one landing last would put back the config+ * it was built before, losing whatever the later one added.*/async store(data: T) {const content = JSON.stringify(data);@@ -42,7 +48,14 @@ export default class ViewModeStorage<T extends object> {content,position: 0};- await server.post(`notes/${this.note.noteId}/attachments?matchBy=title`, payload);++ // Caught before the queue is extended, so a write that fails does not hold back the next+ // one; the failure is still reported to whoever asked for this write.+ const url = `notes/${this.note.noteId}/attachments?matchBy=title`;+ this.lastWrite = this.lastWrite+ .catch(() => {})+ .then(() => server.post(url, payload));+ await this.lastWrite;}async restore() {