package
Bounded batch queue, where items are produced and consumed based on user specified functions
npm install bounded-queueGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Tagged topics.
bounded-queue helps solves the producer–consumer problem.
graph LR;
P(Producer);
B[bounded-queue];
C(Consumer);
P-- batched item -->B;
B-- batched item -->C;
style B fill:#99E,stroke:#333The bounded-queue allows the producer and consumer to operate in
Imagine you have to read records from a database and write those to another database. A simple way to that move the records is to first read from database A and sequentially write each record to database B.
let batchNr = 0;
let items2produce = 10;
/**
* Mockup database A, producer
*/
const dbA = {
readRecord: async () => {
++batchNr;
console.log("Producing batch #", batchNr);
await new Promise(resolve => setTimeout(resolve, 100));
console.log("Produced batch #", batchNr);
return batchNr;
},
moreRecordsAvailable: () => batchNr < items2produce
In the previous example, we either read from database A, or write to database B.
It would be faster if read from database A, while we write to database B, at the same time.
As dbA.readRecord() and dbB.readRecord()areasync` functions, there is no need to introduce threading to accomplish that.
The bounded-queue helps you with that. The following example uses bounded-queue, with a maximum of 3 queued records:
import {queue} from 'bounded-queue';
let batchNr = 0;
let items2produce = 10;
/**
* Mockup database A, producer
*/
const dbA = {
readRecord: async () => {
++batchNr;
console.log("Producing batch #", batchNr);
await new Promise(resolve => setTimeout(resolve, 100));
console.log("Produced batch #", batchNr);
return batchNr;
},
moreRecordsAvailable
Using the bounded-queue, the conversion will complete in roughly half the time.
npm install bounded-queueconstructor(maxQueueSize: number, producer: Producer<ItemType>, consumer: Consumer<ItemType>)maxQueueSize (number): Maximum number of items that can be in the queue.producer (Producer): A function that produces items to be added to the queue.consumer (Consumer): A function that consumes items from the queue.BoundedQueue instance with the specified maxQueueSize, producer, and consumer.length(): numberrun(): Promise<void>producer function and concurrently consumes items using the consumer function.import { queue } from 'your-module';
// Create and run a BoundedQueue with a maximum queue size of 10
queue(10, producer, consumer)
.then(() => {
console.log('Queue processing completed');
})
.catch((error) => {
console.error('Error during queue processing:', error);
});queue<ItemType>(maxQueueSize: number, producer: Producer<ItemType>, consumer: Consumer<ItemType>): Promise<void>maxQueueSize (number): Maximum number of items that can be in the queue.producer (Producer): A function that produces items to be added to the queue.consumer (Consumer): A function that consumes items from the queue.BoundedQueue instance. It takes the same parameters as the BoundedQueue constructor and returns a Promise that resolves when all items have been produced and consumed.null indicated the end of the production.import { queue } from 'your-module';
// Create and run a BoundedQueue with a maximum queue size of 10
queue(10, producer, consumer)
.then(() => {
console.log('Queue processing completed');
})
.catch((error) => {
console.error('Error during queue processing:', error);
});Estimated unpacked size across 2 packages.
1.1 MB
Includes dependencies.