mirror of
https://git.kescher.at/CatCatNya/catstodon.git
synced 2024-11-25 22:31:36 +01:00
dcfd39991b
Port b728c0e8ce
to glitch-soc
Signed-off-by: Claire <claire.github-309c@sitedethib.com>
44 lines
1,018 B
TypeScript
44 lines
1,018 B
TypeScript
import { useRef, useCallback, useEffect } from 'react';
|
|
|
|
export const useTimeout = () => {
|
|
const timeoutRef = useRef<ReturnType<typeof setTimeout>>();
|
|
const callbackRef = useRef<() => void>();
|
|
|
|
const set = useCallback((callback: () => void, delay: number) => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
}
|
|
|
|
callbackRef.current = callback;
|
|
timeoutRef.current = setTimeout(callback, delay);
|
|
}, []);
|
|
|
|
const delay = useCallback((delay: number) => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
}
|
|
|
|
if (!callbackRef.current) {
|
|
return;
|
|
}
|
|
|
|
timeoutRef.current = setTimeout(callbackRef.current, delay);
|
|
}, []);
|
|
|
|
const cancel = useCallback(() => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
timeoutRef.current = undefined;
|
|
callbackRef.current = undefined;
|
|
}
|
|
}, []);
|
|
|
|
useEffect(
|
|
() => () => {
|
|
cancel();
|
|
},
|
|
[cancel],
|
|
);
|
|
|
|
return [set, cancel, delay] as const;
|
|
};
|