[Pkg-javascript-commits] [node-pend] 03/10: ability to cap max simultaneous with `max` property

Andrew Kelley andrewrk-guest at moszumanska.debian.org
Fri Jun 27 22:11:00 UTC 2014


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

andrewrk-guest pushed a commit to branch master
in repository node-pend.

commit 8c84223db36bbb8b3ef939a7c386791945e68e0e
Author: Andrew Kelley <superjoe30 at gmail.com>
Date:   Sat Aug 10 14:10:05 2013 -0400

    ability to cap max simultaneous with `max` property
---
 README.md |  2 --
 index.js  | 13 ++++++++---
 test.js   | 77 ++++++++++++++++++++++++++++++++++++++++++++++-----------------
 3 files changed, 67 insertions(+), 25 deletions(-)

diff --git a/README.md b/README.md
index bf04a5c..f073c15 100644
--- a/README.md
+++ b/README.md
@@ -37,5 +37,3 @@ calling cb 2
 this is excuted when the first 2 have returned.
 err is a possible error in the standard callback style.
 ```
-
-This readme file is longer than the source code.
diff --git a/index.js b/index.js
index 954edf2..ae64857 100644
--- a/index.js
+++ b/index.js
@@ -2,12 +2,18 @@ module.exports = Pend;
 
 function Pend() {
   this.pending = 0;
+  this.max = Infinity;
   this.listeners = [];
+  this.waiting = [];
   this.error = null;
 }
 
 Pend.prototype.go = function(fn) {
-  pendGo(this, fn);
+  if (this.pending < this.max) {
+    pendGo(this, fn);
+  } else {
+    this.waiting.push(fn);
+  }
 };
 
 Pend.prototype.wait = function(cb) {
@@ -25,7 +31,9 @@ function pendGo(self, fn) {
     self.error = self.error || err;
     self.pending -= 1;
     if (self.pending < 0) throw new Error("Callback called twice.");
-    if (self.pending === 0) {
+    if (self.waiting.length > 0 && self.pending < self.max) {
+      pendGo(self, self.waiting.shift());
+    } else if (self.pending === 0) {
       self.listeners.forEach(cbListener);
       self.listeners = [];
     }
@@ -34,4 +42,3 @@ function pendGo(self, fn) {
     listener(self.error);
   }
 }
-
diff --git a/test.js b/test.js
index c676858..e744281 100644
--- a/test.js
+++ b/test.js
@@ -1,22 +1,59 @@
 var assert = require('assert');
 var Pend = require('./');
-var pend = new Pend();
-var results = [];
-pend.go(function(cb) {
-  results.push(1);
-  setTimeout(function() {
-    results.push(3);
-    cb();
-  }, 500);
-});
-pend.go(function(cb) {
-  results.push(2);
-  setTimeout(function() {
-    results.push(4);
-    cb();
-  }, 1000);
-});
-pend.wait(function(err) {
-  assert.deepEqual(results, [1,2,3,4]);
-});
-assert.deepEqual(results, [1, 2]);
+
+testBasic();
+
+function testBasic() {
+  var pend = new Pend();
+  var results = [];
+  pend.go(function(cb) {
+    results.push(1);
+    setTimeout(function() {
+      results.push(3);
+      cb();
+    }, 500);
+  });
+  pend.go(function(cb) {
+    results.push(2);
+    setTimeout(function() {
+      results.push(4);
+      cb();
+    }, 1000);
+  });
+  pend.wait(function(err) {
+    assert.deepEqual(results, [1,2,3,4]);
+    testWithMax();
+  });
+  assert.deepEqual(results, [1, 2]);
+}
+
+function testWithMax() {
+  var pend = new Pend();
+  var results = [];
+  pend.max = 2;
+  pend.go(function(cb) {
+    results.push('a');
+    setTimeout(function() {
+      results.push(1);
+      cb();
+    }, 500);
+  });
+  pend.go(function(cb) {
+    results.push('b');
+    setTimeout(function() {
+      results.push(1);
+      cb();
+    }, 500);
+  });
+  pend.go(function(cb) {
+    results.push('c');
+    setTimeout(function() {
+      results.push(2);
+      cb();
+    }, 100);
+  });
+  pend.wait(function(err) {
+    assert.deepEqual(results, ['a', 'b', 1, 'c', 1, 2]);
+  });
+  assert.deepEqual(results, ['a', 'b']);
+}

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



More information about the Pkg-javascript-commits mailing list