MCPcopy
hub / github.com/callumalpass/tasknotes / debounce

Function debounce

src/settings/components/settingHelpers.ts:420–461  ·  view source on GitHub ↗
(
	func: T,
	wait: number,
	immediate = false
)

Source from the content-addressed store, hash-verified

418 * Returns a debounced function with a flush() method to immediately execute pending calls
419 */
420export function debounce<T extends (...args: never[]) => unknown>(
421 func: T,
422 wait: number,
423 immediate = false
424): DebouncedFunction<T> {
425 let timeout: number | undefined;
426 let lastArgs: Parameters<T> | undefined;
427 let lastThis: unknown;
428
429 const debounced = function (this: unknown, ...args: Parameters<T>) {
430 lastArgs = args;
431 // eslint-disable-next-line @typescript-eslint/no-this-alias -- Debounce preserves the caller's this binding for later invocation.
432 lastThis = this;
433
434 const later = () => {
435 timeout = undefined;
436 lastArgs = undefined;
437 if (!immediate) {
438 func.apply(lastThis, args);
439 }
440 };
441
442 const callNow = immediate && !timeout;
443 window.clearTimeout(timeout);
444 timeout = window.setTimeout(later, wait);
445
446 if (callNow) {
447 func.apply(this, args);
448 }
449 } as DebouncedFunction<T>;
450
451 debounced.flush = () => {
452 if (timeout && lastArgs) {
453 window.clearTimeout(timeout);
454 timeout = undefined;
455 func.apply(lastThis, lastArgs);
456 lastArgs = undefined;
457 }
458 };
459
460 return debounced;
461}

Callers 5

TaskNotesSettingTabClass · 0.90
constructorMethod · 0.90
configureTextSettingFunction · 0.70
configureNumberSettingFunction · 0.70

Calls 2

clearTimeoutMethod · 0.65
setTimeoutMethod · 0.65

Tested by

no test coverage detected