Skip to content

A simple asynchronous blocking priority queue in javascript.

Notifications You must be signed in to change notification settings

massaroni/priority-blocking-queue

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A simple asynchronous blocking priority queue in javascript.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published