Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): run a saved search at most once at a time

Elian Doranccb87872718e
diff --git a/apps/client/src/widgets/search_result.spec.tsx b/apps/client/src/widgets/search_result.spec.tsx
index 250873d976..631848a008 100644
--- a/apps/client/src/widgets/search_result.spec.tsx
+++ b/apps/client/src/widgets/search_result.spec.tsx
@@ -194,6 +194,31 @@ describe("running a saved search when it is shown", () => {
expect(container.querySelector("button")).toBeNull();
});
+ it("does not run a search shown again while its first run is pending", async () => {
+ const savedSearch = searchNote("pendingSearch", "#book");
+ let fail: () => void = () => {};
+ const loadSearchNote = vi.spyOn(froca, "loadSearchNote").mockImplementation(() =>
+ new Promise((_, reject) => { fail = () => reject(new Error("Network down")); }));
+ vi.spyOn(toast, "showError").mockImplementation(() => {});
+
+ shownNote.current = savedSearch;
+ await mount();
+ await settle(() => {
+ shownNote.current = buildNote({ id: "otherNote", title: "Other" });
+ shownNote.notify();
+ });
+ await settle(() => {
+ shownNote.current = savedSearch;
+ shownNote.notify();
+ });
+
+ expect(loadSearchNote).toHaveBeenCalledOnce();
+ expect(container.querySelector(".no-items .bx-loader-alt")).not.toBeNull();
+
+ await settle(() => fail());
+ expect(container.querySelector("button")).not.toBeNull();
+ });
+
it("waits for a background tab to be shown before running its saved search", async () => {
const savedSearch = searchNote("backgroundSearch", "#book");
const loadSearchNote = vi.spyOn(froca, "loadSearchNote").mockResolvedValue(undefined);
diff --git a/apps/client/src/widgets/search_result.tsx b/apps/client/src/widgets/search_result.tsx
index 881a1019ac..42f5e1b58b 100644
--- a/apps/client/src/widgets/search_result.tsx
+++ b/apps/client/src/widgets/search_result.tsx
@@ -106,7 +106,8 @@ function useSavedSearchRun(
{ noteContext, ntxId, parentComponent }: SearchRunContext
) {
const hasTabBeenShown = useHasTabBeenShown(noteContext);
- const [ runningNoteId, setRunningNoteId ] = useState<string>();
+ // A note runs at most once at a time, so a finished run removes only its own note.
+ const [ runningNoteIds, setRunningNoteIds ] = useState<ReadonlySet<string>>(new Set());
const autoRunNoteId = useRef<string | undefined>(undefined);
async function runSearch() {
@@ -115,18 +116,22 @@ function useSavedSearchRun(
return;
}
- setRunningNoteId(noteId);
+ setRunningNoteIds((running) => new Set(running).add(noteId));
try {
const result = await search.runSearchNote(parentComponent, noteId, ntxId);
if (result?.error) {
toast.showError(result.error);
}
} finally {
- // Another search can have started in this tab meanwhile, and it keeps its progress.
- setRunningNoteId((current) => (current === noteId ? undefined : current));
+ setRunningNoteIds((running) => {
+ const remaining = new Set(running);
+ remaining.delete(noteId);
+ return remaining;
+ });
}
}
+ const isRunning = !!note && runningNoteIds.has(note.noteId);
const runsOnShow = hasNotRun && hasTabBeenShown && !glob.TRILIUM_SAFE_MODE
&& !!note && autoRunNoteId.current !== note.noteId;
useEffect(() => {
@@ -137,12 +142,15 @@ function useSavedSearchRun(
useEffect(() => {
if (!runsOnShow || !note) return;
+ // A run still in flight from an earlier showing counts as this showing's run.
autoRunNoteId.current = note.noteId;
- void runSearch();
+ if (!isRunning) {
+ void runSearch();
+ }
}, [ runsOnShow, note ]); // eslint-disable-line react-hooks/exhaustive-deps
return {
- isSearching: hasNotRun && (runsOnShow || runningNoteId === note?.noteId),
+ isSearching: hasNotRun && (runsOnShow || isRunning),
runSearch
};
}