UNPKG

p-queue-es5

Version:

Promise queue with concurrency control

46 lines 1.59 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var lower_bound_1 = require("./lower-bound"); var PriorityQueue = /** @class */ (function () { function PriorityQueue() { this._queue = []; } PriorityQueue.prototype.enqueue = function (run, options) { options = __assign({ priority: 0 }, options); var element = { priority: options.priority, run: run }; if (this.size && this._queue[this.size - 1].priority >= options.priority) { this._queue.push(element); return; } var index = lower_bound_1.default(this._queue, element, function (a, b) { return b.priority - a.priority; }); this._queue.splice(index, 0, element); }; PriorityQueue.prototype.dequeue = function () { var item = this._queue.shift(); return item && item.run; }; Object.defineProperty(PriorityQueue.prototype, "size", { get: function () { return this._queue.length; }, enumerable: true, configurable: true }); return PriorityQueue; }()); exports.default = PriorityQueue; //# sourceMappingURL=priority-queue.js.map