|
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +var __generator = (this && this.__generator) || function (thisArg, body) { |
| 12 | + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; |
| 13 | + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; |
| 14 | + function verb(n) { return function (v) { return step([n, v]); }; } |
| 15 | + function step(op) { |
| 16 | + if (f) throw new TypeError("Generator is already executing."); |
| 17 | + while (_) try { |
| 18 | + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; |
| 19 | + if (y = 0, t) op = [op[0] & 2, t.value]; |
| 20 | + switch (op[0]) { |
| 21 | + case 0: case 1: t = op; break; |
| 22 | + case 4: _.label++; return { value: op[1], done: false }; |
| 23 | + case 5: _.label++; y = op[1]; op = [0]; continue; |
| 24 | + case 7: op = _.ops.pop(); _.trys.pop(); continue; |
| 25 | + default: |
| 26 | + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } |
| 27 | + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } |
| 28 | + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } |
| 29 | + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } |
| 30 | + if (t[2]) _.ops.pop(); |
| 31 | + _.trys.pop(); continue; |
| 32 | + } |
| 33 | + op = body.call(thisArg, _); |
| 34 | + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } |
| 35 | + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; |
| 36 | + } |
| 37 | +}; |
| 38 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 39 | +exports.AsyncQueue = void 0; |
| 40 | +var AsyncQueue = /** @class */ (function () { |
| 41 | + function AsyncQueue(options) { |
| 42 | + if (options === void 0) { options = { concurrency: 1 }; } |
| 43 | + var _this = this; |
| 44 | + this._concurrency = 1; |
| 45 | + this._worker = function () { return __awaiter(_this, void 0, void 0, function () { |
| 46 | + return __generator(this, function (_a) { |
| 47 | + return [2 /*return*/]; |
| 48 | + }); |
| 49 | + }); }; |
| 50 | + this.concurrency(options.concurrency); |
| 51 | + this._data = []; |
| 52 | + this._started = false; |
| 53 | + this._workerCount = 0; |
| 54 | + this._stopOnComplete = options.stopOnComplete === true; |
| 55 | + this._workerFinished = this._workerFinished.bind(this); |
| 56 | + this._workerFailed = this._workerFailed.bind(this); |
| 57 | + } |
| 58 | + AsyncQueue.prototype.overwrite = function (val) { |
| 59 | + this._data = val; |
| 60 | + this.update(); |
| 61 | + }; |
| 62 | + AsyncQueue.prototype.push = function (val) { |
| 63 | + this._data.push(val); |
| 64 | + this.update(); |
| 65 | + }; |
| 66 | + AsyncQueue.prototype.concat = function (val) { |
| 67 | + var _this = this; |
| 68 | + val.forEach(function (item) { |
| 69 | + _this._data.push(item); |
| 70 | + }); |
| 71 | + this.update(); |
| 72 | + }; |
| 73 | + AsyncQueue.prototype.concurrency = function (val) { |
| 74 | + if (val !== undefined) { |
| 75 | + this._concurrency = val; |
| 76 | + return this; |
| 77 | + } |
| 78 | + return this._concurrency; |
| 79 | + }; |
| 80 | + AsyncQueue.prototype.worker = function (val) { |
| 81 | + if (val !== undefined) { |
| 82 | + this._worker = val; |
| 83 | + return this; |
| 84 | + } |
| 85 | + return this._worker; |
| 86 | + }; |
| 87 | + AsyncQueue.prototype.start = function (onFinish) { |
| 88 | + if (onFinish) { |
| 89 | + this._onFinish = onFinish; |
| 90 | + } |
| 91 | + if (this._started) { |
| 92 | + return; |
| 93 | + } |
| 94 | + this._started = true; |
| 95 | + this.update(); |
| 96 | + }; |
| 97 | + AsyncQueue.prototype.stop = function () { |
| 98 | + this._started = false; |
| 99 | + }; |
| 100 | + AsyncQueue.prototype.update = function () { |
| 101 | + if (!this._started) { |
| 102 | + return; |
| 103 | + } |
| 104 | + if (this._workerCount >= this.concurrency()) { |
| 105 | + return; |
| 106 | + } |
| 107 | + if (!this._data.length) { |
| 108 | + if (this._workerCount === 0) { |
| 109 | + this.finished(); |
| 110 | + } |
| 111 | + return; |
| 112 | + } |
| 113 | + this._startWorker(); |
| 114 | + this.update(); |
| 115 | + }; |
| 116 | + AsyncQueue.prototype._startWorker = function () { |
| 117 | + var _this = this; |
| 118 | + if (!this._data.length) { |
| 119 | + return; |
| 120 | + } |
| 121 | + // Grab data for new worker, removing it from |
| 122 | + // the existing array |
| 123 | + var workerData = this._data.shift(); |
| 124 | + // Increment worker count |
| 125 | + this._workerCount++; |
| 126 | + // Fire up worker process |
| 127 | + setTimeout(function () { |
| 128 | + _this._worker(workerData).then(_this._workerFinished).catch(_this._workerFailed); |
| 129 | + }, 1); |
| 130 | + }; |
| 131 | + AsyncQueue.prototype._workerFinished = function () { |
| 132 | + // Decrement worker count |
| 133 | + this._workerCount--; |
| 134 | + this.update(); |
| 135 | + }; |
| 136 | + AsyncQueue.prototype._workerFailed = function () { |
| 137 | + // Decrement worker count |
| 138 | + this._workerCount--; |
| 139 | + this.update(); |
| 140 | + }; |
| 141 | + AsyncQueue.prototype.finished = function () { |
| 142 | + if (this._stopOnComplete) { |
| 143 | + this.stop(); |
| 144 | + } |
| 145 | + this._onFinish && this._onFinish(); |
| 146 | + }; |
| 147 | + return AsyncQueue; |
| 148 | +}()); |
| 149 | +exports.AsyncQueue = AsyncQueue; |
0 commit comments