priority-blocking-queue

1.0.1 • Public • Published

Priority Blocking Queue

A simple priority blocking queue. Items are sorted as they're added to the queue. Takers always remove items in sorted order, and block on an empty queue, waiting for a new item to be added.

Installation

npm install --save priority-blocking-queue

API

Add items to the queue and then remove them in sorted order.

const PriorityBlockingQueue = require('priority-blocking-queue');
let queue = new PriorityBlockingQueue((lhs, rhs) => lhs.p - rhs.p);
queue.put({p:2}).put({p:3}, {p:1});
 
let a = await queue.take();
console.log(a.p);
// 3
 
let b = await queue.take();
console.log(b.p);
// 2
 
let c = await queue.take();
console.log(c.p);
// 1

Wait for an item to become available.

const PriorityBlockingQueue = require('priority-blocking-queue');
let queue = new PriorityBlockingQueue((lhs, rhs) => lhs.p - rhs.p);
queue.take().then((item) => console.log('taker 1 got ', item.p));
queue.take().then((item) => console.log('taker 2 got ', item.p));
 
console.log(queue.size());
// 0
 
console.log(queue.takersBlocked());
// 2
 
setTimeout(function () {
  queue.put({p:2}).put({p:3}, {p:1});
}, 1000);
 
// taker 1 got 3
// taker 2 got 2

Package Sidebar

Install

npm i priority-blocking-queue

Weekly Downloads

257

Version

1.0.1

License

MIT

Unpacked Size

5.44 kB

Total Files

5

Last publish

Collaborators

  • massaroni