import { useCallback, useEffect, useRef } from 'react';
import { unstable_batchedUpdates } from 'react-dom';
import { useEvent } from './event';
export function useIntervalEffect(callback, ms, immediate = false) {
    const callbackEvent = useEvent(() => unstable_batchedUpdates(callback));
    const intervalIdRef = useRef(null);
    const cancel = useCallback(() => {
        if (intervalIdRef.current !== null) {
            clearInterval(intervalIdRef.current);
            intervalIdRef.current = null;
        }
    }, []);
    const restart = useEvent(() => {
        cancel();
        if (immediate) {
            callbackEvent();
        }
        intervalIdRef.current = setInterval(callbackEvent, ms);
    });
    useEffect(() => {
        restart();
        return cancel;
    }, [ms]);
    return [cancel, restart];
}
//# sourceMappingURL=interval-effect.js.map