Pipe.js
This lib is deprecated, do not use it in new projects: use Promises
instead. This lib will remain on NPM for compatibility purpose.
Simply queue asynchrone jobs.
Create a Pipe
New Pipe
var p = new Pipe(
function() {},
function() {},
function(progress) {}
);
Add a job
Simple job:
p.add(function(pipe) {
});
Job with an argument:
p.add(function(pipe, arg) {
}, "argument");
Add multiple job in one round
You can add multiple jobs whose share the same code but that will receive
different arguments:
p.addAll(function(pipe, arg) {
}, ["arg1", "arg2", "arg3"]);
This add 3 jobs and is equivalent to:
p.add(function(pipe, arg) {
}, "arg1");
p.add(function(pipe, arg) {
}, "arg2");
p.add(function(pipe, arg) {
}, "arg3");
Run the pipe
Once all jobs are in the Pipe, you can run it:
p.run()
NOTE: Any argument passed to the run()
method will be forwarded to the
first job.
About jobs and pipe object
Jobs are just Javascript functions. Each job receive a pipe
object as first
parameter.
The pipe
object provides two useful functions:
pipe.done()
: must be called when the job is done (this will start the next
job). Any argument passed to this function will be forwarded to the next job
or to the successCallback
function if it is the last job.pipe.error()
: must be called if an error occurred (this will stop the Pipe
and call the errorCallback
). Any argument passed to this function will be
forwarded to the errorCallback
function.
Example of Job:
function job(pipe, url) {
var img = new Image();
img.onload = function() {
pipe.done(img);
}
img.onerror = function(error) {
pipe.error(error);
}
img.src = url;
}