import { __assign } from './_virtual/_tslib.js'; var defaultOptions = { deferEvents: false }; var Scheduler = /*#__PURE__*/ /** @class */ function () { function Scheduler(options) { this.processingEvent = false; this.queue = []; this.initialized = false; this.options = __assign(__assign({}, defaultOptions), options); } Scheduler.prototype.initialize = function (callback) { this.initialized = true; if (callback) { if (!this.options.deferEvents) { this.schedule(callback); return; } this.process(callback); } this.flushEvents(); }; Scheduler.prototype.schedule = function (task) { if (!this.initialized || this.processingEvent) { this.queue.push(task); return; } if (this.queue.length !== 0) { throw new Error('Event queue should be empty when it is not processing events'); } this.process(task); this.flushEvents(); }; Scheduler.prototype.clear = function () { this.queue = []; }; Scheduler.prototype.flushEvents = function () { var nextCallback = this.queue.shift(); while (nextCallback) { this.process(nextCallback); nextCallback = this.queue.shift(); } }; Scheduler.prototype.process = function (callback) { this.processingEvent = true; try { callback(); } catch (e) { // there is no use to keep the future events // as the situation is not anymore the same this.clear(); throw e; } finally { this.processingEvent = false; } }; return Scheduler; }(); export { Scheduler };