UNPKG

@sofie-automation/server-core-integration

Version:
47 lines 1.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Queue = void 0; class Queue { constructor() { this._isRunning = false; this._queue = []; } async putOnQueue(fcn) { return new Promise((resolve, reject) => { this._queue.push({ fcn, resolve, reject, }); setTimeout(() => { this.checkQueue(); }, 0); }); } checkQueue() { if (!this._isRunning) { const nextOnQueue = this._queue.shift(); if (nextOnQueue) { this._isRunning = true; try { nextOnQueue.fcn().then((result) => { nextOnQueue.resolve(result); this._isRunning = false; this.checkQueue(); }, (error) => { nextOnQueue.reject(error); this._isRunning = false; this.checkQueue(); }); } catch (error) { nextOnQueue.reject(error); this._isRunning = false; this.checkQueue(); } } } } } exports.Queue = Queue; //# sourceMappingURL=queue.js.map