[Pkg-javascript-commits] [node-async] 373/480: Queue calls drain if empty task list is pushed

Jonas Smedegaard js at moszumanska.debian.org
Fri May 2 08:58:43 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 c2b098bb46c9c17bb901a91073dae461dfad7ca8
Author: Teemu Ikonen <teemu.ikonen at iki.fi>
Date:   Sat Jan 11 12:17:39 2014 +0800

    Queue calls drain if empty task list is pushed
---
 lib/async.js       |  8 ++++++++
 test/test-async.js | 22 ++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/lib/async.js b/lib/async.js
index cb6320d..7a4361f 100755
--- a/lib/async.js
+++ b/lib/async.js
@@ -675,6 +675,14 @@
           if(data.constructor !== Array) {
               data = [data];
           }
+          if(data.length == 0) {
+             // call drain immediately if there are no tasks
+             return async.setImmediate(function() {
+                 if (q.drain) {
+                     q.drain();
+                 }
+             });
+          }
           _each(data, function(task) {
               var item = {
                   data: task,
diff --git a/test/test-async.js b/test/test-async.js
index ff401e7..6bec545 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2436,3 +2436,25 @@ exports['queue events'] = function(test) {
     q.push('poo', function () {calls.push('poo cb');});
     q.push('moo', function () {calls.push('moo cb');});
 };
+
+exports['queue empty'] = function(test) {
+    var calls = [];
+    var q = async.queue(function(task, cb) {
+        // nop
+        calls.push('process ' + task);
+        async.setImmediate(cb);
+    }, 3);
+
+    q.drain = function() {
+        test.ok(
+            q.length() == 0 && q.running() == 0,
+            'queue should be empty now and no more workers should be running'
+        );
+        calls.push('drain');
+        test.same(calls, [
+            'drain'
+        ]);
+        test.done();
+    };
+    q.push([]);
+};

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