[Pkg-javascript-commits] [node-async] 159/480: Imported Upstream version 0.1.17

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:22 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 b89c138546b03477c7f2359ecc3006408b2af9ad
Author: Jonas Smedegaard <dr at jones.dk>
Date:   Mon Feb 27 09:54:25 2012 +0100

    Imported Upstream version 0.1.17
---
 .gitignore         |   1 +
 .npmignore         |   4 +
 Makefile           |  12 ++-
 README.md          |  31 +++++--
 dist/async.min.js  |   2 +-
 lib/async.js       |  36 ++++----
 package.json       |  14 ++-
 test/test-async.js | 265 +++++++++++++++++++++++++++++------------------------
 8 files changed, 214 insertions(+), 151 deletions(-)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..3c3629e
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+node_modules
diff --git a/.npmignore b/.npmignore
new file mode 100644
index 0000000..9bdfc97
--- /dev/null
+++ b/.npmignore
@@ -0,0 +1,4 @@
+deps
+dist
+test
+nodelint.cfg
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 00f07ea..bad647c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,21 +1,25 @@
 PACKAGE = asyncjs
 NODEJS = $(if $(shell test -f /usr/bin/nodejs && echo "true"),nodejs,node)
+CWD := $(shell pwd)
+NODEUNIT = $(CWD)/node_modules/nodeunit/bin/nodeunit
+UGLIFY = $(CWD)/node_modules/uglify-js/bin/uglifyjs
+NODELINT = $(CWD)/node_modules/nodelint/nodelint
 
 BUILDDIR = dist
 
-all: build
+all: clean test build
 
 build: $(wildcard  lib/*.js)
 	mkdir -p $(BUILDDIR)
-	uglifyjs lib/async.js > $(BUILDDIR)/async.min.js
+	$(UGLIFY) lib/async.js > $(BUILDDIR)/async.min.js
 
 test:
-	nodeunit test
+	$(NODEUNIT) test
 
 clean:
 	rm -rf $(BUILDDIR)
 
 lint:
-	nodelint --config nodelint.cfg lib/async.js
+	$(NODELINT) --config nodelint.cfg lib/async.js
 
 .PHONY: test build all
diff --git a/README.md b/README.md
index f3c44ac..0cf7fc9 100644
--- a/README.md
+++ b/README.md
@@ -626,7 +626,7 @@ __Example__
         function (err) {
             // 5 seconds have passed
         }
-    });
+    );
 
 
 ---------------------------------------
@@ -654,8 +654,10 @@ __Arguments__
 
 * tasks - An array of functions to run, each function is passed a callback it
   must call on completion.
-* callback(err) - An optional callback to run once all the functions have
-  completed. This function gets passed any error that may have occurred.
+* callback(err, [results]) - An optional callback to run once all the functions
+  have completed. This will be passed the results of the last task's callback.
+
+
 
 __Example__
 
@@ -670,7 +672,9 @@ __Example__
             // arg1 now equals 'three'
             callback(null, 'done');
         }
-    ]);
+    ], function (err, result) {
+       // result now equals 'done'    
+    });
 
 
 ---------------------------------------
@@ -701,6 +705,7 @@ methods:
   alter the concurrency on-the-fly.
 * push(task, [callback]) - add a new task to the queue, the callback is called
   once the worker has finished processing the task.
+  instead of a single task, an array of tasks can be submitted. the respective callback is used for every task in the list.
 * saturated - a callback that is called when the queue length hits the concurrency and further tasks will be queued
 * empty - a callback that is called when the last item from the queue is given to a worker
 * drain - a callback that is called when the last item from the queue has returned from the worker
@@ -729,6 +734,12 @@ __Example__
         console.log('finished processing bar');
     });
 
+    // add some items to the queue (batch-wise)
+
+    q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function (err) {
+        console.log('finished processing bar');
+    });
+
 
 ---------------------------------------
 
@@ -741,7 +752,7 @@ and each function is run as soon as its requirements are satisfied. If any of
 the functions pass an error to their callback, that function will not complete
 (so any other functions depending on it will not run) and the main callback
 will be called immediately with the error. Functions also receive an object
-containing the results of functions on which they depend.
+containing the results of functions which have completed so far.
 
 __Arguments__
 
@@ -749,8 +760,10 @@ __Arguments__
   requirements, with the function itself the last item in the array. The key
   used for each function or array is used when specifying requirements. The
   syntax is easier to understand by looking at the example.
-* callback(err) - An optional callback which is called when all the tasks have
-  been completed. The callback may receive an error as an argument.
+* callback(err, results) - An optional callback which is called when all the
+  tasks have been completed. The callback will receive an error as an argument
+  if any tasks pass an error to their callback. If all tasks complete
+  successfully, it will receive an object containing their results.
 
 __Example__
 
@@ -791,14 +804,14 @@ series functions would look like this:
                 // once there is some data and the directory exists,
                 // write the data to a file in the directory
             },
-            email_link: ['write_file', function(callback){
+            email_link: function(callback){
                 // once the file is written let's email a link to it...
             }
         ]);
     });
 
 For a complicated series of async tasks using the auto function makes adding
-new tasks much easier and makes the code more readable. 
+new tasks much easier and makes the code more readable.
 
 
 ---------------------------------------
diff --git a/dist/async.min.js b/dist/async.min.js
index e4c898b..53e457c 100644
--- a/dist/async.min.js
+++ b/dist/async.min.js
@@ -1 +1 @@
-/*global setTimeout: false, console: false */(function(){var a={},b=this,c=b.async;typeof module!="undefined"&&module.exports?module.exports=a:b.async=a,a.noConflict=function(){return b.async=c,a};var d=function(a,b){if(a.forEach)return a.forEach(b);for(var c=0;c<a.length;c+=1)b(a[c],c,a)},e=function(a,b){if(a.map)return a.map(b);var c=[];return d(a,function(a,d,e){c.push(b(a,d,e))}),c},f=function(a,b,c){return a.reduce?a.reduce(b,c):(d(a,function(a,d,e){c=b(c,a,d,e)}),c)},g=function(a){ [...]
\ No newline at end of file
+/*global setTimeout: false, console: false */(function(){var a={},b=this,c=b.async;typeof module!="undefined"&&module.exports?module.exports=a:b.async=a,a.noConflict=function(){return b.async=c,a};var d=function(a,b){if(a.forEach)return a.forEach(b);for(var c=0;c<a.length;c+=1)b(a[c],c,a)},e=function(a,b){if(a.map)return a.map(b);var c=[];return d(a,function(a,d,e){c.push(b(a,d,e))}),c},f=function(a,b,c){return a.reduce?a.reduce(b,c):(d(a,function(a,d,e){c=b(c,a,d,e)}),c)},g=function(a){ [...]
\ No newline at end of file
diff --git a/lib/async.js b/lib/async.js
index c862008..52276d6 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -64,18 +64,6 @@
         return keys;
     };
 
-    var _indexOf = function (arr, item) {
-        if (arr.indexOf) {
-            return arr.indexOf(item);
-        }
-        for (var i = 0; i < arr.length; i += 1) {
-            if (arr[i] === item) {
-                return i;
-            }
-        }
-        return -1;
-    };
-
     //// exported async module functions ////
 
     //// nextTick implementation with browser-compatible fallback ////
@@ -89,6 +77,7 @@
     }
 
     async.forEach = function (arr, iterator, callback) {
+        callback = callback || function () {};
         if (!arr.length) {
             return callback();
         }
@@ -110,6 +99,7 @@
     };
 
     async.forEachSeries = function (arr, iterator, callback) {
+        callback = callback || function () {};
         if (!arr.length) {
             return callback();
         }
@@ -135,6 +125,7 @@
     };
     
     async.forEachLimit = function (arr, limit, iterator, callback) {
+        callback = callback || function () {};
         if (!arr.length || limit <= 0) {
             return callback(); 
         }
@@ -376,7 +367,7 @@
             }
         };
         var taskComplete = function () {
-            _forEach(listeners, function (fn) {
+            _forEach(listeners.slice(0), function (fn) {
                 fn();
             });
         };
@@ -384,6 +375,7 @@
         addListener(function () {
             if (_keys(results).length === keys.length) {
                 callback(null, results);
+                callback = function () {};
             }
         });
 
@@ -426,10 +418,10 @@
     };
 
     async.waterfall = function (tasks, callback) {
+        callback = callback || function () {};
         if (!tasks.length) {
             return callback();
         }
-        callback = callback || function () {};
         var wrapIterator = function (iterator) {
             return function (err) {
                 if (err) {
@@ -594,9 +586,19 @@
             empty: null,
             drain: null,
             push: function (data, callback) {
-                q.tasks.push({data: data, callback: callback});
-                if(q.saturated && q.tasks.length == concurrency) q.saturated();
-                async.nextTick(q.process);
+                if(data.constructor !== Array) {
+                    data = [data];
+                }
+                _forEach(data, function(task) {
+                    q.tasks.push({
+                        data: task,
+                        callback: typeof callback === 'function' ? callback : null
+                    });
+                    if (q.saturated && q.tasks.length == concurrency) {
+                        q.saturated();
+                    }
+                    async.nextTick(q.process);
+                });
             },
             process: function () {
                 if (workers < q.concurrency && q.tasks.length) {
diff --git a/package.json b/package.json
index 6198943..2e53020 100644
--- a/package.json
+++ b/package.json
@@ -2,7 +2,7 @@
 , "description": "Higher-order functions and common patterns for asynchronous code"
 , "main": "./index"
 , "author": "Caolan McMahon"
-, "version": "0.1.15"
+, "version": "0.1.17"
 , "repository" :
   { "type" : "git"
   , "url" : "http://github.com/caolan/async.git"
@@ -13,4 +13,16 @@
     , "url" : "http://github.com/caolan/async/raw/master/LICENSE"
     }
   ]
+, "dependencies":
+  { "uglify-js": "1.2.x"
+  }
+, "devDependencies":
+    { "nodeunit": ">0.0.0"
+    , "nodelint": ">0.0.0"
+    }
+, "scripts":
+  { "preinstall": "make clean"
+  , "install": "make build"
+  , "test": "make test"
+  }
 }
diff --git a/test/test-async.js b/test/test-async.js
index d3eeddc..ff4b749 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -1,5 +1,70 @@
 var async = require('../lib/async');
 
+if (!Function.prototype.bind) {
+    Function.prototype.bind = function (thisArg) {
+        var args = Array.prototype.slice.call(arguments, 1);
+        var self = this;
+        return function () {
+            self.apply(thisArg, args.concat(Array.prototype.slice.call(arguments)));
+        }
+    };
+}
+
+function forEachIterator(args, x, callback) {
+    setTimeout(function(){
+        args.push(x);
+        callback();
+    }, x*25);
+}
+
+function mapIterator(call_order, x, callback) {
+    setTimeout(function(){
+        call_order.push(x);
+        callback(null, x*2);
+    }, x*25);
+}
+
+function filterIterator(x, callback) {
+    setTimeout(function(){
+        callback(x % 2);
+    }, x*25);
+}
+
+function detectIterator(call_order, x, callback) {
+    setTimeout(function(){
+        call_order.push(x);
+        callback(x == 2);
+    }, x*25);
+}
+
+function forEachNoCallbackIterator(test, x, callback) {
+    test.equal(x, 1);
+    callback();
+    test.done();
+}
+
+function getFunctionsObject(call_order) {
+    return {
+        one: function(callback){
+            setTimeout(function(){
+                call_order.push(1);
+                callback(null, 1);
+            }, 25);
+        },
+        two: function(callback){
+            setTimeout(function(){
+                call_order.push(2);
+                callback(null, 2);
+            }, 50);
+        },
+        three: function(callback){
+            setTimeout(function(){
+                call_order.push(3);
+                callback(null, 3,3);
+            }, 15);
+        }
+    };
+}
 
 exports['auto'] = function(test){
     var callOrder = [];
@@ -101,6 +166,16 @@ exports['auto no callback'] = function(test){
     });
 };
 
+// Issue 24 on github: https://github.com/caolan/async/issues#issue/24
+// Issue 76 on github: https://github.com/caolan/async/issues#issue/76
+exports['auto removeListener has side effect on loop iterator'] = function(test) {
+    async.auto({
+        task1: ['task3', function(callback) { test.done() }],
+        task2: ['task3', function(callback) { /* by design: DON'T call callback */ }],
+        task3: function(callback) { callback(); }
+    });
+};
+
 exports['waterfall'] = function(test){
     test.expect(6);
     var call_order = [];
@@ -272,27 +347,7 @@ exports['parallel no callback'] = function(test){
 
 exports['parallel object'] = function(test){
     var call_order = [];
-    async.parallel({
-        one: function(callback){
-            setTimeout(function(){
-                call_order.push(1);
-                callback(null, 1);
-            }, 25);
-        },
-        two: function(callback){
-            setTimeout(function(){
-                call_order.push(2);
-                callback(null, 2);
-            }, 50);
-        },
-        three: function(callback){
-            setTimeout(function(){
-                call_order.push(3);
-                callback(null, 3,3);
-            }, 15);
-        }
-    },
-    function(err, results){
+    async.parallel(getFunctionsObject(call_order), function(err, results){
         test.equals(err, null);
         test.same(call_order, [3,1,2]);
         test.same(results, {
@@ -368,27 +423,7 @@ exports['series no callback'] = function(test){
 
 exports['series object'] = function(test){
     var call_order = [];
-    async.series({
-        one: function(callback){
-            setTimeout(function(){
-                call_order.push(1);
-                callback(null, 1);
-            }, 25);
-        },
-        two: function(callback){
-            setTimeout(function(){
-                call_order.push(2);
-                callback(null, 2);
-            }, 50);
-        },
-        three: function(callback){
-            setTimeout(function(){
-                call_order.push(3);
-                callback(null, 3,3);
-            }, 15);
-        }
-    },
-    function(err, results){
+    async.series(getFunctionsObject(call_order), function(err, results){
         test.equals(err, null);
         test.same(results, {
             one: 1,
@@ -458,12 +493,7 @@ exports['iterator.next'] = function(test){
 
 exports['forEach'] = function(test){
     var args = [];
-    async.forEach([1,3,2], function(x, callback){
-        setTimeout(function(){
-            args.push(x);
-            callback();
-        }, x*25);
-    }, function(err){
+    async.forEach([1,3,2], forEachIterator.bind(this, args), function(err){
         test.same(args, [1,2,3]);
         test.done();
     });
@@ -490,14 +520,13 @@ exports['forEach error'] = function(test){
     setTimeout(test.done, 50);
 };
 
+exports['forEach no callback'] = function(test){
+    async.forEach([1], forEachNoCallbackIterator.bind(this, test));
+};
+
 exports['forEachSeries'] = function(test){
     var args = [];
-    async.forEachSeries([1,3,2], function(x, callback){
-        setTimeout(function(){
-            args.push(x);
-            callback();
-        }, x*25);
-    }, function(err){
+    async.forEachSeries([1,3,2], forEachIterator.bind(this, args), function(err){
         test.same(args, [1,3,2]);
         test.done();
     });
@@ -527,6 +556,10 @@ exports['forEachSeries error'] = function(test){
     setTimeout(test.done, 50);
 };
 
+exports['forEachSeries no callback'] = function(test){
+    async.forEachSeries([1], forEachNoCallbackIterator.bind(this, test));
+};
+
 exports['forEachLimit'] = function(test){
     var args = [];
     var arr = [0,1,2,3,4,5,6,7,8,9];
@@ -555,12 +588,7 @@ exports['forEachLimit empty array'] = function(test){
 exports['forEachLimit limit exceeds size'] = function(test){
     var args = [];
     var arr = [0,1,2,3,4,5,6,7,8,9];
-    async.forEachLimit(arr, 20, function(x,callback){
-        setTimeout(function(){
-            args.push(x);
-            callback();
-        }, x*5);
-    }, function(err){
+    async.forEachLimit(arr, 20, forEachIterator.bind(this, args), function(err){
         test.same(args, arr);
         test.done();
     });
@@ -569,12 +597,7 @@ exports['forEachLimit limit exceeds size'] = function(test){
 exports['forEachLimit limit equal size'] = function(test){
     var args = [];
     var arr = [0,1,2,3,4,5,6,7,8,9];
-    async.forEachLimit(arr, 10, function(x,callback){
-        setTimeout(function(){
-            args.push(x);
-            callback();
-        }, x*5);
-    }, function(err){
+    async.forEachLimit(arr, 10, forEachIterator.bind(this, args), function(err){
         test.same(args, arr);
         test.done();
     });
@@ -608,14 +631,13 @@ exports['forEachLimit error'] = function(test){
     setTimeout(test.done, 25);
 };
 
+exports['forEachLimit no callback'] = function(test){
+    async.forEachLimit([1], 1, forEachNoCallbackIterator.bind(this, test));
+};
+
 exports['map'] = function(test){
     var call_order = [];
-    async.map([1,3,2], function(x, callback){
-        setTimeout(function(){
-            call_order.push(x);
-            callback(null, x*2);
-        }, x*25);
-    }, function(err, results){
+    async.map([1,3,2], mapIterator.bind(this, call_order), function(err, results){
         test.same(call_order, [1,2,3]);
         test.same(results, [2,6,4]);
         test.done();
@@ -645,12 +667,7 @@ exports['map error'] = function(test){
 
 exports['mapSeries'] = function(test){
     var call_order = [];
-    async.mapSeries([1,3,2], function(x, callback){
-        setTimeout(function(){
-            call_order.push(x);
-            callback(null, x*2);
-        }, x*25);
-    }, function(err, results){
+    async.mapSeries([1,3,2], mapIterator.bind(this, call_order), function(err, results){
         test.same(call_order, [1,3,2]);
         test.same(results, [2,6,4]);
         test.done();
@@ -728,9 +745,7 @@ exports['foldr alias'] = function(test){
 };
 
 exports['filter'] = function(test){
-    async.filter([3,1,2], function(x, callback){
-        setTimeout(function(){callback(x % 2);}, x*25);
-    }, function(results){
+    async.filter([3,1,2], filterIterator, function(results){
         test.same(results, [3,1]);
         test.done();
     });
@@ -748,9 +763,7 @@ exports['filter original untouched'] = function(test){
 };
 
 exports['filterSeries'] = function(test){
-    async.filterSeries([3,1,2], function(x, callback){
-        setTimeout(function(){callback(x % 2);}, x*25);
-    }, function(results){
+    async.filterSeries([3,1,2], filterIterator, function(results){
         test.same(results, [3,1]);
         test.done();
     });
@@ -767,9 +780,7 @@ exports['selectSeries alias'] = function(test){
 };
 
 exports['reject'] = function(test){
-    async.reject([3,1,2], function(x, callback){
-        setTimeout(function(){callback(x % 2);}, x*25);
-    }, function(results){
+    async.reject([3,1,2], filterIterator, function(results){
         test.same(results, [2]);
         test.done();
     });
@@ -787,9 +798,7 @@ exports['reject original untouched'] = function(test){
 };
 
 exports['rejectSeries'] = function(test){
-    async.rejectSeries([3,1,2], function(x, callback){
-        setTimeout(function(){callback(x % 2);}, x*25);
-    }, function(results){
+    async.rejectSeries([3,1,2], filterIterator, function(results){
         test.same(results, [2]);
         test.done();
     });
@@ -875,12 +884,7 @@ exports['all alias'] = function(test){
 
 exports['detect'] = function(test){
     var call_order = [];
-    async.detect([3,2,1], function(x, callback){
-        setTimeout(function(){
-            call_order.push(x);
-            callback(x == 2);
-        }, x*25);
-    }, function(result){
+    async.detect([3,2,1], detectIterator.bind(this, call_order), function(result){
         call_order.push('callback');
         test.equals(result, 2);
     });
@@ -892,12 +896,7 @@ exports['detect'] = function(test){
 
 exports['detect - mulitple matches'] = function(test){
     var call_order = [];
-    async.detect([3,2,2,1,2], function(x, callback){
-        setTimeout(function(){
-            call_order.push(x);
-            callback(x == 2);
-        }, x*25);
-    }, function(result){
+    async.detect([3,2,2,1,2], detectIterator.bind(this, call_order), function(result){
         call_order.push('callback');
         test.equals(result, 2);
     });
@@ -909,12 +908,7 @@ exports['detect - mulitple matches'] = function(test){
 
 exports['detectSeries'] = function(test){
     var call_order = [];
-    async.detectSeries([3,2,1], function(x, callback){
-        setTimeout(function(){
-            call_order.push(x);
-            callback(x == 2);
-        }, x*25);
-    }, function(result){
+    async.detectSeries([3,2,1], detectIterator.bind(this, call_order), function(result){
         call_order.push('callback');
         test.equals(result, 2);
     });
@@ -926,12 +920,7 @@ exports['detectSeries'] = function(test){
 
 exports['detectSeries - multiple matches'] = function(test){
     var call_order = [];
-    async.detectSeries([3,2,2,1,2], function(x, callback){
-        setTimeout(function(){
-            call_order.push(x);
-            callback(x == 2);
-        }, x*25);
-    }, function(result){
+    async.detectSeries([3,2,2,1,2], detectIterator.bind(this, call_order), function(result){
         call_order.push('callback');
         test.equals(result, 2);
     });
@@ -1072,7 +1061,9 @@ exports['noConflict - node only'] = function(test){
         var filename = __dirname + '/../lib/async.js';
         fs.readFile(filename, function(err, content){
             if(err) return test.done();
-            var Script = process.binding('evals').Script;
+
+            // Script -> NodeScript in node v0.6.x
+            var Script = process.binding('evals').Script || process.binding('evals').NodeScript;
 
             var s = new Script(content, filename);
             var s2 = new Script(
@@ -1207,7 +1198,7 @@ exports['whilst'] = function (test) {
 
 exports['queue'] = function (test) {
     var call_order = [],
-        delays = [40,20,60,20];
+        delays = [160,80,240,80];
 
     // worker1: --1-4
     // worker2: -2---3
@@ -1257,7 +1248,7 @@ exports['queue'] = function (test) {
         test.equal(q.concurrency, 2);
         test.equal(q.length(), 0);
         test.done();
-    }, 200);
+    }, 800);
 };
 
 exports['queue changing concurrency'] = function (test) {
@@ -1317,7 +1308,7 @@ exports['queue changing concurrency'] = function (test) {
 
 exports['queue push without callback'] = function (test) {
     var call_order = [],
-        delays = [40,20,60,20];
+        delays = [160,80,240,80];
 
     // worker1: --1-4
     // worker2: -2---3
@@ -1343,7 +1334,43 @@ exports['queue push without callback'] = function (test) {
             'process 3'
         ]);
         test.done();
-    }, 200);
+    }, 800);
+};
+
+exports['queue bulk task'] = function (test) {
+    var call_order = [],
+        delays = [160,80,240,80];
+
+    // worker1: --1-4
+    // worker2: -2---3
+    // order of completion: 2,1,4,3
+
+    var q = async.queue(function (task, callback) {
+        setTimeout(function () {
+            call_order.push('process ' + task);
+            callback('error', task);
+        }, delays.splice(0,1)[0]);
+    }, 2);
+
+    q.push( [1,2,3,4], function (err, arg) {
+        test.equal(err, 'error');
+        call_order.push('callback ' + arg);
+    });
+
+    test.equal(q.length(), 4);
+    test.equal(q.concurrency, 2);
+
+    setTimeout(function () {
+        test.same(call_order, [
+            'process 2', 'callback 2',
+            'process 1', 'callback 1',
+            'process 4', 'callback 4',
+            'process 3', 'callback 3'
+        ]);
+        test.equal(q.concurrency, 2);
+        test.equal(q.length(), 0);
+        test.done();
+    }, 800);
 };
 
 exports['memoize'] = function (test) {

-- 
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