From 84e36d34d4fcc141af77800867eb9b18566f6e6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Efe=20G=C3=B6kdemir?= Date: Fri, 13 Nov 2026 02:06:05 +0300 Subject: [PATCH] Fix dropped theme language server checks --- .changeset/brave-otters-dance.md | 5 ++ .../src/server/startServer.ts | 1 + .../src/utils/debounce.spec.ts | 57 ++++++++++++++ .../src/utils/debounce.ts | 77 +++++++++++++------ 4 files changed, 118 insertions(+), 22 deletions(-) create mode 100644 .changeset/brave-otters-dance.md create mode 100644 packages/theme-language-server-common/src/utils/debounce.spec.ts diff --git a/.changeset/brave-otters-dance.md b/.changeset/brave-otters-dance.md new file mode 100644 index 000000000..0b59a1501 --- /dev/null +++ b/.changeset/brave-otters-dance.md @@ -0,0 +1,5 @@ +--- +"@shopify/theme-language-server-common": patch +--- + +Queue debounced theme checks so triggers are not dropped and asynchronous runs do not overlap. diff --git a/packages/theme-language-server-common/src/server/startServer.ts b/packages/theme-language-server-common/src/server/startServer.ts index 00026e797..bdc32b73e 100644 --- a/packages/theme-language-server-common/src/server/startServer.ts +++ b/packages/theme-language-server-common/src/server/startServer.ts @@ -191,6 +191,7 @@ export function startServer( themeGraphManager, }), 100, + (previous, next): [string[]] => [[...new Set([...previous[0], ...next[0]])]], ); const getTranslationsForURI: GetTranslationsForURI = async (uri) => { diff --git a/packages/theme-language-server-common/src/utils/debounce.spec.ts b/packages/theme-language-server-common/src/utils/debounce.spec.ts new file mode 100644 index 000000000..0d165ec55 --- /dev/null +++ b/packages/theme-language-server-common/src/utils/debounce.spec.ts @@ -0,0 +1,57 @@ +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { debounce } from './debounce'; + +const mergeValues = (previous: [string[]], next: [string[]]): [string[]] => [ + [...new Set([...previous[0], ...next[0]])], +]; + +describe('debounce', () => { + afterEach(() => { + vi.useRealTimers(); + }); + + it('merges calls made before a forced run', async () => { + vi.useFakeTimers(); + const run = vi.fn<(values: string[]) => void>(); + const debouncedRun = debounce(run, 100, mergeValues); + + debouncedRun(['first']); + debouncedRun.force(['second']); + debouncedRun(['third']); + + await vi.advanceTimersByTimeAsync(100); + + expect(run).toHaveBeenCalledOnce(); + expect(run).toHaveBeenCalledWith(['first', 'second', 'third']); + }); + + it('queues calls made while an asynchronous invocation is running', async () => { + vi.useFakeTimers(); + let finishFirstRun!: () => void; + const firstRun = new Promise((resolve) => { + finishFirstRun = resolve; + }); + const run = vi + .fn<(values: string[]) => Promise>() + .mockImplementationOnce(() => firstRun) + .mockResolvedValue(undefined); + const debouncedRun = debounce(run, 100, mergeValues); + + debouncedRun(['first']); + await vi.advanceTimersByTimeAsync(100); + expect(run).toHaveBeenCalledOnce(); + + debouncedRun(['during-run']); + debouncedRun.force(['forced-during-run']); + await vi.advanceTimersByTimeAsync(500); + expect(run).toHaveBeenCalledOnce(); + + finishFirstRun(); + await Promise.resolve(); + await Promise.resolve(); + await vi.advanceTimersByTimeAsync(100); + + expect(run).toHaveBeenCalledTimes(2); + expect(run).toHaveBeenLastCalledWith(['during-run', 'forced-during-run']); + }); +}); diff --git a/packages/theme-language-server-common/src/utils/debounce.ts b/packages/theme-language-server-common/src/utils/debounce.ts index 60962636a..f80429299 100644 --- a/packages/theme-language-server-common/src/utils/debounce.ts +++ b/packages/theme-language-server-common/src/utils/debounce.ts @@ -2,17 +2,14 @@ import { ArgumentTypes } from '@shopify/theme-check-common'; export interface DebouncedFunction { /** - * A function that will execute on the trailing edge of a timer with the - * last arguments it was called with (unless forced) + * A function that executes after the debounce interval. Calls made while a + * previous invocation is running are queued for a later invocation. */ (...args: ArgumentTypes): void; /** - * Debounced version of a function but making sure that it is called with - * this specific set of arguments. - * - * Subsequent calls will be ignored until the timer has expired and the - * function has executed. + * Mark this call as forced. Later calls may be queued, but do not replace + * this call while it is waiting for the timer. */ force(...args: ArgumentTypes): void; } @@ -20,9 +17,8 @@ export interface DebouncedFunction { /** * debounce(fn, ms) * - * A debounced function only executes once after a timer has expired. Repeated - * call to the debounced function before its timer has expired result in a delayed - * execution of the function. + * Calls made before the timer expires are debounced. Calls made while the + * function is running are queued and run after the current invocation finishes. * * This is useful in cases where you have an "expensive" function that you only want * to execute after the user is idle for a little bit. @@ -35,26 +31,63 @@ export interface DebouncedFunction { * * @param fn a function that should be debounced * @param ms milliseconds after last function call for it to execute - * @returns a function that will execute on the trailing edge of a timer with the last argument it was called with + * @param mergeArguments how to combine pending arguments. By default, the + * arguments from the latest call are used. + * @returns a debounced function */ -export function debounce(fn: F, ms?: number): DebouncedFunction { +export function debounce( + fn: F, + ms?: number, + mergeArguments: (previous: ArgumentTypes, next: ArgumentTypes) => ArgumentTypes = ( + _previous, + next, + ) => next, +): DebouncedFunction { let timeoutId: ReturnType | null = null; - let force = false; // force use a certain set of arguments in the next call + let pendingArgs: ArgumentTypes | null = null; + let running = false; + let forcePending = false; - const debouncedFn = (...args: ArgumentTypes): void => { - if (timeoutId !== null && force) return; - if (timeoutId !== null) clearTimeout(timeoutId); - timeoutId = setTimeout(async () => { - await Promise.resolve(fn(...args)); + const schedule = () => { + if (running || timeoutId !== null || pendingArgs === null) return; + + timeoutId = setTimeout(() => { timeoutId = null; - force = false; + const args = pendingArgs; + pendingArgs = null; + forcePending = false; + if (args === null) return; + + running = true; + Promise.resolve() + .then(() => fn(...args)) + .finally(() => { + running = false; + schedule(); + }); }, ms); }; - debouncedFn.force = (...args: ArgumentTypes): void => { - debouncedFn(...args); - force = true; + const enqueue = (args: ArgumentTypes, force: boolean) => { + pendingArgs = pendingArgs === null ? args : mergeArguments(pendingArgs, args); + + if (running) { + if (force) forcePending = true; + return; + } + + if (timeoutId !== null) { + if (forcePending && !force) return; + clearTimeout(timeoutId); + timeoutId = null; + } + + schedule(); + if (force) forcePending = true; }; + const debouncedFn = (...args: ArgumentTypes): void => enqueue(args, false); + debouncedFn.force = (...args: ArgumentTypes): void => enqueue(args, true); + return debouncedFn; }