/** * Utility functions for the application */ /** * Debounce helper to prevent duplicate function calls * @param func - The function to debounce * @param delay - The delay in milliseconds * @returns A debounced version of the function with a cleanup method */ export const debounce = void>( func: T, delay: number ): (((...args: Parameters) => void) & { cancel: () => void }) => { let timeoutId: NodeJS.Timeout | undefined; const debounced = (...args: Parameters) => { if (timeoutId !== undefined) { clearTimeout(timeoutId); } timeoutId = setTimeout(() => func(...args), delay); }; // Add cancel method to clear pending timeouts debounced.cancel = () => { if (timeoutId !== undefined) { clearTimeout(timeoutId); timeoutId = undefined; } }; return debounced; };