[Pkg-javascript-commits] [node-async] 77/480: callback methods w/ test & docs
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:14 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 570cf8e16ae6775a10c00302d5b36a7f2d56bf9d
Author: MaZderMind <peter at mazdermind.de>
Date: Sun Apr 17 19:17:07 2011 +0200
callback methods w/ test & docs
---
README.md | 8 ++++++++
lib/async.js | 6 ++++++
test/test-async.js | 24 ++++++++++++++++++++++++
3 files changed, 38 insertions(+)
diff --git a/README.md b/README.md
index 196740a..039d942 100644
--- a/README.md
+++ b/README.md
@@ -674,6 +674,9 @@ 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.
+* 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
__Example__
@@ -685,6 +688,11 @@ __Example__
}, 2);
+ // assign a callback
+ q.drain = function() {
+ console.log('all items have been processed');
+ }
+
// add some items to the queue
q.push({name: 'foo'}, function (err) {
diff --git a/lib/async.js b/lib/async.js
index 7e4b058..9be463e 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -549,13 +549,19 @@
var tasks = [];
var q = {
concurrency: concurrency,
+ saturated: null,
+ empty: null,
+ drain: null,
push: function (data, callback) {
tasks.push({data: data, callback: callback});
+ if(q.saturated && tasks.length == concurrency) q.saturated();
async.nextTick(q.process);
},
process: function () {
if (workers < q.concurrency && tasks.length) {
var task = tasks.splice(0, 1)[0];
+ if(q.empty && tasks.length == 0) q.empty();
+ if(q.drain && tasks.length + workers == 0) q.drain();
workers += 1;
worker(task.data, function () {
workers -= 1;
diff --git a/test/test-async.js b/test/test-async.js
index 4f5648f..a0d5393 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -1319,3 +1319,27 @@ exports['falsy return values in parallel'] = function (test) {
}
);
};
+
+exports['queue events'] = function(test) {
+ test.expect(3);
+ var q = async.queue(function(task, cb) {
+ // nop
+ cb();
+ }, 3);
+
+ q.saturated = function() {
+ test.ok(q.length() == 3, 'queue should be saturated now');
+ }
+ q.empty = function() {
+ test.ok(q.length() == 0, 'queue should be empty now');
+ }
+ q.drain = function() {
+ test.ok(q.length() == 0 && q.running() == 0, 'queue should be empty now and no more workers should be running');
+ test.done();
+ }
+ q.push('foo');
+ q.push('bar');
+ q.push('zoo');
+ q.push('poo');
+ q.push('moo');
+};
--
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