A blocking queue implementation with a fixed capacity. The enqueue and dequeue methods will block if the queue is full or empty, respectively, until space is available or an item is enqueued. This allows for producers being throttled by back-pressure from the availability of consumers and a very simple implementation of a continuous consumer.
Example
async function consume(q, id) {
while (true) {
const msg = await q.dequeue();
console.log(id, "started processing", msg);
await new Promise((resolve) => setTimeout(resolve, 100)); // Pretend to be busy
console.log(id, "finished processing", msg);
}
}
const q = new Queue(2);
consume(q, "Worker 1");
consume(q, "Worker 2");
await q.enqueue("Hello, World 1!"); // Consumed immediately
await q.enqueue("Hello, World 2!"); // Consumed immediately
await q.enqueue("Hello, World 3!"); // Held in queue
await q.enqueue("Hello, World 4!"); // Held in queue
await q.enqueue("Hello, World 5!"); // Blocked until worker available
await q.enqueue("Hello, World 6!"); // Blocked until worker available
Methods
(async) dequeue()
Get the next item from the queue. If the queue is empty, this will block
until an item is enqueued. Always await this method to ensure you get the
proper result out.
Returns:
The next item from the queue
(async) enqueue(item)
Put an item in the queue. If the queue is full and you await this
function it will block until space is available, allowing for back-pressure
to throttle producers. If you don't await this function, you effectively
have an "infinite" capacity, since the enqueue functions will wait in the
JavaScript event loop Promise queue. This is probably not what you want.
Parameters:
| Name | Type | Description |
|---|---|---|
item |
* | The item to put in the queue |
isEmpty()
Returns:
true if the queue is empty, false otherwise
isFull()
Returns:
true if the queue is full, false otherwise
peek()
Peek at the next item in the queue without dequeuing it. This will return
undefined if the queue is empty, but it will not block.
Returns:
The next item in the queue, or undefined if the queue is empty
size()
Returns:
The number of items currently in the queue