86 lines
3.9 KiB
JavaScript
86 lines
3.9 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
Object.defineProperty(exports, "PromiseQueue", {
|
|
enumerable: true,
|
|
get: function() {
|
|
return PromiseQueue;
|
|
}
|
|
});
|
|
const _class_private_field_loose_base = require("@swc/helpers/_/_class_private_field_loose_base");
|
|
const _class_private_field_loose_key = require("@swc/helpers/_/_class_private_field_loose_key");
|
|
var _maxConcurrency = /*#__PURE__*/ _class_private_field_loose_key._("_maxConcurrency"), _runningCount = /*#__PURE__*/ _class_private_field_loose_key._("_runningCount"), _queue = /*#__PURE__*/ _class_private_field_loose_key._("_queue"), _processNext = /*#__PURE__*/ _class_private_field_loose_key._("_processNext");
|
|
class PromiseQueue {
|
|
enqueue(promiseFn) {
|
|
let taskResolve;
|
|
let taskReject;
|
|
const taskPromise = new Promise((resolve, reject)=>{
|
|
taskResolve = resolve;
|
|
taskReject = reject;
|
|
});
|
|
const task = async ()=>{
|
|
try {
|
|
_class_private_field_loose_base._(this, _runningCount)[_runningCount]++;
|
|
const result = await promiseFn();
|
|
taskResolve(result);
|
|
} catch (error) {
|
|
taskReject(error);
|
|
} finally{
|
|
_class_private_field_loose_base._(this, _runningCount)[_runningCount]--;
|
|
_class_private_field_loose_base._(this, _processNext)[_processNext]();
|
|
}
|
|
};
|
|
const enqueueResult = {
|
|
promiseFn: taskPromise,
|
|
task
|
|
};
|
|
// wonder if we should take a LIFO approach here
|
|
_class_private_field_loose_base._(this, _queue)[_queue].push(enqueueResult);
|
|
_class_private_field_loose_base._(this, _processNext)[_processNext]();
|
|
return taskPromise;
|
|
}
|
|
bump(promiseFn) {
|
|
const index = _class_private_field_loose_base._(this, _queue)[_queue].findIndex((item)=>item.promiseFn === promiseFn);
|
|
if (index > -1) {
|
|
const bumpedItem = _class_private_field_loose_base._(this, _queue)[_queue].splice(index, 1)[0];
|
|
_class_private_field_loose_base._(this, _queue)[_queue].unshift(bumpedItem);
|
|
_class_private_field_loose_base._(this, _processNext)[_processNext](true);
|
|
}
|
|
}
|
|
constructor(maxConcurrency = 5){
|
|
Object.defineProperty(this, _processNext, {
|
|
value: processNext
|
|
});
|
|
Object.defineProperty(this, _maxConcurrency, {
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, _runningCount, {
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
Object.defineProperty(this, _queue, {
|
|
writable: true,
|
|
value: void 0
|
|
});
|
|
_class_private_field_loose_base._(this, _maxConcurrency)[_maxConcurrency] = maxConcurrency;
|
|
_class_private_field_loose_base._(this, _runningCount)[_runningCount] = 0;
|
|
_class_private_field_loose_base._(this, _queue)[_queue] = [];
|
|
}
|
|
}
|
|
function processNext(forced) {
|
|
if (forced === void 0) forced = false;
|
|
if ((_class_private_field_loose_base._(this, _runningCount)[_runningCount] < _class_private_field_loose_base._(this, _maxConcurrency)[_maxConcurrency] || forced) && _class_private_field_loose_base._(this, _queue)[_queue].length > 0) {
|
|
var _class_private_field_loose_base__queue_shift;
|
|
(_class_private_field_loose_base__queue_shift = _class_private_field_loose_base._(this, _queue)[_queue].shift()) == null ? void 0 : _class_private_field_loose_base__queue_shift.task();
|
|
}
|
|
}
|
|
|
|
if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
|
|
Object.defineProperty(exports.default, '__esModule', { value: true });
|
|
Object.assign(exports.default, exports);
|
|
module.exports = exports.default;
|
|
}
|
|
|
|
//# sourceMappingURL=promise-queue.js.map
|