ascor
Version:
一些常用的简单的js工具
138 lines (137 loc) • 5.05 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __spreadArrays = (this && this.__spreadArrays) || function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Queue = void 0;
var EventEmitter_1 = require("./EventEmitter");
var Queue = /** @class */ (function (_super) {
__extends(Queue, _super);
function Queue(config) {
if (config === void 0) { config = { autoStart: false, workers: [], concurrency: Infinity }; }
var _a, _b, _c;
var _this = _super.call(this) || this;
_this.workers = []; // 任务列表
_this.workersCache = []; // 保存原来的任务列表
_this.pending = 0; // 进行中的任务数量
_this.autoStart = false; // 是否自动开始
_this.isRuning = false; //是否执行中
_this.isAborted = false; //是否中止了
_this.concurrency = Infinity; //并发数量
_this.autoStart = (_a = config === null || config === void 0 ? void 0 : config.autoStart) !== null && _a !== void 0 ? _a : false; // 是否自动开始
_this.concurrency = (_b = config === null || config === void 0 ? void 0 : config.concurrency) !== null && _b !== void 0 ? _b : Infinity; //并发数量
_this.push.apply(//并发数量
_this, ((_c = config === null || config === void 0 ? void 0 : config.workers) !== null && _c !== void 0 ? _c : []));
return _this;
}
Object.defineProperty(Queue.prototype, "length", {
/**
* 获取执行中和队列中的数量,已执行完的不计算在内
*/
get: function () {
return this.workers.length + this.pending;
},
enumerable: false,
configurable: true
});
/**
* 添加队列任务
* @param {...any} workers 任务列表
*/
Queue.prototype.push = function () {
var _a, _b;
var workers = [];
for (var _i = 0; _i < arguments.length; _i++) {
workers[_i] = arguments[_i];
}
(_a = this.workers).push.apply(_a, workers);
(_b = this.workersCache).push.apply(_b, workers);
if (this.autoStart) {
this.start();
}
};
/**
* 执行下一任务
* @param {*} worker
* @returns
*/
Queue.prototype.next = function (worker) {
var _this = this;
if (worker === void 0) { worker = null; }
if (this.isAborted) {
return false;
}
if (!worker) {
if (this.length <= 0) {
this.isRuning = false;
this.emit("end", null);
return false;
}
if (this.workers.length <= 0) {
return false;
}
}
this.pending++;
var _worker = worker !== null && worker !== void 0 ? worker : this.workers.shift();
Promise.resolve(typeof _worker == "function" ? _worker() : _worker)
.then(function (res) {
!_this.isAborted && _this.emit("success", res);
})
.catch(function (error) {
!_this.isAborted && _this.emit("error", error, _worker);
})
.finally(function () {
_this.pending--;
!_this.isAborted && _this.next();
});
};
/**
* 开始任务
*/
Queue.prototype.start = function () {
if (this.isAborted) {
// 如果之前已经中止过,则重新取之前的全部队列,重新执行
this.workers = __spreadArrays(this.workersCache);
}
this.isAborted = false;
if (!this.isRuning) {
this.emit("start");
this.isRuning = true;
}
while (this.concurrency > this.pending && this.workers.length > 0 && !this.isAborted) {
this.next();
}
};
/**
* 中止任务
*/
Queue.prototype.abort = function () {
this.isAborted = true;
if (this.isRuning) {
this.isRuning = false;
this.emit("end", new Error("The queue is aborted !"));
}
};
Queue.prototype.end = function () {
this.abort();
};
return Queue;
}(EventEmitter_1.EventEmitter));
exports.Queue = Queue;