// tslint:disable:no-any no-unsafe-any no-this-assignment /** * The type of the debounced function. */ export type Debounced
= ((...args: P) => void) & { clear(): void; }; /** * Create a debounced function. * @param action the function to debounce * @param delay the number of milliseconds to delay invoking * @param immediate if true, invoke the function at leading edge, instead of the trailing */ export function debounce
( action: (...args: P) => R, delay: number = 0, immediate: boolean = false, ): Debounced
{ type InternalDebounced = Debounced
; let context: any; let params: P | undefined; let result: R | undefined; let timeoutId: number | undefined; function clear(): void { context = undefined; params = undefined; result = undefined; clearTimeout(timeoutId); timeoutId = undefined; } function invoke(): void { timeoutId = undefined; if (!immediate) { result = action.apply(context, params as P); } } const wrapper: (...args: P) => void = function(this: any, ...args: P): void { params = args; context = this; if (immediate && !timeoutId) { result = action.apply(context, params); } if (timeoutId) { clearTimeout(timeoutId); } timeoutId = setTimeout(invoke, delay) as any; }; (wrapper as InternalDebounced).clear = clear; return wrapper as InternalDebounced; }