interface ThrottleOptions { emitLast?: boolean; } export const throttle = ) => void | Promise>( func: T, delay: number, options: ThrottleOptions = { emitLast: true }, ): ((...args: Parameters) => void) => { let lastCall = 0; let timeout: ReturnType | null = null; let lastArgs: Parameters | null = null; return (...args: Parameters): void => { const now = Date.now(); const remaining = delay - (now - lastCall); const callFunc = () => { lastCall = Date.now(); timeout = null; func(...args); }; if (remaining <= 0) { if (timeout) { clearTimeout(timeout); timeout = null; } callFunc(); } else { lastArgs = args; if (!timeout) { timeout = setTimeout(() => { timeout = null; if (lastArgs && options.emitLast) { func(...(lastArgs as Parameters)); lastArgs = null; } }, remaining); } } }; };