@newdash/newdash
Version:
javascript/typescript utility library
82 lines (81 loc) • 2.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BlockedQueue = exports.QueueOverFlowError = void 0;
/* eslint-disable max-len */
const assert_1 = require("../assert");
/**
*
* queue pending size out of limit
*
* @internal
* @private
* @ignore
*/
class QueueOverFlowError extends Error {
}
exports.QueueOverFlowError = QueueOverFlowError;
/**
* BlockedQueue
*
* a blocked queue for async operations
*
* @since 5.19.0
* @category Functional
*/
class BlockedQueue {
_capacity;
_notifyQueue;
_maxPending;
_container;
/**
* @param capacity default 1000
* @param maxPending default 10 * capacity, the max allowed number of pending items
*/
constructor(capacity = 1000, maxPending = 10 * capacity) {
(0, assert_1.mustProvide)(capacity, "capacity", "number");
(0, assert_1.mustProvide)(maxPending, "maxPending", "number");
this._capacity = capacity;
this._container = new Array(0);
this._notifyQueue = new Array(0);
this._maxPending = maxPending;
}
/**
* push an item to the queue, blocked when the queue is full
*
* @throws {QueueOverFlowError} throw when pending items are too much
* @param item
* @returns
*/
async enQueue(item) {
if (this._container.length >= this._capacity) {
if (this._notifyQueue.length <= this._maxPending) {
return new Promise((resolve) => {
this._notifyQueue.push(() => {
this._container.push(item);
resolve();
});
});
}
throw new QueueOverFlowError(`BlockedQueue: the number of pending items is more than the max allowed number of pending items: '${this._maxPending}', value: '${item}'`);
}
this._container.push(item);
return;
}
/**
* retrieve an item from queue
*
* @returns
*/
async deQueue() {
if (this._container.length > 0) {
const item = this._container.shift();
if (this._notifyQueue.length > 0) {
this._notifyQueue.shift()();
}
return item;
}
return undefined;
}
}
exports.BlockedQueue = BlockedQueue;
exports.default = BlockedQueue;