Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #2 from endeepak/master

callbacks generated by MULTI can include an identifier which can subsequently be used to distinguish results
  • Loading branch information...
commit d11311b3bc1ce8c4e467c7e978908667b4a792dd 2 parents f9c5184 + 99a05fa
Will Conant authored

Showing 4 changed files with 60 additions and 2 deletions. Show diff stats Hide diff stats

  1. +15 0 README.md
  2. +8 0 examples/keystore.js
  3. +7 1 flow.js
  4. +30 1 tests.js
15 README.md
Source Rendered
@@ -173,6 +173,21 @@ Here is an example of `this.MULTI()` in action (repeated from the overview):
173 173 }
174 174 );
175 175
  176 + 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.
  177 +
  178 + Example:
  179 +
  180 + flow.exec(
  181 + function() {
  182 + dbGet('userIdOf:bobvance', this.MULTI('bob'));
  183 + dbGet('userIdOf:joohndoe', this.MULTI('john'));
  184 + },function(results) {
  185 + dbSet('user:' + results['bob'] + ':email', 'bobvance@potato.egg');
  186 + dbSet('user:' + results['john'] + ':email', 'joohndoe@potato.egg');
  187 + okWeAreDone();
  188 + }
  189 + );
  190 +
176 191 In many cases, you may simply discard the arguments passed to each of the callbacks generated
177 192 by `this.MULTI()`, but if you need them, they are accessible as an array of `arguments`
178 193 objects passed as the first argument of the next function. Each `arguments` object will be
8 examples/keystore.js
@@ -31,6 +31,14 @@ exports.get = function(key, callback) {
31 31 }, 100);
32 32 }
33 33
  34 +// keystore.exists(key, callback)
  35 +// callback signature: (value)
  36 +exports.exists = function(key, callback) {
  37 + setTimeout(function() {
  38 + if (callback) callback(key in db);
  39 + }, 100);
  40 +}
  41 +
34 42 // keystore.increment(key, incrementBy, callback)
35 43 // callback signature: (error, newValue)
36 44 exports.increment = function(key, incrementBy, callback) {
8 flow.js
@@ -58,11 +58,17 @@
58 58 // MULTI can be used to generate callbacks that must ALL be called before the next step
59 59 // in the flow is executed. Arguments to those callbacks are accumulated, and an array of
60 60 // of those arguments objects is sent as the one argument to the next step in the flow.
61   - flowState.MULTI = function() {
  61 + // @param {String} resultId An identifier to get the result of a multi call.
  62 + flowState.MULTI = function(resultId) {
62 63 flowState.__multiCount += 1;
63 64 return function() {
64 65 flowState.__multiCount -= 1;
65 66 flowState.__multiOutputs.push(arguments);
  67 +
  68 + if (resultId) {
  69 + var result = arguments.length <= 1 ? arguments[0] : arguments
  70 + flowState.__multiOutputs[resultId] = result;
  71 + }
66 72
67 73 if (flowState.__multiCount === 0) {
68 74 var multiOutputs = flowState.__multiOutputs;
31 tests.js
@@ -4,7 +4,7 @@ var keystore = require('./examples/keystore');
4 4
5 5 var flowsComplete = 0;
6 6 setTimeout(function() {
7   - var expected = 3;
  7 + var expected = 5;
8 8 assert.strictEqual(flowsComplete, expected, flowsComplete + "/" + expected +" flows finished");
9 9 }, 1000);
10 10
@@ -34,6 +34,35 @@ flow.exec(
34 34 }
35 35 );
36 36
  37 +// MULTI with result identifier test
  38 +flow.exec(
  39 + function() {
  40 + var db = keystore.getDb();
  41 + db['firstName'] = "Bob"
  42 + db['lastName'] = "Vance"
  43 + keystore.get("firstName", this.MULTI('first-name'));
  44 + keystore.get("lastName", this.MULTI('last-name'));
  45 + },function(results) {
  46 + assert.strictEqual(results['first-name'][1], "Bob", "multi with result identifier test didn't work");
  47 + assert.strictEqual(results['last-name'][1], "Vance", "multi with result identifier test didn't work");
  48 + flowsComplete += 1;
  49 + }
  50 +);
  51 +
  52 +// MULTI with result identifier for single return value test
  53 +flow.exec(
  54 + function() {
  55 + var db = keystore.getDb();
  56 + db['bob'] = "Bob Vance"
  57 + keystore.exists("bob", this.MULTI('bob-exists'));
  58 + keystore.exists("john", this.MULTI('john-exists'));
  59 + },function(results) {
  60 + assert.strictEqual(results['bob-exists'], true, "multi with result identifier for single return value test didn't work");
  61 + assert.strictEqual(results['john-exists'], false, "multi with result identifier for single return value test didn't work");
  62 + flowsComplete += 1;
  63 + }
  64 +);
  65 +
37 66 // serialForEach test
38 67 var valueSequence = [];
39 68 flow.serialForEach([1, 2, 3, 4], function(val) {

0 comments on commit d11311b

Please sign in to comment.
Something went wrong with that request. Please try again.