Source

utils/setTimerRecursive.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * Gradually invoking each of the stack of functions with a constant interval
 *
 * @param callStack The stack of functions to call in sequence.
 * @param interval The constant interval between each invocation.
 * @returns A reference value pointed to the titimmer references stack. It is useful to cancel refs.
 * @category utils
 * @module setTimerRecursive
 */
function setTimerRecursive(timer, callStack, interval, setAborter) {
    if (timer === void 0) { timer = 'timeout'; }
    if (callStack === void 0) { callStack = []; }
    if (interval === void 0) { interval = 150; }
    // The buffer stack to store timmer references
    var refs = [];
    // The flag to indicate an abort
    var isAborted = false;
    // Prepare aborter
    var abort = function () { isAborted = true; };
    // Set aborter
    if (setAborter)
        setAborter(abort);
    // Define the recursive function
    var recur = function (callStackIndex) {
        var callback = function () {
            // Check abort
            if (isAborted)
                return;
            // Get the next function
            var func = callStack[callStackIndex];
            // Invoke the next function
            if (func)
                func();
            // Check if it needs to recur
            if (callStackIndex + 1 < callStack.length)
                // Recur next call stack index
                recur(callStackIndex + 1);
        };
        var ref = timer === 'timeout'
            ? setTimeout(callback, interval)
            : requestAnimationFrame(callback);
        // Store id to buffer
        refs.push(ref);
    };
    // Call the recursive function
    recur(0);
    // Return the timer references buffer
    return refs;
}
exports.default = setTimerRecursive;