[Pkg-javascript-commits] [node-async] 269/480: reimplementing queue unshift
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:33 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 1768a3d6dab2f2b3eacd8407bb646231c5fd6768
Author: KARASZI István <github at spam.raszi.hu>
Date: Mon Feb 4 19:07:26 2013 +0100
reimplementing queue unshift
---
lib/async.js | 40 +++++++++++++++++++++++++++-------------
test/test-async.js | 19 +++++++++++++++++++
2 files changed, 46 insertions(+), 13 deletions(-)
diff --git a/lib/async.js b/lib/async.js
index 42ea9a7..2247d0e 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -690,6 +690,29 @@
};
async.queue = function (worker, concurrency) {
+ function _insert(q, data, pos, callback) {
+ if(data.constructor !== Array) {
+ data = [data];
+ }
+ _forEach(data, function(task) {
+ var item = {
+ data: task,
+ callback: typeof callback === 'function' ? callback : null
+ };
+
+ if (pos) {
+ q.tasks.unshift(item);
+ } else {
+ q.tasks.push(item);
+ }
+
+ if (q.saturated && q.tasks.length === concurrency) {
+ q.saturated();
+ }
+ async.nextTick(q.process);
+ });
+ }
+
var workers = 0;
var q = {
tasks: [],
@@ -698,19 +721,10 @@
empty: null,
drain: null,
push: function (data, callback) {
- 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);
- });
+ _insert(q, data, false, callback);
+ },
+ unshift: function (data, callback) {
+ _insert(q, data, true, callback);
},
process: function () {
if (workers < q.concurrency && q.tasks.length) {
diff --git a/test/test-async.js b/test/test-async.js
index c0c1f2f..fafa6d7 100644
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -1673,6 +1673,25 @@ exports['queue push without callback'] = function (test) {
}, 800);
};
+exports['queue unshift'] = function (test) {
+ var queue_order = [];
+
+ var q = async.queue(function (task, callback) {
+ queue_order.push(task);
+ callback();
+ }, 1);
+
+ q.unshift(4);
+ q.unshift(3);
+ q.unshift(2);
+ q.unshift(1);
+
+ setTimeout(function () {
+ test.same(queue_order, [ 1, 2, 3, 4 ]);
+ test.done();
+ }, 100);
+};
+
exports['queue too many callbacks'] = function (test) {
var q = async.queue(function (task, callback) {
callback();
--
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