Registry / testing / priority-blocking-queue

priority-blocking-queue

JSON →
library1.0.1jsnpmunverified

A simple asynchronous priority queue for Node.js (v1.0.1). Items are inserted in sorted order using a custom comparator (default descending). The take() method returns a Promise that resolves to the next highest-priority item, blocking if the queue is empty until an item is put. Supports multiple blocked takers. Lightweight, no dependencies, CJS only. Differentiators: minimal API (put/take/size/takersBlocked), comparator is maximum-oriented (item with largest comparator value is taken first). Not compatible with ESM imports.

npm install priority-blocking-queue
INSTALL
IMPORT
SIG · PRIORITY-BLOCKING-
P
priority-blocking-queue
testingjavascriptv1.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

PriorityBlockingQueue
const PriorityBlockingQueue = require('priority-blocking-queue');
import PriorityBlockingQueue from 'priority-blocking-queue';
Package is CJS-only; cannot use ESM import (will throw SyntaxError).
PriorityBlockingQueue
const { PriorityBlockingQueue } = require('priority-blocking-queue');
const { default: PriorityBlockingQueue } = require('priority-blocking-queue');
Export is a single constructor, not a named export with default property.
PriorityBlockingQueue
const Queue = require('priority-blocking-queue'); const queue = new Queue(comparator);
const queue = new PriorityBlockingQueue(comparator);
The require returns the constructor directly; you can assign it to any variable name.

Shows instantiation with comparator, adding multiple items at once, and asynchronous consumption with blocking.

const PriorityBlockingQueue = require('priority-blocking-queue'); // Comparator: items with larger .p are taken first (max-priority) const queue = new PriorityBlockingQueue((lhs, rhs) => lhs.p - rhs.p); queue.put({p: 2}, {p: 3}, {p: 1}); async function process() { while (queue.size() > 0) { const item = await queue.take(); console.log('Processing item with priority', item.p); } console.log('Queue empty, waiting...'); const item = await queue.take(); console.log('Got item after wait:', item.p); } process(); // Processing item with priority 3 // Processing item with priority 2 // Processing item with priority 1 // Queue empty, waiting... // (then blocks until another put)
Debug
Known issues
gotchaComparator defines maximum priority: item with largest value (lhs.p - rhs.p > 0) is taken first, not minimum.
fix
Reverse comparator logic if you want min-priority queue: (lhs, rhs) => rhs.p - lhs.p.
affects: >=1.0
gotchaput() accepts multiple items but does NOT return a Promise; it returns the queue instance for chaining.
fix
Use await queue.put(item) only if you need to wait for space (not implemented); simply call queue.put(item1, item2, ...).
affects: >=1.0
deprecatedMethod add() is deprecated in favor of put().
fix
Use put() instead of add().
affects: >=1.0
gotchatake() returns a Promise that resolves when an item is available; it never rejects (no timeout).
fix
If you need a timeout, wrap take() with Promise.race and a timeout Promise.
affects: >=1.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Package is CJS-only; ESM import fails.
fix
Use require() instead of import.
TypeError: PriorityBlockingQueue is not a constructor
Using wrong import style (named import vs default).
fix
Use const PriorityBlockingQueue = require('priority-blocking-queue');
items are taken in reverse order
Comparator assumes min-priority but library uses max-priority.
fix
Reverse comparator: (lhs, rhs) => rhs.p - lhs.p for min-priority.
queue.put is not a function
Using add() which was deprecated and removed? Actually add is present but deprecated; error occurs if you do not require correctly.
fix
Check that require returns the constructor: const PriorityBlockingQueue = require('priority-blocking-queue');
Upgrade
Version history
1.0.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
priority-blocking-queue — npm install priority-blocking-queue · libregistry