Skip to content

Commit

Permalink
Add batch callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Baulig committed Mar 17, 2012
1 parent dc84145 commit a324d75
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
26 changes: 26 additions & 0 deletions README.md
Expand Up @@ -29,6 +29,7 @@ Example:
* [Sequential & Parallel](#seqandpar)
* [Sequence Parameters](#parameters)
* [Ordered Parallel Paramters](#parpar)
* [Batch Callbacks](#batch)
* [Unit Tests](#tests)

## <a name="small" />Small
Expand Down Expand Up @@ -90,6 +91,31 @@ Example:
// function2[0] === 'Hello, World!'
});

## <a name="batch" />Batch Callbacks

first(function () {
// first do something
this();
}).
then(function () {
// then do something else
this();
}, function {
// then do something else
this();
}).
then(function () {
// then do something else
this();
}).
whilst(function () {
// whilst doing something else
this();
}, function () {
// whilst doing something else
this();
});

## <a name="tests" />Unit Tests

make test
Expand Down
15 changes: 9 additions & 6 deletions lib/first.js
@@ -1,6 +1,9 @@
/*jshint eqeqeq:true, proto:true, laxcomma:true, undef:true, node:true, expr: true*/

var slice = Array.prototype.slice, isArray = require('util').isArray;
var slice = Array.prototype.slice
, isArray = require('util').isArray
, push = Array.prototype.push
, concat = Array.prototype.concat;

module.exports = function first (f) {
var functions = [f], next = function () {
Expand Down Expand Up @@ -43,16 +46,16 @@ module.exports = function first (f) {
};

var deferred = {
then: function(f) {
functions.push(f);
then: function() {
push.apply(functions, arguments);
return this;
},
whilst: function(f) {
whilst: function() {
var last = functions.length-1;
if (isArray (functions[last])) {
functions[last].push(f);
push.apply(functions[last], arguments);
} else {
functions[last] = [functions[last], f];
functions[last] = concat.apply([functions[last]], arguments);
}
return this;
}
Expand Down

0 comments on commit a324d75

Please sign in to comment.