react-live-chat-loader
Version:
Implement live chat in your react app without taking a performance hit.
31 lines (30 loc) • 3.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var requestIdleCallback = typeof window !== 'undefined' ? window.requestIdleCallback : null;
var waitForLoad = function waitForLoad(check, callback) {
var elapsedTime = 0;
// If the provider fails to load we don't want to keep checking continuously
// therefore we set a max duration we're willing to wait before executing the callback
var maxWaitDuration = 10000;
// If the browser does not support requestIdleCallback we'll wait the fallback duration
// before executing the callback
var fallbackDuration = 1000;
var _scheduleLoad = function scheduleLoad(deadline) {
if (check() || elapsedTime >= maxWaitDuration) {
callback();
return;
}
elapsedTime += deadline.timeRemaining();
requestIdleCallback === null || requestIdleCallback === void 0 || requestIdleCallback(_scheduleLoad);
};
if (requestIdleCallback) {
requestIdleCallback(_scheduleLoad);
} else {
setTimeout(callback, fallbackDuration);
}
};
var _default = exports.default = waitForLoad;
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJyZXF1ZXN0SWRsZUNhbGxiYWNrIiwid2luZG93Iiwid2FpdEZvckxvYWQiLCJjaGVjayIsImNhbGxiYWNrIiwiZWxhcHNlZFRpbWUiLCJtYXhXYWl0RHVyYXRpb24iLCJmYWxsYmFja0R1cmF0aW9uIiwic2NoZWR1bGVMb2FkIiwiZGVhZGxpbmUiLCJ0aW1lUmVtYWluaW5nIiwic2V0VGltZW91dCIsIl9kZWZhdWx0IiwiZXhwb3J0cyIsImRlZmF1bHQiXSwic291cmNlcyI6WyIuLi8uLi9zcmMvdXRpbHMvd2FpdEZvckxvYWQudHMiXSwic291cmNlc0NvbnRlbnQiOlsiY29uc3QgcmVxdWVzdElkbGVDYWxsYmFjayA9XG4gIHR5cGVvZiB3aW5kb3cgIT09ICd1bmRlZmluZWQnID8gd2luZG93LnJlcXVlc3RJZGxlQ2FsbGJhY2sgOiBudWxsXG5cbmNvbnN0IHdhaXRGb3JMb2FkID0gKGN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