Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): stop note chips re-requesting a missing note in a loop

Claudec85411f80138
diff --git a/apps/client/src/services/froca.spec.ts b/apps/client/src/services/froca.spec.ts
index 5078fb65a1..f86d9fdd3b 100644
--- a/apps/client/src/services/froca.spec.ts
+++ b/apps/client/src/services/froca.spec.ts
@@ -405,6 +405,9 @@ describe("getNotes / getNote / noteExists / getNoteFromCache", () => {
const note = buildNote({ id: "gn-real", title: "Real" });
await expect(froca.getNote(note.noteId)).resolves.toBe(note);
+
+ server.post = vi.fn(async () => ({ notes: [], branches: [], attributes: [] })) as typeof server.post;
+ await expect(froca.getNote("gn-missing", true)).resolves.toBeNull();
});
it("getNoteFromCache throws on empty id and returns the cached note otherwise", () => {
diff --git a/apps/client/src/services/froca.ts b/apps/client/src/services/froca.ts
index 3be6204f53..8fad9a40c5 100644
--- a/apps/client/src/services/froca.ts
+++ b/apps/client/src/services/froca.ts
@@ -298,7 +298,7 @@ class FrocaImpl implements Froca {
return null;
}
- return (await this.getNotes([noteId], silentNotFoundError))[0];
+ return (await this.getNotes([noteId], silentNotFoundError))[0] ?? null;
}
getNoteFromCache(noteId: string): FNote | undefined {
diff --git a/packages/codemirror/src/extensions/trilium_note_chips.spec.ts b/packages/codemirror/src/extensions/trilium_note_chips.spec.ts
index b0e641e64b..708853dd9b 100644
--- a/packages/codemirror/src/extensions/trilium_note_chips.spec.ts
+++ b/packages/codemirror/src/extensions/trilium_note_chips.spec.ts
@@ -88,6 +88,17 @@ describe("triliumNoteChips", () => {
expect(resolve).toHaveBeenCalledTimes(1);
});
+ it("asks only once for a missing note whose lookup settles to undefined", async () => {
+ const redrawn = vi.fn();
+ const resolve = vi.fn(() => Promise.resolve(undefined)) as unknown as NoteChipResolver;
+ const parent = build("~author.noteId = gone", resolve, [ EditorView.updateListener.of(redrawn) ]);
+
+ await vi.waitFor(() => expect(redrawn).toHaveBeenCalled());
+ await new Promise((settle) => setTimeout(settle, 20));
+ expect(resolve).toHaveBeenCalledTimes(1);
+ expect(parent.textContent).toContain("gone");
+ });
+
it("keeps a drawn chip across an edit elsewhere in the query", () => {
const parent = build("~author.noteId = abc123", () => TOLKIEN);
const chip = parent.querySelector(".cm-note-chip");
diff --git a/packages/codemirror/src/extensions/trilium_note_chips.ts b/packages/codemirror/src/extensions/trilium_note_chips.ts
index 96e9738efd..7ad21300e1 100644
--- a/packages/codemirror/src/extensions/trilium_note_chips.ts
+++ b/packages/codemirror/src/extensions/trilium_note_chips.ts
@@ -132,7 +132,7 @@ class NoteChipsPlugin {
const answer = this.resolve(noteId);
if (!(answer instanceof Promise)) {
- this.resolved.set(noteId, answer);
+ this.resolved.set(noteId, answer ?? null);
return answer;
}
@@ -141,7 +141,8 @@ class NoteChipsPlugin {
.catch(() => null)
.then((chip) => {
this.pending.delete(noteId);
- this.resolved.set(noteId, chip);
+ // `undefined` would read as never asked, and the dispatch below would ask again.
+ this.resolved.set(noteId, chip ?? null);
// Outside the update that asked for it, so dispatching here is safe.
this.view.dispatch({ effects: noteChipResolved.of() });
});