docs(docs-infra): avoid persistent AbortSignal listener in wait debounce in search

Cleans up the abort listener once the timeout resolves to prevent it from
remaining attached longer than necessary.

(cherry picked from commit 94b707957a)
This commit is contained in:
SkyZeroZx 2026-01-09 12:17:25 -05:00 committed by Jessica Janiuk
parent a63f6d0978
commit a6efb7752d

View file

@ -228,16 +228,19 @@ function matched(snippet: SnippetResult | undefined): boolean {
*/
function wait(ms: number, signal: AbortSignal): Promise<void> {
return new Promise<void>((resolve, reject) => {
const timeout = setTimeout(() => resolve(), ms);
let timeout: ReturnType<typeof setTimeout> | undefined;
signal.addEventListener(
'abort',
() => {
clearTimeout(timeout);
reject(new Error('Operation aborted'));
},
{once: true},
);
const onAbort = () => {
clearTimeout(timeout);
reject(new Error('Operation aborted'));
};
timeout = setTimeout(() => {
signal.removeEventListener('abort', onAbort);
resolve();
}, ms);
signal.addEventListener('abort', onAbort, {once: true});
});
}