[Pkg-javascript-commits] [node-async] 378/480: only call cargo.drain once each time it's drained

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:44 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 489c6c3208b24eb4568640237fa6de0a656eba64
Author: Justin York <justin at genealogysystems.com>
Date:   Mon Feb 24 18:20:03 2014 +0000

    only call cargo.drain once each time it's drained
---
 lib/async.js       |  5 ++++-
 test/test-async.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/lib/async.js b/lib/async.js
index 1eebb15..dcbc511 100755
--- a/lib/async.js
+++ b/lib/async.js
@@ -751,6 +751,7 @@
             saturated: null,
             empty: null,
             drain: null,
+            drained: true,
             push: function (data, callback) {
                 if(data.constructor !== Array) {
                     data = [data];
@@ -760,6 +761,7 @@
                         data: task,
                         callback: typeof callback === 'function' ? callback : null
                     });
+                    cargo.drained = false;
                     if (cargo.saturated && tasks.length === payload) {
                         cargo.saturated();
                     }
@@ -769,7 +771,8 @@
             process: function process() {
                 if (working) return;
                 if (tasks.length === 0) {
-                    if(cargo.drain) cargo.drain();
+                    if(cargo.drain && !cargo.drained) cargo.drain();
+                    cargo.drained = true;
                     return;
                 }
 
diff --git a/test/test-async.js b/test/test-async.js
index ff401e7..fd6fe46 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2195,6 +2195,53 @@ exports['cargo bulk task'] = function (test) {
     }, 800);
 };
 
+exports['cargo drain once'] = function (test) {
+   
+   var c = async.cargo(function (tasks, callback) {
+      callback();
+    }, 3);
+    
+    var drainCounter = 0;
+    c.drain = function () {
+      drainCounter++;
+    }
+    
+    for(var i = 0; i < 10; i++){
+      c.push(i);
+    }
+    
+    setTimeout(function(){
+      test.equal(drainCounter, 1);
+      test.done();
+    }, 500);
+};
+
+exports['cargo drain twice'] = function (test) {
+    
+    var c = async.cargo(function (tasks, callback) {
+      callback();
+    }, 3);
+    
+    var loadCargo = function(){
+      for(var i = 0; i < 10; i++){
+        c.push(i);
+      }
+    };
+    
+    var drainCounter = 0;
+    c.drain = function () {
+      drainCounter++;
+    }
+
+    loadCargo();
+    setTimeout(loadCargo, 500);
+
+    setTimeout(function(){
+      test.equal(drainCounter, 2);
+      test.done();
+    }, 1000);
+};
+
 exports['memoize'] = function (test) {
     test.expect(4);
     var call_order = [];

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