README
Priority Concurrency Queue
A fast promise queue with priority and concurrency options.
Installation
This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. Node.js 0.10 or higher is required.
Installation is done using the
npm install
command:
$ npm install @robinlemon/priority-concurrency-queue
Usage
Sorted highest priority first, then by time added for those of the same priority.
Short Example:
import { AsyncQueue, IQueueItem } from '@robinlemon/priority-concurrency-queue';
await (new AsyncQueue({
Concurrency: 10
}).Add({
Task: async () => {};
Priority: 1;
}).Start());
console.log('Tasks Complete!');
Using AutoStart:
import { AsyncQueue, IQueueItem } from '@robinlemon/priority-concurrency-queue';
new AsyncQueue({
Concurrency: 10,
AutoStart: true,
}).Add({
Task: async () => {};
Priority: 1;
});
Long Example:
import { AsyncQueue, IQueueItem } from '@robinlemon/priority-concurrency-queue';
const Concurrency = 10;
const Queue = new AsyncQueue({ Concurrency });
const Item: IQueueItem = {
Task: async () => {};
Priority: 1;
};
Queue.Add(Item);
console.log(Queue.Tasks); // 1
console.log(Queue.isRunning); // false
Queue.Start(); // start the queue
console.log(Queue.isRunning); // true
await Queue.Start(); // wait until complete
console.log(Queue.Tasks); // 0
console.log(Queue.isRunning); // false
/**
* Since the concurrency is 10,
* only 10 tasks will be processed
* and the rest will be idle.
*/
for(let i = 0; i < 100; i++) Queue.Add(Item);
Queue.Start();
Queue.Stop();
console.log('Tasks Complete!');
Queue.Clear(true); // Wait for tasks to finish
// The above is the same as:
Queue.Clear()
await Queue.Start();
Queue.Add(Item);
console.log(Queue.Tasks); // 1
Queue.ClearPriority(1);
console.log(Queue.Tasks); // 0
Tests
To run the test suite, first install the dependencies, then run npm test
:
$ npm install
$ npm test