Permalink
Browse files

v1.1 - added callback data

  • Loading branch information...
jgallen23 committed Jul 24, 2011
1 parent 8998b6d commit 65da4a2e7a641c765653a1076474f9bf25712202
Showing with 50 additions and 54 deletions.
  1. +2 −2 build.json
  2. +18 −13 resistance.js
  3. +2 −2 resistance.min.js
  4. +1 −1 src/copyright.js
  5. +17 −12 src/resistance.js
  6. +10 −24 tests/test.js
View
@@ -9,7 +9,7 @@
"JSHINT_OPTS": {
"boss": true,
"forin": false,
- "curly": true,
+ "curly": false,
"debug": false,
"devel": false,
"evil": false,
@@ -19,7 +19,7 @@
"white": false,
"indent": 2,
"whitespace": true,
- "asi": true
+ "asi": false
}
}
View
@@ -1,6 +1,6 @@
/*!
* Resistance - A javascript flow controller
- * v1.0.2
+ * v1.1.0
* https://github.com/jgallen23/resistance
* copyright JGA 2011
* MIT License
@@ -9,12 +9,14 @@
!function(obj) {
var runSeries = function(fns, callback) {
- if (fns.length == 0) return callback();
+ if (fns.length === 0) return callback();
var completed = 0;
+ var data = [];
var iterate = function() {
- fns[completed](function() {
+ fns[completed](function(results) {
+ data[completed] = results;
if (++completed == fns.length) {
- if (callback) callback();
+ if (callback) callback(data);
} else {
iterate();
}
@@ -24,18 +26,21 @@
};
var runParallel = function(fns, callback) {
- if (fns.length == 0) return callback();
+ if (fns.length === 0) return callback();
var started = 0;
var completed = 0;
+ var data = [];
var iterate = function() {
- fns[started](function() {
- if (++completed == fns.length) {
- if (callback) callback();
- return;
- }
- });
- if (++started != fns.length)
- iterate();
+ fns[started]((function(i) {
+ return function(results) {
+ data[i] = results;
+ if (++completed == fns.length) {
+ if (callback) callback(data);
+ return;
+ }
+ };
+ })(started));
+ if (++started != fns.length) iterate();
};
iterate();
};
View
@@ -1,8 +1,8 @@
/*!
* Resistance - A javascript flow controller
- * v1.0.2
+ * v1.1.0
* https://github.com/jgallen23/resistance
* copyright JGA 2011
* MIT License
*/
-!function(a){var b=function(a,b){if(a.length==0)return b();var c=0,d=function(){a[c](function(){++c==a.length?b&&b():d()})};d()},c=function(a,b){if(a.length==0)return b();var c=0,d=0,e=function(){a[c](function(){++d==a.length&&b&&b()}),++c!=a.length&&e()};e()},d=a.R;a.R={noConflict:function(){a.R=d;return this},series:b,parallel:c}}(typeof exports=="undefined"?this:exports)
+!function(a){var b=function(a,b){if(a.length===0)return b();var c=0,d=[],e=function(){a[c](function(f){d[c]=f,++c==a.length?b&&b(d):e()})};e()},c=function(a,b){if(a.length===0)return b();var c=0,d=0,e=[],f=function(){a[c](function(c){return function(f){e[c]=f;++d==a.length&&b&&b(e)}}(c)),++c!=a.length&&f()};f()},d=a.R;a.R={noConflict:function(){a.R=d;return this},series:b,parallel:c}}(typeof exports=="undefined"?this:exports)
View
@@ -1,6 +1,6 @@
/*!
* Resistance - A javascript flow controller
- * v1.0.2
+ * v1.1.0
* https://github.com/jgallen23/resistance
* copyright JGA 2011
* MIT License
View
@@ -1,12 +1,14 @@
!function(obj) {
var runSeries = function(fns, callback) {
- if (fns.length == 0) return callback();
+ if (fns.length === 0) return callback();
var completed = 0;
+ var data = [];
var iterate = function() {
- fns[completed](function() {
+ fns[completed](function(results) {
+ data[completed] = results;
if (++completed == fns.length) {
- if (callback) callback();
+ if (callback) callback(data);
} else {
iterate();
}
@@ -16,18 +18,21 @@
};
var runParallel = function(fns, callback) {
- if (fns.length == 0) return callback();
+ if (fns.length === 0) return callback();
var started = 0;
var completed = 0;
+ var data = [];
var iterate = function() {
- fns[started](function() {
- if (++completed == fns.length) {
- if (callback) callback();
- return;
- }
- });
- if (++started != fns.length)
- iterate();
+ fns[started]((function(i) {
+ return function(results) {
+ data[i] = results;
+ if (++completed == fns.length) {
+ if (callback) callback(data);
+ return;
+ }
+ };
+ })(started));
+ if (++started != fns.length) iterate();
};
iterate();
};
View
@@ -1,33 +1,19 @@
var R = require("../src/resistance").R;
-var testA = function(cb) {
- setTimeout(function() {
- console.log("Test A Complete");
- cb();
- }, 500);
-};
-
-var testB = function(cb) {
- setTimeout(function() {
- console.log("Test B Complete");
- cb();
- }, 500);
-};
-
R.series([
- testA,
- testA,
- testA
- ], function() {
- console.log("Series Complete");
+ function(cb) { setTimeout(function() { console.log("Series 1 (500)"); cb(1); }, 500); },
+ function(cb) { setTimeout(function() { console.log("Series 2 (250)"); cb(2); }, 250); },
+ function(cb) { setTimeout(function() { console.log("Series 3 (100)"); cb(3); }, 100); }
+ ], function(data) {
+ console.log("Series Complete", data);
});
R.parallel([
- testB,
- testB,
- testB
- ], function() {
- console.log("Parallel Complete");
+ function(cb) { setTimeout(function() { console.log("Parallel 1 (500)"); cb(1); }, 500); },
+ function(cb) { setTimeout(function() { console.log("Parallel 2 (250)"); cb(2); }, 250); },
+ function(cb) { setTimeout(function() { console.log("Parallel 3 (100)"); cb(3); }, 100); }
+ ], function(data) {
+ console.log("Parallel Complete", data);
});

0 comments on commit 65da4a2

Please sign in to comment.