simple-in-memory-queue
Version:
A simple in-memory queue, for nodejs and the browser, with consumers for common usecases.
61 lines • 3.12 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createQueueWithBatchConsumer = void 0;
const constants_1 = require("../../domain/constants");
const createQueue_1 = require("../queue/createQueue");
/**
* creates a queue with a consumer that consumes batches of items
*
* note
* - batches are created when either batchSize is met or delayThreshold is exceeded
*
* usecases
* - sending batches of events to another source, without waiting too long between event creation and event submission
*/
const createQueueWithBatchConsumer = ({ consumer, threshold, }) => {
// create the queue
const queue = (0, createQueue_1.createQueue)({ order: constants_1.QueueOrder.FIRST_IN_FIRST_OUT });
// define what to do when a threshold is crossed
let priorTimeoutHandle = null;
const onThresholdExceeded = () => __awaiter(void 0, void 0, void 0, function* () {
// clear the timeout, to ensure that if size threshold fires the timeout threshold wont cause a duplicate trigger
if (priorTimeoutHandle) {
clearTimeout(priorTimeoutHandle);
priorTimeoutHandle = null;
}
// define the size of the batch to pull
const size = queue.length > threshold.size
? threshold.size // if queue has more items than threshold, use threshold size as batch size -> sets max size
: queue.length + 0; //otherwise, batch size is all available items; (+0 to ensure its a copy, and not a reference)
// grab the batch of items
const items = queue.peek(size);
// wait for the consumer to successfully process the items
yield consumer({ items });
// dequeue the items from the queue, now that they're all processed
queue.pop(size);
});
// and set checks for whether the threshold is exceeded, on each push
queue.on.push.subscribe({
consumer: () => {
// if a timeout does not exist, start it
if (!priorTimeoutHandle)
priorTimeoutHandle = setTimeout(onThresholdExceeded, threshold.milliseconds);
// if size has been exceeded, trigger now
if (queue.length >= threshold.size)
void onThresholdExceeded();
},
});
// return the queue
return queue;
};
exports.createQueueWithBatchConsumer = createQueueWithBatchConsumer;
//# sourceMappingURL=createQueueWithBatchConsumer.js.map