1.0.1 • Published 5 years ago

priority-blocking-queue v1.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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