import { TAnyFunction } from '../typeHelpers' /** * ### debounce(func, wait) * * Create a debounced function that delays invoking `func` until `wait` milliseconds * have elapsed since the last time the debounced function was invoked. * * ```js * const func = () => console.log('Heavy processing happening') * const debouncedFunc = flocky.debounce(func, 250) * ``` */ type FunctionWithVoidReturn> = (...args: Parameters) => void export function debounce>( func: TFunc, wait: number ): FunctionWithVoidReturn { let timeoutID: NodeJS.Timeout | null = null return function (this: unknown, ...args: Array) { if (timeoutID) clearTimeout(timeoutID) timeoutID = setTimeout(() => func.apply(this, args), wait) } as FunctionWithVoidReturn }