Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tag: 0.1.0
Fetching contributors…

Cannot retrieve contributors at this time

file 93 lines (62 sloc) 2.755 kb

jWorkflow

Dude, where is my workflow?

jWorkflow is a workflow engine for JavaScript that provides the ability to create workflows to chain methods together in an easy to understand syntax:

var fooodOrder = jWorkflow.order(garlicChicken)
                          .andThen(whiteRice)
                          .andThen(wontonSoup)
                          .andThen(cookiesFortune)
                          .andThen(noAndThen)
                          .andThen(noAndThen)
                          .andThen(noAndThen)
                          .andThen(noAndThen);

fooodOrder.start();

Usage

jWorkflow orders are started with a call to jWorkflow.order:

function dude() {
    // some of the best code in the world will live here
}

var order = jWorkflow.order(dude);

// orders can also be started with no initial function
var pizzacoli = jWorkflow.order();

jWorkflow tasks at the root are just functions that will be invoked in the order they are built. Any number of tasks can be then appended to the order:

order.andThen(sweet).andThen(dude).andThen(sweet);

The functions passed into the order will not be invoked until you call:

order.start();

The context to be used when invoking the functions can be passed in while creating the order:

order.andThen(transfunctioner.photonAcceleratorAnnihilationBeam, transfunctioner);

Passing Values between tasks

jWorkflow tasks can access the return value of the previous task with the previous parameter:

function meaningOfLife() {
   //find the meaning of life
   return 42; 
}

function writeBook(previous) {
   console.log("the meaning of life is " + previous);
}

var guide = jWorkflow.order(meaningOfLife).andThen(writeBook);
guide.start();

Handling Async calls

Sometimes(probably all the time) you will need to do something async when working with tasks, jWorkflow provides the ability to control the execution of the workflow via a baton that is passed to the task

function procrastinate(previous, baton) {
    //take the baton, this means the next task will not run until you pass the baton
    baton.take();

    window.setTimeout(function() {
        //do some stuff

        //please be nice and always remember to pass the baton!
        baton.pass();
    }, 1000);
}

the start method provides a callback to execute when the workflow is finished:

order.start(function() {
    console.log("dude!, your car is behind that mail truck!");
}

you can also pass context to use for the callback:

order.start(function() {
    //do stuff
}, transfunctioner);

Contributers:

Gord Tanner <gord@tinyhippos.com>
Something went wrong with that request. Please try again.