/** * Lightweight throttle implementation to replace lodash-es throttle. * Ensures the function is called at most once per `wait` milliseconds. * Includes a `cancel()` method for cleanup. */ export interface ThrottledFunction any> { (...args: Parameters): void; cancel: () => void; } export function throttle any>( func: T, wait: number ): ThrottledFunction { let timeoutId: ReturnType | null = null; let lastArgs: Parameters | null = null; let lastCallTime = 0; const throttled = function (this: any, ...args: Parameters) { const now = Date.now(); const remaining = wait - (now - lastCallTime); if (remaining <= 0 || remaining > wait) { if (timeoutId) { clearTimeout(timeoutId); timeoutId = null; } lastCallTime = now; func.apply(this, args); } else if (!timeoutId) { lastArgs = args; timeoutId = setTimeout(() => { lastCallTime = Date.now(); timeoutId = null; if (lastArgs) { func.apply(this, lastArgs); lastArgs = null; } }, remaining); } } as ThrottledFunction; throttled.cancel = () => { if (timeoutId) { clearTimeout(timeoutId); timeoutId = null; } lastArgs = null; lastCallTime = 0; }; return throttled; }