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();
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);
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();
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); }
If you want to pass a return value to the next task you can pass it along with the baton.
NOTE: if you did take the baton, the return value from your function will NOT be passed to the next task:
function awesometown(previous, baton) { baton.take(); window.setTimeout(function() { //do stuff baton.pass(420); //This value will be passed to the next task }, 100); return 50; // this will NOT be passed to the next function since you took the baton. }
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);
Gord Tanner <gord@tinyhippos.com>