1 | import { __assign } from './_virtual/_tslib.js';
|
2 |
|
3 | var defaultOptions = {
|
4 | deferEvents: false
|
5 | };
|
6 |
|
7 | var Scheduler =
|
8 |
|
9 |
|
10 |
|
11 | function () {
|
12 | function Scheduler(options) {
|
13 | this.processingEvent = false;
|
14 | this.queue = [];
|
15 | this.initialized = false;
|
16 | this.options = __assign(__assign({}, defaultOptions), options);
|
17 | }
|
18 |
|
19 | Scheduler.prototype.initialize = function (callback) {
|
20 | this.initialized = true;
|
21 |
|
22 | if (callback) {
|
23 | if (!this.options.deferEvents) {
|
24 | this.schedule(callback);
|
25 | return;
|
26 | }
|
27 |
|
28 | this.process(callback);
|
29 | }
|
30 |
|
31 | this.flushEvents();
|
32 | };
|
33 |
|
34 | Scheduler.prototype.schedule = function (task) {
|
35 | if (!this.initialized || this.processingEvent) {
|
36 | this.queue.push(task);
|
37 | return;
|
38 | }
|
39 |
|
40 | if (this.queue.length !== 0) {
|
41 | throw new Error('Event queue should be empty when it is not processing events');
|
42 | }
|
43 |
|
44 | this.process(task);
|
45 | this.flushEvents();
|
46 | };
|
47 |
|
48 | Scheduler.prototype.clear = function () {
|
49 | this.queue = [];
|
50 | };
|
51 |
|
52 | Scheduler.prototype.flushEvents = function () {
|
53 | var nextCallback = this.queue.shift();
|
54 |
|
55 | while (nextCallback) {
|
56 | this.process(nextCallback);
|
57 | nextCallback = this.queue.shift();
|
58 | }
|
59 | };
|
60 |
|
61 | Scheduler.prototype.process = function (callback) {
|
62 | this.processingEvent = true;
|
63 |
|
64 | try {
|
65 | callback();
|
66 | } catch (e) {
|
67 |
|
68 |
|
69 | this.clear();
|
70 | throw e;
|
71 | } finally {
|
72 | this.processingEvent = false;
|
73 | }
|
74 | };
|
75 |
|
76 | return Scheduler;
|
77 | }();
|
78 |
|
79 | export { Scheduler };
|