import { useCallback, useRef } from 'react'; export interface ThrottleOptions { wait?: number; immediate?: boolean; } export type ThrottleCallback = (...args: T) => void; export function useThrottle( callback: ThrottleCallback, options?: ThrottleOptions ): ThrottleCallback { const timeoutIdRef = useRef(); const argsRef = useRef(); const { wait, immediate } = options ?? {}; const debounceCallback = useCallback( (...cbArgs: T) => { argsRef.current = cbArgs; if (timeoutIdRef.current) { return; } if (immediate) { callback(...cbArgs); } timeoutIdRef.current = window.setTimeout(() => { if (argsRef.current) { callback(...argsRef.current); } argsRef.current = undefined; timeoutIdRef.current = undefined; }, wait); }, [callback, wait, immediate] ); return debounceCallback; }