1
0
mirror of https://github.com/musix-org/musix-oss synced 2024-12-23 16:13:18 +00:00
musix-oss/node_modules/m3u8stream/dist/queue.js
2020-03-03 22:30:50 +02:00

52 lines
1.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Queue {
/**
* A really simple queue with concurrency.
*/
constructor(worker, options = {}) {
this._worker = worker;
this._concurrency = options.concurrency || 1;
this.tasks = [];
this.total = 0;
this.active = 0;
}
/**
* Push a task to the queue.
*/
push(item, callback) {
this.tasks.push({ item, callback });
this.total++;
this._next();
}
/**
* Process next job in queue.
*/
_next() {
if (this.active >= this._concurrency || !this.tasks.length) {
return;
}
const { item, callback } = this.tasks.shift();
let callbackCalled = false;
this.active++;
this._worker(item, (err, result) => {
if (callbackCalled) {
return;
}
this.active--;
callbackCalled = true;
if (callback) {
callback(err, result);
}
this._next();
});
}
/**
* Stops processing queued jobs.
*/
die() {
this.tasks = [];
}
}
exports.default = Queue;
//# sourceMappingURL=queue.js.map