import { useMemo } from 'react';
export function runInNextFrame(callback) {
    var rafId = requestAnimationFrame(function () {
        rafId = requestAnimationFrame(function () {
            callback();
            rafId = null;
        });
    });
    return function () {
        if (rafId) {
            cancelAnimationFrame(rafId);
            rafId = null;
        }
    };
}
export function runOnceInNextFrame(callback) {
    var ticking = false;
    var savedArgs;
    var cancel = null;
    var fn = function () {
        var args = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            args[_i] = arguments[_i];
        }
        savedArgs = args;
        if (!ticking) {
            cancel = runInNextFrame(function () {
                callback.apply(void 0, savedArgs);
                cancel = null;
                ticking = false;
            });
            ticking = true;
        }
    };
    fn.cancel = function () {
        cancel === null || cancel === void 0 ? void 0 : cancel();
    };
    return fn;
}
export var useRunOnceInNextFrame = function (cb, disabled) {
    return useMemo(function () { return (!disabled ? runOnceInNextFrame(cb) : cb); }, [cb, disabled]);
};
