Skip to content

Commit

Permalink
MULTI can take identfier to get the result of a function in the last …
Browse files Browse the repository at this point in the history
…step
  • Loading branch information
endeepak committed May 7, 2011
1 parent 01d71f1 commit 99a05fa
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
16 changes: 7 additions & 9 deletions README.md
Expand Up @@ -173,20 +173,18 @@ Here is an example of `this.MULTI()` in action (repeated from the overview):
}
);

You can process the arguments returned from function by passing a callback to MULTI. The next step in the flow is executed only after the callback passed to MULTI is invoked.
You can identify the results of a function by passing a result identifier to MULTI. The results of a function can retrieved using this key in the final step. The result will be a single value if callback receives 0 or 1 argument, otherwise it will be an array of arguments passed to the callback.

Example:

flow.exec(
function() {
dbGet('userIdOf:bobvance', this.MULTI(function(userId){
dbSet('user:' + userId + ':email', 'bobvance@potato.egg');
}));
dbGet('userIdOf:joohndoe', this.MULTI(function(userId){
dbSet('user:' + userId + ':email', 'joohndoe@potato.egg');
});
},function(argsArray) {
okWeAreDone()
dbGet('userIdOf:bobvance', this.MULTI('bob'));
dbGet('userIdOf:joohndoe', this.MULTI('john'));
},function(results) {
dbSet('user:' + results['bob'] + ':email', 'bobvance@potato.egg');
dbSet('user:' + results['john'] + ':email', 'joohndoe@potato.egg');
okWeAreDone();
}
);

Expand Down
8 changes: 8 additions & 0 deletions examples/keystore.js
Expand Up @@ -31,6 +31,14 @@ exports.get = function(key, callback) {
}, 100);
}

// keystore.exists(key, callback)
// callback signature: (value)
exports.exists = function(key, callback) {
setTimeout(function() {
if (callback) callback(key in db);
}, 100);
}

// keystore.increment(key, incrementBy, callback)
// callback signature: (error, newValue)
exports.increment = function(key, incrementBy, callback) {
Expand Down
9 changes: 7 additions & 2 deletions flow.js
Expand Up @@ -58,12 +58,17 @@
// MULTI can be used to generate callbacks that must ALL be called before the next step
// in the flow is executed. Arguments to those callbacks are accumulated, and an array of
// of those arguments objects is sent as the one argument to the next step in the flow.
flowState.MULTI = function(callback) {
// @param {String} resultId An identifier to get the result of a multi call.
flowState.MULTI = function(resultId) {
flowState.__multiCount += 1;
return function() {
flowState.__multiCount -= 1;
flowState.__multiOutputs.push(arguments);
if (callback) callback.apply(this, arguments);

if (resultId) {
var result = arguments.length <= 1 ? arguments[0] : arguments
flowState.__multiOutputs[resultId] = result;
}

if (flowState.__multiCount === 0) {
var multiOutputs = flowState.__multiOutputs;
Expand Down
37 changes: 23 additions & 14 deletions tests.js
Expand Up @@ -4,7 +4,7 @@ var keystore = require('./examples/keystore');

var flowsComplete = 0;
setTimeout(function() {
var expected = 4;
var expected = 5;
assert.strictEqual(flowsComplete, expected, flowsComplete + "/" + expected +" flows finished");
}, 1000);

Expand Down Expand Up @@ -34,22 +34,31 @@ flow.exec(
}
);

// MULTI with callback test
var fetchedFirstName, fetchedLastName;
// MULTI with result identifier test
flow.exec(
function() {
var db = keystore.getDb();
db.firstName = "Bob"
db.lastName = "Vance"
keystore.get("firstName", this.MULTI(function(error, value){
fetchedFirstName = value;
}));
keystore.get("lastName", this.MULTI(function(error, value){
fetchedLastName = value;
}));
},function() {
assert.strictEqual(fetchedFirstName, "Bob", "multi with callback test didn't work");
assert.strictEqual(fetchedLastName, "Vance", "multi with callback test didn't work");
db['firstName'] = "Bob"
db['lastName'] = "Vance"
keystore.get("firstName", this.MULTI('first-name'));
keystore.get("lastName", this.MULTI('last-name'));
},function(results) {
assert.strictEqual(results['first-name'][1], "Bob", "multi with result identifier test didn't work");
assert.strictEqual(results['last-name'][1], "Vance", "multi with result identifier test didn't work");
flowsComplete += 1;
}
);

// MULTI with result identifier for single return value test
flow.exec(
function() {
var db = keystore.getDb();
db['bob'] = "Bob Vance"
keystore.exists("bob", this.MULTI('bob-exists'));
keystore.exists("john", this.MULTI('john-exists'));
},function(results) {
assert.strictEqual(results['bob-exists'], true, "multi with result identifier for single return value test didn't work");
assert.strictEqual(results['john-exists'], false, "multi with result identifier for single return value test didn't work");
flowsComplete += 1;
}
);
Expand Down

0 comments on commit 99a05fa

Please sign in to comment.