UNPKG

@esm2cjs/p-queue

Version:

Promise queue with concurrency control. This is a fork of sindresorhus/p-queue, but with CommonJS support.

40 lines (39 loc) 1.85 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _PriorityQueue_queue; import lowerBound from './lower-bound.js'; export default class PriorityQueue { constructor() { _PriorityQueue_queue.set(this, []); } enqueue(run, options) { options = { priority: 0, ...options, }; const element = { priority: options.priority, run, }; if (this.size && __classPrivateFieldGet(this, _PriorityQueue_queue, "f")[this.size - 1].priority >= options.priority) { __classPrivateFieldGet(this, _PriorityQueue_queue, "f").push(element); return; } const index = lowerBound(__classPrivateFieldGet(this, _PriorityQueue_queue, "f"), element, (a, b) => b.priority - a.priority); __classPrivateFieldGet(this, _PriorityQueue_queue, "f").splice(index, 0, element); } dequeue() { const item = __classPrivateFieldGet(this, _PriorityQueue_queue, "f").shift(); return item === null || item === void 0 ? void 0 : item.run; } filter(options) { return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").filter((element) => element.priority === options.priority).map((element) => element.run); } get size() { return __classPrivateFieldGet(this, _PriorityQueue_queue, "f").length; } } _PriorityQueue_queue = new WeakMap();