Skip to content

Build your personal knowledge base with Trilium Notes

client/collections: fix an issue

Adorian Doranfd9f2a5975fd
diff --git a/apps/client/src/widgets/collections/view_mode_storage.spec.ts b/apps/client/src/widgets/collections/view_mode_storage.spec.ts
index c4213d2068..a7073f5650 100644
--- a/apps/client/src/widgets/collections/view_mode_storage.spec.ts
+++ b/apps/client/src/widgets/collections/view_mode_storage.spec.ts
@@ -108,6 +108,41 @@ describe("ViewModeStorage", () => {
]);
});
+ /**
+ * A queued write announces itself while a later one is still waiting to be sent, so what comes
+ * back is what the view wrote a moment ago rather than what it now holds. Taken for an external
+ * change, it would put the view back to that older config, and the next change made from there
+ * would drop whatever the queued write carried.
+ */
+ it("passes over the echo of its own write that a later one has moved past", async () => {
+ // Carrying an attachment already, which is what the echoes below are read from.
+ const note = noteWithStoredConfig(JSON.stringify({ view: { zoom: 0 } }));
+ const storage = new ViewModeStorage<Config>(note, "geoMap");
+ 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 } });
+ await new Promise((resolve) => setTimeout(resolve));
+
+ // The first write has landed and announced itself; the second has not been sent yet.
+ vi.mocked(server.get).mockResolvedValue({ content: JSON.stringify({ view: { zoom: 1 } }) });
+ expect(await storage.restoreIfChanged()).toBeUndefined();
+
+ releaseFirst();
+ await Promise.all([ first, second ]);
+
+ // ...and the second write echoes back as its own too.
+ vi.mocked(server.get).mockResolvedValue({ content: JSON.stringify({ view: { zoom: 2 } }) });
+ expect(await storage.restoreIfChanged()).toBeUndefined();
+
+ // A change from anywhere else is still reported.
+ vi.mocked(server.get).mockResolvedValue({ content: JSON.stringify({ view: { zoom: 9 } }) });
+ expect(await storage.restoreIfChanged()).toEqual({ view: { zoom: 9 } });
+ });
+
/** 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");
diff --git a/apps/client/src/widgets/collections/view_mode_storage.ts b/apps/client/src/widgets/collections/view_mode_storage.ts
index c5a248de75..c9a3c99544 100644
--- a/apps/client/src/widgets/collections/view_mode_storage.ts
+++ b/apps/client/src/widgets/collections/view_mode_storage.ts
@@ -14,6 +14,14 @@ export default class ViewModeStorage<T extends object> {
private lastKnownContent?: string;
/** The write in flight, which the next one is queued behind. See {@link store}. */
private lastWrite: Promise<unknown> = Promise.resolve();
+ /**
+ * What this view has written and not yet heard back about, which its own echoes carry.
+ *
+ * `lastKnownContent` alone cannot recognise them: a queued write announces itself while a later
+ * one is still waiting to be sent, so what comes back is what this view wrote a moment ago
+ * rather than what it now holds. See {@link restoreIfChanged}.
+ */
+ private ownWrites = new Set<string>();
constructor(note: FNote, viewType: ViewModeStorageType) {
this.note = note;
@@ -49,13 +57,22 @@ export default class ViewModeStorage<T extends object> {
position: 0
};
+ this.ownWrites.add(content);
+
// 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;
+
+ try {
+ await this.lastWrite;
+ } catch (e) {
+ // Nothing was stored, so nothing will echo back.
+ this.ownWrites.delete(content);
+ throw e;
+ }
}
async restore() {
@@ -71,12 +88,26 @@ export default class ViewModeStorage<T extends object> {
* Like {@link restore}, but resolves to `undefined` if the stored content matches what was last
* stored or restored, so that callers only react to genuinely external changes (e.g. the same
* view opened in another split, or synced from another instance).
+ *
+ * A write of this view's own that a later one has already moved past is also passed over.
+ * Handing it back would take the view to the config it held before that later write, and a
+ * change made from there would be built on it, dropping what the later write carried.
*/
async restoreIfChanged() {
const content = await this.fetchContent();
- if (content === undefined || content === this.lastKnownContent) {
+ if (content === undefined) {
return undefined;
}
+
+ if (content === this.lastKnownContent) {
+ this.ownWrites.delete(content);
+ return undefined;
+ }
+
+ if (this.ownWrites.delete(content)) {
+ return undefined;
+ }
+
this.lastKnownContent = content;
return JSON.parse(content) as T;
}