@mdaemon/process-queue
v1.4.2
Published
A class for pushing objects to a queue and getting the next object from the queue to be processed
Downloads
19
Maintainers
Readme
@mdaemon/process-queue, A class for pushing objects to a queue and getting the next object from the queue to be processed
Install
$ npm install @mdaemon/process-queue --saveNode CommonJS
const ProcessQueue = require("@mdaemon/process-queue/dist/processQueue.cjs");Node Modules
import ProcessQueue from "@mdaemon/process-queue/dist/processQueue.mjs";Web
<script type="text/javascript" src="/path_to_modules/dist/processQueue.umd.js">Creating a ProcessQueue
// Define your item type
interface MyQueueItem {
id: string | number;
// other properties...
}
// Create a new ProcessQueue
const queue = new ProcessQueue<MyQueueItem>();
Constructor Options
The ProcessQueue constructor accepts two optional parameters:
new ProcessQueue<QueueItem>(emplace?, maxSize?)Parameters:
emplace(boolean, default:false): Controls how duplicate items are handled when added to the queue- When
false: New items are added to the front of the queue, and any existing item with the same ID is removed first - When
true: New items replace existing items at their current position in the queue, or are added to the end if not found
- When
maxSize(number, default:1000): Maximum number of items allowed in the queue- Throws an error if you attempt to add items beyond this limit
Examples:
// Default behavior: items added to front, max 1000 items
const queue1 = new ProcessQueue<MyQueueItem>();
// Custom: replace items in-place, default max size
const queue2 = new ProcessQueue<MyQueueItem>(true);
// Custom: add to front, max 500 items
const queue3 = new ProcessQueue<MyQueueItem>(false, 500);
// Custom: replace in-place, max 100 items
const queue4 = new ProcessQueue<MyQueueItem>(true, 100);Adding Items to the Queue
const item: MyQueueItem = { id: '1', /* other properties */ };
const added = queue.queueItem(item);
console.log(added); // true if the item was added, false if it was already being processedGetting the Next Item
const nextItem = queue.getNextItem();
if (nextItem) {
// Process the item
// ...
// Mark it as done when finished
queue.doneProcessing(nextItem.id);
}Checking Queue Status
console.log(queue.length()); // Number of items in the queue
console.log(queue.busy()); // true if any items are being processed
console.log(queue.processSize()); // Number of items currently being processedRemoving Items
const removed = queue.removeFromQueue('itemId');
console.log(removed); // true if the item was removed, false if it wasn't in the queueGetting All Queue Items
const allItems = queue.getQueue();
console.log(allItems); // Array of all items in the queueAPI Reference
queueItem(item: QueueItem): boolean: Adds an item to the queue.
getNextItem(): QueueItem | null: Retrieves and removes the next item from the queue.
isProcessing(id: ItemID): boolean: Checks if an item is currently being processed.
doneProcessing(id?: ItemID): void: Marks an item (or all items if no id is provided) as done processing.
removeFromQueue(id: ItemID): boolean: Removes an item from the queue.
length(prop?: string, val?: any): number: Returns the number of items in the queue, optionally filtered by a property value.
busy(): boolean: Checks if any items are currently being processed.
processSize(): number: Returns the number of items currently being processed.
getQueue(processing: boolean = false): QueueItem[]: Returns all items in the queue, optionally moving them to the processing state.
clear(): void: Removes all items from both the queue and processing lists.
processBatch(batchSize: number): QueueItem[]: Processes multiple items from the front of the queue at once, moving them to the processing state.
isEmpty(): boolean: Returns true if the queue contains no items, false otherwise.
License
Published under the LGPL-2.1 license.
Published by MDaemon Technologies, Ltd. Simple Secure Email https://www.mdaemon.com
