1.0.2 • Published 8 years ago

flozz-pipejs v1.0.2

Weekly downloads
3
License
BSD-3-Clause
Repository
github
Last release
8 years ago

Pipe.js

Build Status NPM Version License

This lib is deprecated, do not use it in new projects: use Promises instead. This lib will remain on NPM for compatibility purpose.

Pipe

Simply queue asynchrone jobs.

Create a Pipe

New Pipe

var p = new Pipe(
    function() {},         // Success Callback
    function() {},         // Error Callback
    function(progress) {}  // Progress callback
);

Add a job

Simple job:

p.add(function(pipe) {
    // Job code here...
});

Job with an argument:

p.add(function(pipe, arg) {
    // Job code here...
}, "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) {
    // Job code here...
}, ["arg1", "arg2", "arg3"]);

This add 3 jobs and is equivalent to:

p.add(function(pipe, arg) {
    // Job code here...
}, "arg1");

p.add(function(pipe, arg) {
    // Job code here...
}, "arg2");

p.add(function(pipe, arg) {
    // Job code here...
}, "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;
}