@itwin/imodels-client-authoring
Version:
iModels API client wrapper for applications that author iModels.
36 lines • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LimitedParallelQueue = void 0;
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
class LimitedParallelQueue {
_queue = [];
_maxParallelPromises;
constructor(config) {
this._maxParallelPromises = config.maxParallelPromises;
}
push(item) {
this._queue.push(item);
}
async waitAll() {
const currentlyExecutingPromises = new Array();
while (this._queue.length !== 0 ||
currentlyExecutingPromises.length !== 0) {
while (this._queue.length !== 0 &&
currentlyExecutingPromises.length < this._maxParallelPromises) {
// We create a promise that removes itself from the `currentlyExecutingPromises` queue after it resolves.
const itemToExecute = this._queue.shift();
const executingItem = itemToExecute().then(() => {
const indexOfItemInQueue = currentlyExecutingPromises.indexOf(executingItem);
currentlyExecutingPromises.splice(indexOfItemInQueue, 1);
});
currentlyExecutingPromises.push(executingItem);
}
await Promise.race(currentlyExecutingPromises);
}
}
}
exports.LimitedParallelQueue = LimitedParallelQueue;
//# sourceMappingURL=LimitedParallelQueue.js.map