"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TaskQueue = void 0;
var TaskQueue = (function () {
    function TaskQueue() {
        this.tasks = [];
        this.stopOnError = true;
    }
    TaskQueue.prototype.add = function (task) {
        this.tasks.push(task);
    };
    TaskQueue.prototype.addSync = function (task) {
        this.add(function (cb) {
            try {
                task();
                cb();
            }
            catch (err) {
                cb(err);
                return;
            }
        });
    };
    TaskQueue.prototype.runAll = function (cb) {
        var _this = this;
        var taskList = this.tasks;
        this.tasks = [];
        var index = -1;
        var runNext = function () {
            index++;
            if (index >= taskList.length) {
                if (cb)
                    cb();
                return;
            }
            try {
                taskList[index](function (err) {
                    if (err) {
                        if (cb)
                            cb(err);
                        if (_this.stopOnError)
                            return;
                    }
                    runNext();
                });
            }
            catch (err) {
                if (cb)
                    cb(err);
            }
        };
        runNext();
    };
    return TaskQueue;
}());
exports.TaskQueue = TaskQueue;
