mirror of
https://github.com/pterodactyl/panel.git
synced 2026-11-30 11:25:52 -05:00
53 lines
1.4 KiB
TypeScript
53 lines
1.4 KiB
TypeScript
import { useEffect, useMemo, useRef } from 'react';
|
|
|
|
export type DebouncedCallback<TArgs extends unknown[]> = ((...args: TArgs) => void) & {
|
|
cancel: () => void;
|
|
flush: () => void;
|
|
};
|
|
|
|
/** A pending call still runs when the component unmounts. */
|
|
export const useDebouncedCallback = <TArgs extends unknown[]>(
|
|
callback: (...args: TArgs) => void,
|
|
delay: number
|
|
): DebouncedCallback<TArgs> => {
|
|
const callbackRef = useRef(callback);
|
|
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
const pendingRef = useRef<TArgs | null>(null);
|
|
|
|
useEffect(() => {
|
|
callbackRef.current = callback;
|
|
}, [callback]);
|
|
|
|
const debounced = useMemo(() => {
|
|
const cancel = () => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
timeoutRef.current = null;
|
|
}
|
|
|
|
pendingRef.current = null;
|
|
};
|
|
|
|
const flush = () => {
|
|
const args = pendingRef.current;
|
|
|
|
cancel();
|
|
if (args) {
|
|
callbackRef.current(...args);
|
|
}
|
|
};
|
|
|
|
const call = (...args: TArgs) => {
|
|
cancel();
|
|
pendingRef.current = args;
|
|
timeoutRef.current = setTimeout(flush, delay);
|
|
};
|
|
|
|
return Object.assign(call, { cancel, flush });
|
|
}, [delay]);
|
|
|
|
useEffect(() => debounced.flush, [debounced]);
|
|
|
|
return debounced;
|
|
};
|