import { useCallback, useEffect, useRef } from "react";
function useScheduledCallback(callback) {
    const rafIdRef = useRef(null);
    const callbackRef = useRef(callback);
    useEffect(()=>{
        callbackRef.current = callback;
    }, [
        callback
    ]);
    useEffect(()=>()=>{
            if (null !== rafIdRef.current) cancelAnimationFrame(rafIdRef.current);
        }, []);
    return useCallback(()=>{
        if (null !== rafIdRef.current) cancelAnimationFrame(rafIdRef.current);
        rafIdRef.current = requestAnimationFrame(()=>{
            callbackRef.current();
            rafIdRef.current = null;
        });
    }, []);
}
export { useScheduledCallback };
