[Pkg-javascript-commits] [node-async] 10/480: added iterator method

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:07 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository node-async.

commit 4930440b1ff75d97b00322fe3f371bad7985e6c1
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Wed May 19 13:03:02 2010 +0100

    added iterator method
---
 lib/async.js       | 55 +++++++++++++++++++++++++++++++++---------------------
 test/test-async.js | 49 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 83 insertions(+), 21 deletions(-)

diff --git a/lib/async.js b/lib/async.js
index faac869..3102bb3 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -50,18 +50,15 @@ exports.auto = function(tasks, callback){
 };
 
 exports.waterfall = function(arr){
-    var makeCallback = function(index){
-        if(index < arr.length){
-            return function(){
-                var args = Array.prototype.slice.call(arguments);
-                if(index < arr.length-1){
-                    args = args.concat(makeCallback(index+1));
-                }
-                process.nextTick(function(){arr[index].apply(null, args);});
-            }
-        }
+    var wrapIterator = function(iterator){
+        return function(){
+            var args = Array.prototype.slice.call(arguments);
+            var next = iterator.next();
+            if(next) args.push(wrapIterator(next));
+            process.nextTick(function(){iterator.apply(null, args)});
+        };
     };
-    makeCallback(0)();
+    wrapIterator(exports.iterator(arr))();
 };
 
 exports.parallel = function(arr, callback){
@@ -79,17 +76,33 @@ exports.parallel = function(arr, callback){
 
 exports.series = function(arr, callback){
     var results = [];
-    var makeCallback = function(index){
+    var saveArgs = function(fn){
         return function(){
-            if(index > 0){
-                var args = Array.prototype.slice.call(arguments);
-                results.push((args.length > 1) ? args: args[0]);
-            }
-            if(index < arr.length){
-                arr[index](makeCallback(index+1));
-            }
-            else callback(results);
+            var args = Array.prototype.slice.call(arguments);
+            results.push((args.length > 1) ? args: args[0]);
+            fn.apply(null, args);
+        }
+    };
+    var wrapIterator = function(iterator){
+        return saveArgs(function(){
+            var next = iterator.next();
+            if(next) iterator(wrapIterator(iterator.next()));
+            else iterator(saveArgs(function(){callback(results.slice(1));}));
+        });
+    };
+    wrapIterator(exports.iterator(arr))();
+};
+
+exports.iterator = function(arr){
+    var makeCallback = function(index){
+        var fn = function(){
+            arr[index].apply(null, arguments);
+            return fn.next();
+        }
+        fn.next = function(){
+            return (index < arr.length-1)? makeCallback(index+1): undefined;
         }
+        return fn;
     };
-    makeCallback(0)();
+    return makeCallback(0);
 };
diff --git a/test/test-async.js b/test/test-async.js
index 1451190..0f3321a 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -164,3 +164,52 @@ exports.testSeries = function(test){
         test.done();
     });
 };
+
+exports.testIterator = function(test){
+    var call_order = [];
+    var iterator = async.iterator([
+        function(){call_order.push(1);},
+        function(arg1){
+            test.equals(arg1, 'arg1');
+            call_order.push(2);
+        },
+        function(arg1, arg2){
+            test.equals(arg1, 'arg1');
+            test.equals(arg2, 'arg2');
+            call_order.push(3);
+        },
+    ]);
+    iterator();
+    test.same(call_order, [1]);
+    var iterator2 = iterator();
+    test.same(call_order, [1,1]);
+    var iterator3 = iterator2('arg1');
+    test.same(call_order, [1,1,2]);
+    var iterator4 = iterator3('arg1', 'arg2');
+    test.same(call_order, [1,1,2,3]);
+    test.equals(iterator4, undefined);
+    test.done();
+};
+
+exports.testIteratorNext = function(test){
+    var call_order = [];
+    var iterator = async.iterator([
+        function(){call_order.push(1);},
+        function(arg1){
+            test.equals(arg1, 'arg1');
+            call_order.push(2);
+        },
+        function(arg1, arg2){
+            test.equals(arg1, 'arg1');
+            test.equals(arg2, 'arg2');
+            call_order.push(3);
+        },
+    ]);
+    var fn = iterator.next();
+    var iterator2 = fn('arg1');
+    test.same(call_order, [2]);
+    iterator2('arg1','arg2');
+    test.same(call_order, [2,3]);
+    test.equals(iterator2.next(), undefined);
+    test.done();
+};

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-async.git



More information about the Pkg-javascript-commits mailing list