better-chatbot / src /hooks /use-debounce.ts
Bot
Initial commit for HF Spaces
05c5ed5
Raw
History Blame Contribute Delete
693 Bytes
import { useCallback, useRef } from "react";
/**
* Custom hook for debouncing function calls
* @param callback - Function to debounce
* @param delay - Delay in milliseconds
* @returns Debounced function
*/
export function useDebounce<T extends (...args: any[]) => any>(
callback: T,
delay: number,
): T {
const timeoutRef = useRef<NodeJS.Timeout | null>(null);
const debouncedCallback = useCallback(
(...args: Parameters<T>) => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
timeoutRef.current = setTimeout(() => {
callback(...args);
}, delay);
},
[callback, delay],
) as T;
return debouncedCallback;
}