Browse Source

Add process.nextTick()

This is a replacement for the common hack:

  setTimeout(cb, 0);

It's much more efficient.
v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
8abeffa9ea
  1. 3
      doc/api.txt
  2. 4
      lib/multipart.js
  3. 32
      src/node.js
  4. 28
      test/mjsunit/test-next-tick.js

3
doc/api.txt

@ -123,6 +123,9 @@ Returns the memory usage of the Node process. It looks like this
+
+heapTotal+ and +heapUsed+ refer to V8's memory usage.
+process.nextTick(callback)+::
On the next loop around the event loop call this callback.
+process.exit(code=0)+::
Ends the process with the specified code. By default it exits with the
success code 0.

4
lib/multipart.js

@ -7,9 +7,9 @@ exports.parse = function(options) {
try {
var stream = new exports.Stream(options);
} catch (e) {
setTimeout(function() {
process.nextTick(function() {
promise.emitError(e);
}, 0);
});
return promise;
}

32
src/node.js

@ -377,6 +377,30 @@ var eventsModule = createInternalModule('events', function (exports) {
var events = eventsModule.exports;
// nextTick()
var nextTickQueue = [];
var nextTickWatcher = new process.IdleWatcher();
nextTickWatcher.setPriority(process.EVMAXPRI); // max priority
nextTickWatcher.callback = function () {
var l = nextTickQueue.length;
while (l--) {
var cb = nextTickQueue.shift();
cb();
}
if (nextTickQueue.length == 0) nextTickWatcher.stop();
};
process.nextTick = function (callback) {
nextTickQueue.push(callback);
nextTickWatcher.start();
};
// Signal Handlers
function isSignal (event) {
@ -829,9 +853,9 @@ function loadModule (request, parent) {
debug("found " + JSON.stringify(id) + " in cache");
// In cache
var module = moduleCache[id];
setTimeout(function () {
process.nextTick(function () {
loadPromise.emitSuccess(module.exports);
}, 0);
});
} else {
debug("looking for " + JSON.stringify(id) + " in " + JSON.stringify(paths));
// Not in cache
@ -868,11 +892,11 @@ Module.prototype.loadObject = function (filename, loadPromise) {
var self = this;
// XXX Not yet supporting loading from HTTP. would need to download the
// file, store it to tmp then run dlopen on it.
setTimeout(function () {
process.nextTick(function () {
self.loaded = true;
process.dlopen(filename, self.exports); // FIXME synchronus
loadPromise.emitSuccess(self.exports);
}, 0);
});
};
function cat (id, loadPromise) {

28
test/mjsunit/test-next-tick.js

@ -0,0 +1,28 @@
process.mixin(require("./common"));
var complete = 0;
process.nextTick(function () {
complete++;
process.nextTick(function () {
complete++;
process.nextTick(function () {
complete++;
});
});
});
setTimeout(function () {
process.nextTick(function () {
complete++;
});
}, 50);
process.nextTick(function () {
complete++;
});
process.addListener('exit', function () {
assert.equal(5, complete);
});
Loading…
Cancel
Save