Queuer
Run easily queue of tasks.
Installation
It is available with bower or npm:
bower install queuer.js
npm install queuer.js
Include queuer.min.js
to the HTML, and the queuer
object is now available in the global scope:
<script type="text/javascript" src="/path/to/bower_components/queuer.js/dist/queuer.min.js"></script>
Alternately, you can use a module manager to avoid global scoping:
var queuer = require('queuer.js');
import queuer from 'queuer.js';
Usage
Create a queue
var queue = queuer();
Register tasks on it
queue.task('task1', function(payload, queue) {
console.log('I am a task');
});
Running the queue
queue(initialPayload)
.then(function(payload) {
})
.catch(function(error) {
});
Dealing with events
The queue is an event emitter. That means you can emit or listen events on it. The queue already emit some events:
EVENT_TASK_START
: A task was started.EVENT_TASK_STOP
: A task was stopped.EVENT_TASK_START
: A task threw an error.EVENT_CANCEL
: The queue was canceled.
To register an event listener use on(event, listener)
or once(event, listener)
method on the queue:
queue.on(queue.EVENT_TASK_START, function(taskName, payload) {
});
queue.once(queue.EVENT_CANCEL, function() {
});
As the queue is given as an argument to the task, you can use it in tasks to listen or to emit some custom event as you wish:
queue.task('task1', function(payload, queue) {
queue.once(queue.EVENT_CANCEL, function() {
});
queue.on('myCustomEvent', function(data) {
});
});
queue.emit('myCustomEvent', 'test');
Cancel the queue
The queue exposes a shorcut method to cancel it queue.cancel()
. You can pass arguments to it if you wish, they will be forwarded across the cancel event.
Development
Installation
make install
Build
make build
or make build-dev
(unminified version)
Watch
make watch
Test
make test
Contributing
All contributions are welcome and must pass the tests. If you add a new feature, please write tests for it.
License
This application is available under the MIT License.