(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.AtomDispatcher = void 0;
    class AtomDispatcher {
        constructor() {
            this.head = null;
            this.tail = null;
        }
        onTimeout() {
            if (this.paused) {
                return;
            }
            if (!this.head) {
                return;
            }
            const item = this.head;
            this.head = item.next;
            item.next = null;
            if (!this.head) {
                this.tail = null;
            }
            item();
            setTimeout(() => {
                this.onTimeout();
            }, 1);
        }
        pause() {
            this.paused = true;
        }
        start() {
            this.paused = false;
            setTimeout(() => {
                this.onTimeout();
            }, 1);
        }
        callLater(f) {
            if (this.tail) {
                this.tail.next = f;
                this.tail = f;
            }
            else {
                this.head = f;
                this.tail = f;
            }
            if (!this.paused) {
                this.start();
            }
        }
        waitForAll() {
            return new Promise((resolve, reject) => {
                this.callLater(() => {
                    resolve();
                });
            });
        }
    }
    exports.AtomDispatcher = AtomDispatcher;
});
//# sourceMappingURL=AtomDispatcher.js.map