var FrozenTaskManager = /** @class */ (function () {
    function FrozenTaskManager() {
        var _this = this;
        this.tasks = [];
        this.isDestroy = false;
        this.isScheduling = false;
        this.schedule = function () {
            _this.isScheduling = true;
            var task = _this.tasks.shift();
            if (task && !_this.isDestroy) {
                task.status = "running";
                task.fn.apply(null).then(function () {
                    if (_this.tasks.length > 0) {
                        setTimeout(_this.schedule);
                    }
                    else {
                        _this.isScheduling = false;
                    }
                }).catch(function () {
                    if (_this.tasks.length > 0) {
                        setTimeout(_this.schedule);
                    }
                    else {
                        _this.isScheduling = false;
                    }
                });
            }
        };
    }
    FrozenTaskManager.prototype.getRunningTask = function () {
        if (this.tasks[0] && this.tasks[0].status === "running") {
            return this.tasks[0];
        }
        return null;
    };
    FrozenTaskManager.prototype.addTask = function (type, fn) {
        var task = {
            type: type,
            status: "wait",
            fn: fn,
        };
        var runningTask = this.getRunningTask();
        if (runningTask && type === runningTask.type) {
            return;
        }
        else {
            this.tasks = this.tasks.filter(function (v) { return v.type !== type; });
            this.tasks.push(task);
            if (!this.isScheduling) {
                this.schedule();
            }
        }
    };
    FrozenTaskManager.prototype.destroy = function () {
        this.isDestroy = true;
    };
    return FrozenTaskManager;
}());
export { FrozenTaskManager };
