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
perf(autocomplete): hold a search until the one in flight settles
Elian Doran477e3848bd3b
diff --git a/apps/client/src/services/note_autocomplete.spec.ts b/apps/client/src/services/note_autocomplete.spec.tsindex 2e7dfd0cd5..9d8e887f6b 100644--- a/apps/client/src/services/note_autocomplete.spec.ts+++ b/apps/client/src/services/note_autocomplete.spec.ts@@ -5,7 +5,7 @@ import $ from "jquery";const {triggerCommand, getActiveContextNoteId, getActiveContext, chooseNoteType, createNote,- getInboxNotePath, getInboxTarget, translate, getAllCommands, searchCommands+ getInboxNotePath, getInboxTarget, translate, getAllCommands, searchCommands, logError} = vi.hoisted(() => ({triggerCommand: vi.fn(),getActiveContextNoteId: vi.fn<() => string | null>(() => "activeNote"),@@ -20,7 +20,8 @@ const {// keeps the label assertions about which string is chosen rather than about its English.translate: vi.fn((key: string, _opts?: Record<string, unknown>) => key),getAllCommands: vi.fn(() => [] as any[]),- searchCommands: vi.fn(() => [] as any[])+ searchCommands: vi.fn(() => [] as any[]),+ logError: vi.fn()}));vi.mock("../components/app_context.js", () => ({@@ -50,6 +51,18 @@ vi.mock("./command_registry.js", () => ({default: { getAllCommands, searchCommands }}));+// Narrows the blanket ws mock from test/setup.ts to a spy, so what the module reports can be asserted.+vi.mock("./ws.js", () => ({+ default: {+ subscribeToMessages() {},+ async waitForMaxKnownEntityChangeId() {}+ },+ subscribeToMessages() {},+ unsubscribeToMessage() {},+ async waitForMaxKnownEntityChangeId() {},+ logError+}));+// Imports AFTER vi.mock calls.import server from "./server.js";import froca from "./froca.js";@@ -430,6 +443,9 @@ describe("autocompleteSource (via dataset)", () => {});});+// Mirrors SEARCH_DEBOUNCE_MS in note_autocomplete.ts, which is internal.+const DEBOUNCE_MS = 50;+describe("source debounce", () => {beforeEach(() => {vi.clearAllMocks();@@ -495,6 +511,53 @@ describe("source debounce", () => {expect(queries.some((url) => url.includes("query=b2"))).toBe(true);});+ it("keeps one search in flight, so a slow one cannot make the rest queue behind it", async () => {+ const pending: Array<() => void> = [];+ server.get = vi.fn(+ () => new Promise<any[]>((resolve) => pending.push(() => resolve([])))+ ) as typeof server.get;++ const { dataset } = initAndGetSource();+ dataset.source("h", vi.fn());+ expect(server.get).toHaveBeenCalledTimes(1);++ // Each keystroke opens its own burst, so without single-flight every one of them would+ // reach the server while the first search is still running.+ for (const term of ["he", "hel", "hell", "hello"]) {+ await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 10);+ dataset.source(term, vi.fn());+ }+ expect(server.get).toHaveBeenCalledTimes(1);++ pending[0]();+ await vi.runAllTimersAsync();++ // Only the newest term is searched for; the ones typed past are dropped, not queued.+ expect(server.get).toHaveBeenCalledTimes(2);+ expect(server.get).toHaveBeenLastCalledWith(expect.stringContaining("query=hello"));+ });++ it("reports a failed search and keeps accepting the next one", async () => {+ server.get = vi.fn(async () => {+ throw new Error("boom");+ }) as typeof server.get;++ const { dataset } = initAndGetSource();+ dataset.source("h", vi.fn());+ await vi.runAllTimersAsync();++ // The scheduler awaits the search, so a rejection has to be reported here rather than+ // left to surface as an unhandled one.+ expect(logError).toHaveBeenCalledWith(expect.stringContaining("boom"));++ server.get = vi.fn(async () => []) as typeof server.get;+ await vi.advanceTimersByTimeAsync(DEBOUNCE_MS + 10);+ dataset.source("hi", vi.fn());++ // The failure released the slot, rather than wedging the input for good.+ expect(server.get).toHaveBeenCalledTimes(1);+ });+it("debounces and skips the search while composing input", async () => {const { $el, dataset } = initAndGetSource();// simulate IME composition activediff --git a/apps/client/src/services/note_autocomplete.ts b/apps/client/src/services/note_autocomplete.tsindex f2f74cb007..9ec34e9d1c 100644--- a/apps/client/src/services/note_autocomplete.ts+++ b/apps/client/src/services/note_autocomplete.ts@@ -22,16 +22,53 @@ const SELECTED_EXTERNAL_LINK_KEY = "data-external-link";const SEARCH_DEBOUNCE_MS = 50;/**- * Debounces one input's searches: the first keystroke after a pause queries immediately, and a burst- * typed faster than {@link SEARCH_DEBOUNCE_MS} collapses into one search that runs once it stops.- * Each input holds its own timer, so a keystroke in one cannot cancel a search another is waiting on.+ * Paces one input's searches: the first keystroke after a pause queries immediately, a burst typed+ * faster than {@link SEARCH_DEBOUNCE_MS} collapses into one search that runs once it stops, and at+ * most one search is ever outstanding.+ *+ * The single-flight part is what keeps a slow search from compounding. The server answers+ * autocomplete requests one at a time, so firing a second while the first is still running makes+ * every later keystroke wait out the whole queue ahead of it. Holding the newest term back until+ * the outstanding search settles paces requests at whatever the server can actually serve, without+ * the client having to know how slow that is.+ *+ * Each input holds its own timer and its own in-flight search, so a keystroke in one cannot cancel+ * or delay what another is waiting on.*/function createSearchScheduler() {+ type Search = () => void | Promise<void>;+let timeoutId: ReturnType<typeof setTimeout> | undefined;let lastCallAt = 0;+ let running = false;+ let queued: Search | undefined;++ function start(runSearch: Search) {+ // Only the newest term is worth searching for, so a search waiting here replaces the one+ // before it instead of queueing behind it.+ if (running) {+ queued = runSearch;+ return;+ }++ running = true;+ void Promise.resolve(runSearch())+ .catch((e) => logError(`Autocomplete search failed: ${e}`))+ .finally(() => {+ running = false;+ const next = queued;+ queued = undefined;+ if (next) {+ start(next);+ }+ });+ }- return (runSearch: () => void) => {+ return (runSearch: Search) => {+ // A newer keystroke supersedes whatever the previous one left waiting, be that a pending+ // timer or a search held back by the one in flight.clearTimeout(timeoutId);+ queued = undefined;// Measured from the previous keystroke rather than the previous search. Measuring from the// search paces requests at a fixed rate instead of ending the window when typing pauses.@@ -40,9 +77,9 @@ function createSearchScheduler() {lastCallAt = now;if (startsBurst) {- runSearch();+ start(runSearch);} else {- timeoutId = setTimeout(runSearch, SEARCH_DEBOUNCE_MS);+ timeoutId = setTimeout(() => start(runSearch), SEARCH_DEBOUNCE_MS);}};}@@ -390,7 +427,8 @@ function initNoteAutocomplete($el: JQuery<HTMLElement>, options?: Options) {if (isComposingInput) {return;}- autocompleteSource(term, cb, options);+ // Returned so the scheduler holds the next search until this one settles.+ return autocompleteSource(term, cb, options);});},displayKey: "notePathTitle",