@visactor/vtable
Version:
canvas table width high performance
22 lines (21 loc) • 764 B
JavaScript
export class LimitPromiseQueue {
constructor(max) {
this.max = max, this._count = 0, this._pendingTaskQueue = [];
}
call(caller, ...arg) {
return new Promise(((resolve, reject) => {
const task = this._createTask(caller, arg, resolve, reject);
this._count >= this.max ? this._pendingTaskQueue.push(task) : task();
}));
}
_createTask(caller, arg, resolve, reject) {
return () => {
this._count++, caller(...arg).then(resolve).catch(reject).finally((() => {
if (this._count--, this._pendingTaskQueue.length) {
this._pendingTaskQueue.shift()();
}
}));
};
}
}
//# sourceMappingURL=LimitPromiseQueue.js.map