mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-01-27 19:16:32 +01:00
23 lines
489 B
JavaScript
23 lines
489 B
JavaScript
import { useCallback, useRef } from 'preact/hooks';
|
|
|
|
export default function useDebouncedCallback(
|
|
callback,
|
|
delay,
|
|
dependencies = [],
|
|
) {
|
|
const timeout = useRef();
|
|
|
|
const comboDeps = dependencies
|
|
? [callback, delay, ...dependencies]
|
|
: [callback, delay];
|
|
|
|
return useCallback((...args) => {
|
|
if (timeout.current != null) {
|
|
clearTimeout(timeout.current);
|
|
}
|
|
|
|
timeout.current = setTimeout(() => {
|
|
callback(...args);
|
|
}, delay);
|
|
}, comboDeps);
|
|
}
|