"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var isFunc_1 = __importDefault(require("../validators/isFunc"));
/**
* Try some synchronous execution until it is successful time by time.
* e.g. Calling React ref instance method.
*
* @param eachTry Each try callback. Return a boolean to indicate whether it is successful or not.
* @param tryTime The number of time to try
* @param interval The interval between each try
* @category utils
* @module tryUntil
*/
function tryUntil(handler, tryTime, interval, callback) {
if (interval === void 0) { interval = 100; }
// Create each try function
var tryEach = function (index, tryTime) {
var isDone = handler(index, tryTime);
if (isDone) {
// Invoke success callback
(0, isFunc_1.default)(callback) && callback(true);
}
else if (index + 1 === tryTime) {
// Invoke failed callback
(0, isFunc_1.default)(callback) && callback(false);
}
else {
// Invoke next try
setTimeout(function () { return tryEach(index + 1, tryTime); }, interval);
}
};
// Invoke recursive function
tryEach(0, tryTime);
}
exports.default = tryUntil;
Source