A simple, automated queue processor
How to install
npm install bb-queue
How to use
var BBQ = require('bb-queue');
var bbq = new BBQ(callback, { options }, context);
Arguments | ||
---|---|---|
callback | function(object, [next]) | A function that takes one argument, this being the queue object to be processed. This function is called on each item of the queue |
options | object [optional] |
|
context | object [optional] | The object that the callback will be bound to, making the object accessible through the *this* keyword |
Methods | |
---|---|
add(object) | Adds an object to the queue, can be any type of object as long as you callback can handle it |
clear() | Clears the queue |
stop() | Stops/Pauses queue processing |
resume() | Resumes queue processing |
Syncronous Processing
If a second parameter is defined on the callback, calling that parameter as a function will cause the queue to advance to the next item.
var bbq = new BBQ(function(item, next) {
process(function() {
console.log(item);
next();
});
});
Examples
/*
* Queue is immediately monitored upon instantiation. Each object of the
* queue is passed into the callback for processing one at a time.
*/
var BBQ = require('bb-queue');
var bbq = new BBQ(function(ingredient) {
console.log(ingredient);
});
bbq.add('bread');
bbq.add('lettace');
// You can pass multiple items in one call to add()
bbq.add('mustard', 'onion', 'bacon');
/*
* Will print...
* bread
* lettace
* mustard
* onion
* bacon
*/
bbq.stop(); // Stop/Pause processing
bbq.add('cat'); // oops
bbq.clear(); // Clear queue
bbq.add('turkey', 'ham');
bbq.resume(); // Resume processing
bbq.stop(); // Don't leave the queue processing if re-assigning
bbq = new BBQ(function(color) {
console.log(color);
}, { lifo: true });
bbq.add('red', 'green', 'purple');
/*
* Prints...
* purple
* green
* red
*/
Change Log
1.0.0
- First release