[Pkg-javascript-commits] [node-async] 02/480: added existing code and tests

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:06 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 44072b8570ae6a0c199ed4ced4dff728809127cd
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date:   Thu May 13 18:51:20 2010 +0100

    added existing code and tests
---
 lib/async.js       | 61 +++++++++++++++++++++++++++++++++++++++++
 test/test-async.js | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 141 insertions(+)

diff --git a/lib/async.js b/lib/async.js
new file mode 100644
index 0000000..d7b2efd
--- /dev/null
+++ b/lib/async.js
@@ -0,0 +1,61 @@
+var events = require('events');
+
+
+exports.requires = function(requirements, fn){
+    return {requires: requirements, run: fn};
+};
+
+exports.auto = function(tasks, callback){
+    var keys = Object.keys(tasks);
+    if(!keys.length) return callback(null);
+
+    var completed = [];
+    var emitter = new events.EventEmitter();
+    emitter.addListener('taskComplete', function(){
+        if(completed.length == keys.length){
+            callback(null);
+        }
+    });
+
+    keys.forEach(function(k){
+        var task = tasks[k];
+        if(task instanceof Function){
+            task = {run: task};
+        }
+        var taskEnv = {
+            done: function(){
+                completed.push(k);
+                emitter.emit('taskComplete');
+            }
+        };
+        var requires = task.requires || [];
+        var ready = function(){
+            return requires.reduce(function(a,x){
+                return (a && completed.indexOf(x) != -1);
+            }, true);
+        };
+        if(ready()){
+            task.run(taskEnv);
+        }
+        else {
+            var listener = function(){
+                if(ready()){
+                    emitter.removeListener('taskComplete', listener);
+                    task.run(taskEnv);
+                }
+            };
+            emitter.addListener('taskComplete', listener);
+        }
+    });
+};
+
+exports.waterfall = function(arr){
+    var i = 0;
+    (function(){
+        if(i < arr.length){
+            var args = Array.prototype.slice.call(arguments);
+            arr[i].apply(null, args.concat(arguments.callee));
+            i++;
+        }
+    })();
+};
diff --git a/test/test-async.js b/test/test-async.js
new file mode 100644
index 0000000..d6347b1
--- /dev/null
+++ b/test/test-async.js
@@ -0,0 +1,80 @@
+var async = require('async');
+
+
+exports.testRequires = function(test){
+    var fn = function(){return 'test';};
+    test.same(
+        async.requires(['task1','task2'], fn),
+        {requires: ['task1','task2'], run: fn}
+    );
+    test.done();
+};
+
+exports.testAuto = function(test){
+    var callOrder = [];
+    var testdata = [{test: 'test'}];
+    async.auto({
+        task1: {
+            requires: ['task2'],
+            run: function(task){
+                setTimeout(function(){
+                    callOrder.push('task1');
+                    task.done();
+                }, 100);
+            }
+        },
+        task2: function(task){
+            setTimeout(function(){
+                callOrder.push('task2');
+                task.done();
+            }, 200);
+        },
+        task3: {
+            requires: ['task2'],
+            run: function(task){
+                callOrder.push('task3');
+                task.done();
+            }
+        },
+        task4: {
+            requires: ['task1', 'task2'],
+            run: function(task){
+                callOrder.push('task4');
+                task.done();
+            }
+        }
+    },
+    function(err){
+        test.same(callOrder, ['task2','task3','task1','task4']);
+        test.done();
+    });
+};
+
+exports.testWaterfall = function(test){
+    test.expect(6);
+    var call_order = [];
+    async.waterfall([
+        function(callback){
+            call_order.push('fn1');
+            process.nextTick(function(){callback('one', 'two');});
+        },
+        function(arg1, arg2, callback){
+            call_order.push('fn2');
+            test.equals(arg1, 'one');
+            test.equals(arg2, 'two');
+            setTimeout(function(){callback(arg1, arg2, 'three');}, 100);
+        },
+        function(arg1, arg2, arg3, callback){
+            call_order.push('fn3');
+            test.equals(arg1, 'one');
+            test.equals(arg2, 'two');
+            test.equals(arg3, 'three');
+            process.nextTick(function(){callback('four');});
+        },
+        function(arg4, callback){
+            call_order.push('fn4');
+            test.same(call_order, ['fn1','fn2','fn3','fn4']);
+            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