[Pkg-javascript-commits] [node-async] 313/480: revert detection of sync tasks and add setImmediate
Jonas Smedegaard
js at moszumanska.debian.org
Fri May 2 08:58:37 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 a0d565d01bd256ea3d67104925139576d110aadb
Author: Caolan McMahon <caolan at caolanmcmahon.com>
Date: Fri Mar 15 12:37:31 2013 +0000
revert detection of sync tasks and add setImmediate
---
lib/async.js | 82 ++++++++++--------------------------------------------
test/test-async.js | 57 +------------------------------------
2 files changed, 15 insertions(+), 124 deletions(-)
diff --git a/lib/async.js b/lib/async.js
index ad3982a..98f2127 100755
--- a/lib/async.js
+++ b/lib/async.js
@@ -75,11 +75,11 @@
//// nextTick implementation with browser-compatible fallback ////
if (typeof process === 'undefined' || !(process.nextTick)) {
if (typeof setImmediate === 'function') {
- async.nextTick = function (fn) {
- setImmediate(fn);
- };
+ async.setImmediate = setImmediate;
+ async.nextTick = setImmediate;
}
else {
+ async.setImmediate = async.nextTick;
async.nextTick = function (fn) {
setTimeout(fn, 0);
};
@@ -87,6 +87,7 @@
}
else {
async.nextTick = process.nextTick;
+ async.setImmediate = setImmediate;
}
async.each = function (arr, iterator, callback) {
@@ -119,7 +120,6 @@
}
var completed = 0;
var iterate = function () {
- var sync = true;
iterator(arr[completed], function (err) {
if (err) {
callback(err);
@@ -131,16 +131,10 @@
callback(null);
}
else {
- if (sync) {
- async.nextTick(iterate);
- }
- else {
- iterate();
- }
+ iterate();
}
}
});
- sync = false;
};
iterate();
};
@@ -441,7 +435,7 @@
}
else {
results[k] = args;
- async.nextTick(taskComplete);
+ async.setImmediate(taskComplete);
}
};
var requires = task.slice(0, Math.abs(task.length - 1)) || [];
@@ -485,7 +479,7 @@
else {
args.push(callback);
}
- async.nextTick(function () {
+ async.setImmediate(function () {
iterator.apply(null, args);
});
}
@@ -607,21 +601,12 @@
async.whilst = function (test, iterator, callback) {
if (test()) {
- var sync = true;
iterator(function (err) {
if (err) {
return callback(err);
}
- if (sync) {
- async.nextTick(function () {
- async.whilst(test, iterator, callback);
- });
- }
- else {
- async.whilst(test, iterator, callback);
- }
+ async.whilst(test, iterator, callback);
});
- sync = false;
}
else {
callback();
@@ -629,45 +614,27 @@
};
async.doWhilst = function (iterator, test, callback) {
- var sync = true;
iterator(function (err) {
if (err) {
return callback(err);
}
if (test()) {
- if (sync) {
- async.nextTick(function () {
- async.doWhilst(iterator, test, callback);
- });
- }
- else {
- async.doWhilst(iterator, test, callback);
- }
+ async.doWhilst(iterator, test, callback);
}
else {
callback();
}
});
- sync = false;
};
async.until = function (test, iterator, callback) {
if (!test()) {
- var sync = true;
iterator(function (err) {
if (err) {
return callback(err);
}
- if (sync) {
- async.nextTick(function () {
- async.until(test, iterator, callback);
- });
- }
- else {
- async.until(test, iterator, callback);
- }
+ async.until(test, iterator, callback);
});
- sync = false;
}
else {
callback();
@@ -675,26 +642,17 @@
};
async.doUntil = function (iterator, test, callback) {
- var sync = true;
iterator(function (err) {
if (err) {
return callback(err);
}
if (!test()) {
- if (sync) {
- async.nextTick(function () {
- async.doUntil(iterator, test, callback);
- });
- }
- else {
- async.doUntil(iterator, test, callback);
- }
+ async.doUntil(iterator, test, callback);
}
else {
callback();
}
});
- sync = false;
};
async.queue = function (worker, concurrency) {
@@ -720,7 +678,7 @@
if (q.saturated && q.tasks.length === concurrency) {
q.saturated();
}
- async.nextTick(q.process);
+ async.setImmediate(q.process);
});
}
@@ -744,7 +702,6 @@
q.empty();
}
workers += 1;
- var sync = true;
var next = function () {
workers -= 1;
if (task.callback) {
@@ -755,19 +712,8 @@
}
q.process();
};
- var cb = only_once(function () {
- var cbArgs = arguments;
-
- if (sync) {
- async.nextTick(function () {
- next.apply(null, cbArgs);
- });
- } else {
- next.apply(null, arguments);
- }
- });
+ var cb = only_once(next);
worker(task.data, cb);
- sync = false;
}
},
length: function () {
@@ -803,7 +749,7 @@
cargo.saturated();
}
});
- async.nextTick(cargo.process);
+ async.setImmediate(cargo.process);
},
process: function process() {
if (working) return;
diff --git a/test/test-async.js b/test/test-async.js
index 5bd72bf..cba6f4b 100755
--- a/test/test-async.js
+++ b/test/test-async.js
@@ -2341,7 +2341,7 @@ exports['queue events'] = function(test) {
var q = async.queue(function(task, cb) {
// nop
calls.push('process ' + task);
- cb();
+ async.setImmediate(cb);
}, 3);
q.saturated = function() {
@@ -2381,58 +2381,3 @@ exports['queue events'] = function(test) {
q.push('poo', function () {calls.push('poo cb');});
q.push('moo', function () {calls.push('moo cb');});
};
-
-exports['avoid stack overflows for sync tasks'] = function (test) {
- if (typeof window !== 'undefined') {
- // skip this test in the browser, it takes AGES
- return test.done();
- }
- var arr = [];
- var funcarr = [];
- for (var i = 0; i < 10000; i++) {
- arr.push[i];
- funcarr.push(function (cb) { return cb(); });
- }
- var iter = function (i, cb) { cb(); };
- var counter = 0;
- var pred1 = function () {
- return counter <= 10000;
- };
- var iter = function (cb) {
- counter++;
- cb();
- };
- var pred2 = function () {
- return counter > 10000;
- };
- var resetCounter = function (cb) {
- counter = 0;
- cb();
- }
- async.series([
- async.apply(async.each, arr, iter),
- async.apply(async.eachSeries, arr, iter),
- async.apply(async.eachLimit, arr, iter, 2),
- async.apply(async.whilst, pred1, iter),
- resetCounter,
- async.apply(async.until, pred2, iter),
- resetCounter,
- async.apply(async.doWhilst, iter, pred1),
- resetCounter,
- async.apply(async.doUntil, iter, pred2),
- async.apply(async.series, funcarr),
- async.apply(async.parallel, funcarr),
- function (callback) {
- var q = async.queue(function (task, cb) {
- cb();
- }, 2);
- for (var j = 0; j < 10000; j++) {
- q.push(j);
- }
- q.drain = callback;
- }
- ],
- function (err) {
- test.done(err);
- });
-};
--
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