// https://levelup.gitconnected.com/debounce-in-javascript-improve-your-applications-performance-5b01855e086 // blue_modules/debounce.ts type DebouncedFunction void> = { (this: ThisParameterType, ...args: Parameters): void; cancel(): void; }; const debounce = void>(func: T, wait: number): DebouncedFunction => { let timeout: NodeJS.Timeout | null; const debouncedFunction = function (this: ThisParameterType, ...args: Parameters) { const later = () => { timeout = null; func.apply(this, args); }; if (timeout) { clearTimeout(timeout); } timeout = setTimeout(later, wait); }; debouncedFunction.cancel = () => { if (timeout) { clearTimeout(timeout); } timeout = null; }; return debouncedFunction as DebouncedFunction; }; export default debounce;