1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var ImportSequencer = (function () {
|
4 | function ImportSequencer(onSequencerEmpty) {
|
5 | this.imports = [];
|
6 | this.variableImports = [];
|
7 | this._onSequencerEmpty = onSequencerEmpty;
|
8 | this._currentDepth = 0;
|
9 | }
|
10 | ImportSequencer.prototype.addImport = function (callback) {
|
11 | var importSequencer = this, importItem = {
|
12 | callback: callback,
|
13 | args: null,
|
14 | isReady: false
|
15 | };
|
16 | this.imports.push(importItem);
|
17 | return function () {
|
18 | importItem.args = Array.prototype.slice.call(arguments, 0);
|
19 | importItem.isReady = true;
|
20 | importSequencer.tryRun();
|
21 | };
|
22 | };
|
23 | ImportSequencer.prototype.addVariableImport = function (callback) {
|
24 | this.variableImports.push(callback);
|
25 | };
|
26 | ImportSequencer.prototype.tryRun = function () {
|
27 | this._currentDepth++;
|
28 | try {
|
29 | while (true) {
|
30 | while (this.imports.length > 0) {
|
31 | var importItem = this.imports[0];
|
32 | if (!importItem.isReady) {
|
33 | return;
|
34 | }
|
35 | this.imports = this.imports.slice(1);
|
36 | importItem.callback.apply(null, importItem.args);
|
37 | }
|
38 | if (this.variableImports.length === 0) {
|
39 | break;
|
40 | }
|
41 | var variableImport = this.variableImports[0];
|
42 | this.variableImports = this.variableImports.slice(1);
|
43 | variableImport();
|
44 | }
|
45 | }
|
46 | finally {
|
47 | this._currentDepth--;
|
48 | }
|
49 | if (this._currentDepth === 0 && this._onSequencerEmpty) {
|
50 | this._onSequencerEmpty();
|
51 | }
|
52 | };
|
53 | return ImportSequencer;
|
54 | }());
|
55 | exports.default = ImportSequencer;
|
56 |
|
\ | No newline at end of file |