12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- 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 };
|