UNPKG

@itwin/imodels-client-authoring

Version:

iModels API client wrapper for applications that author iModels.

32 lines 1.53 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ export 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); } } } //# sourceMappingURL=LimitedParallelQueue.js.map