"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); // Runs a limited number of promises at one time class ConcurrentQueue { constructor(consecutive) { this.consecutive = consecutive; this.queue = []; this.current = 0; this.drainListeners = []; } _checkQueue() { if (this.current == 0 && this.queue.length == 0) { for (let drainListener of this.drainListeners) { drainListener(); } this.drainListeners = []; } while (this.current < this.consecutive && this.queue.length > 0) { let taskData = this.queue.shift(); if (taskData === undefined) break; this.current += 1; (() => __awaiter(this, void 0, void 0, function* () { try { taskData.resolve(yield taskData.task()); } catch (e) { taskData.reject(e); } this.current -= 1; this._checkQueue(); }))(); } } // returns a promise that can be awaited to get the resolution or rejection of the task's execution push(task) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { this.queue.push({ task, resolve, reject }); this._checkQueue(); }); }); } waitForDrain() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve) => { this.drainListeners.push(() => { resolve(); }); this._checkQueue(); }); }); } } exports.default = ConcurrentQueue;