summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian White <mscdex@mscdex.net>2015-05-04 14:40:25 -0400
committerBrian White <mscdex@mscdex.net>2015-05-25 10:14:18 -0400
commit5abd4ac079b390467360d671a186a061b5aba736 (patch)
tree5965f0c339836e6e50da1e44dd16f91746e2cf8c
parenta74c2c94584aea67276ee15a67d9165bfd4ee9a5 (diff)
downloadnodejs-5abd4ac079b390467360d671a186a061b5aba736.tar.gz
nodejs-5abd4ac079b390467360d671a186a061b5aba736.tar.bz2
nodejs-5abd4ac079b390467360d671a186a061b5aba736.zip
lib: simplify nextTick() usage
This commit removes unnecessary nextTick() closures and adds some shared nextTick() callbacks for better re-use. PR-URL: https://github.com/nodejs/io.js/pull/1612 Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com> Reviewed-By: Brendan Ashworth <brendan.ashworth@me.com>
-rw-r--r--lib/_stream_writable.js12
-rw-r--r--lib/_tls_legacy.js6
-rw-r--r--lib/_tls_wrap.js4
-rw-r--r--lib/dgram.js9
-rw-r--r--lib/dns.js11
-rw-r--r--lib/net.js48
-rw-r--r--lib/timers.js9
7 files changed, 42 insertions, 57 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js
index 54f751abd..06bde5561 100644
--- a/lib/_stream_writable.js
+++ b/lib/_stream_writable.js
@@ -292,22 +292,16 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {
}
function onwriteError(stream, state, sync, er, cb) {
+ --state.pendingcb;
if (sync)
- process.nextTick(onwriteErrorNT, state, cb, er);
- else {
- state.pendingcb--;
+ process.nextTick(cb, er);
+ else
cb(er);
- }
stream._writableState.errorEmitted = true;
stream.emit('error', er);
}
-function onwriteErrorNT(state, cb, er) {
- state.pendingcb--;
- cb(er);
-}
-
function onwriteStateUpdate(state) {
state.writing = false;
state.writecb = null;
diff --git a/lib/_tls_legacy.js b/lib/_tls_legacy.js
index 0a0c7bca7..2ae922a81 100644
--- a/lib/_tls_legacy.js
+++ b/lib/_tls_legacy.js
@@ -448,16 +448,16 @@ CryptoStream.prototype.destroy = function(err) {
}
this._opposite.destroy();
- process.nextTick(destroyNT, this, err);
+ process.nextTick(destroyNT, this, err ? true : false);
};
-function destroyNT(self, err) {
+function destroyNT(self, hadErr) {
// Force EOF
self.push(null);
// Emit 'close' event
- self.emit('close', err ? true : false);
+ self.emit('close', hadErr);
}
diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js
index b39cdb9a3..9c934b9ff 100644
--- a/lib/_tls_wrap.js
+++ b/lib/_tls_wrap.js
@@ -444,9 +444,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
}
if (!this._handle.renegotiate()) {
if (callback) {
- process.nextTick(function() {
- callback(new Error('Failed to renegotiate'));
- });
+ process.nextTick(callback, new Error('Failed to renegotiate'));
}
return false;
}
diff --git a/lib/dgram.js b/lib/dgram.js
index f95b44926..f30e562c3 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -320,19 +320,14 @@ Socket.prototype.send = function(buffer,
!!callback);
if (err && callback) {
// don't emit as error, dgram_legacy.js compatibility
- process.nextTick(sendEmitErrorNT, err, address, port, callback);
+ var ex = exceptionWithHostPort(err, 'send', address, port);
+ process.nextTick(callback, ex);
}
}
});
};
-function sendEmitErrorNT(err, address, port, callback) {
- var ex = exceptionWithHostPort(err, 'send', address, port);
- callback(ex);
-}
-
-
function afterSend(err) {
if (err) {
err = exceptionWithHostPort(err, 'send', this.address, this.port);
diff --git a/lib/dns.js b/lib/dns.js
index 28f1607b4..fd0b12b1d 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -61,17 +61,16 @@ function makeAsync(callback) {
// The API already returned, we can invoke the callback immediately.
callback.apply(null, arguments);
} else {
- process.nextTick(callMakeAsyncCbNT, callback, arguments);
+ var args = new Array(arguments.length + 1);
+ args[0] = callback;
+ for (var i = 1, a = 0; a < arguments.length; ++i, ++a)
+ args[i] = arguments[a];
+ process.nextTick.apply(null, args);
}
};
}
-function callMakeAsyncCbNT(callback, args) {
- callback.apply(null, args);
-}
-
-
function onlookup(err, addresses) {
if (err) {
return this.callback(errnoException(err, 'getaddrinfo', this.hostname));
diff --git a/lib/net.js b/lib/net.js
index 0c5930bca..53095210f 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -268,14 +268,10 @@ function writeAfterFIN(chunk, encoding, cb) {
// TODO: defer error events consistently everywhere, not just the cb
self.emit('error', er);
if (typeof cb === 'function') {
- process.nextTick(writeAfterFINNT, cb, er);
+ process.nextTick(cb, er);
}
}
-function writeAfterFINNT(cb, er) {
- cb(er);
-}
-
exports.Socket = Socket;
exports.Stream = Socket; // Legacy naming.
@@ -442,9 +438,7 @@ Socket.prototype._destroy = function(exception, cb) {
function fireErrorCallbacks() {
if (cb) cb(exception);
if (exception && !self._writableState.errorEmitted) {
- process.nextTick(function() {
- self.emit('error', exception);
- });
+ process.nextTick(emitErrorNT, self, exception);
self._writableState.errorEmitted = true;
}
}
@@ -962,7 +956,10 @@ function lookupAndConnect(self, options) {
// immediately calls net.Socket.connect() on it (that's us).
// There are no event listeners registered yet so defer the
// error event to the next tick.
- process.nextTick(connectErrorNT, self, err, options);
+ err.host = options.host;
+ err.port = options.port;
+ err.message = err.message + ' ' + options.host + ':' + options.port;
+ process.nextTick(connectErrorNT, self, err);
} else {
self._unrefTimer();
connect(self,
@@ -976,10 +973,7 @@ function lookupAndConnect(self, options) {
}
-function connectErrorNT(self, err, options) {
- err.host = options.host;
- err.port = options.port;
- err.message = err.message + ' ' + options.host + ':' + options.port;
+function connectErrorNT(self, err) {
self.emit('error', err);
self._destroy();
}
@@ -1205,9 +1199,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
if (typeof rval === 'number') {
var error = exceptionWithHostPort(rval, 'listen', address, port);
- process.nextTick(function() {
- self.emit('error', error);
- });
+ process.nextTick(emitErrorNT, self, error);
return;
}
self._handle = rval;
@@ -1222,9 +1214,7 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
var ex = exceptionWithHostPort(err, 'listen', address, port);
self._handle.close();
self._handle = null;
- process.nextTick(function() {
- self.emit('error', ex);
- });
+ process.nextTick(emitErrorNT, self, ex);
return;
}
@@ -1239,6 +1229,11 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
};
+function emitErrorNT(self, err) {
+ self.emit('error', err);
+}
+
+
function emitListeningNT(self) {
// ensure handle hasn't closed
if (self._handle)
@@ -1412,9 +1407,7 @@ function onconnection(err, clientHandle) {
Server.prototype.getConnections = function(cb) {
function end(err, connections) {
- process.nextTick(function() {
- cb(err, connections);
- });
+ process.nextTick(cb, err, connections);
}
if (!this._usingSlaves) {
@@ -1493,13 +1486,16 @@ Server.prototype._emitCloseIfDrained = function() {
return;
}
- process.nextTick(function() {
- debug('SERVER: emit close');
- self.emit('close');
- });
+ process.nextTick(emitCloseNT, self);
};
+function emitCloseNT(self) {
+ debug('SERVER: emit close');
+ self.emit('close');
+}
+
+
Server.prototype.listenFD = util.deprecate(function(fd, type) {
return this.listen({ fd: fd });
}, 'listenFD is deprecated. Use listen({fd: <number>}).');
diff --git a/lib/timers.js b/lib/timers.js
index 27c4b0717..494c599b3 100644
--- a/lib/timers.js
+++ b/lib/timers.js
@@ -97,9 +97,7 @@ function listOnTimeout() {
// when the timeout threw its exception.
var oldDomain = process.domain;
process.domain = null;
- process.nextTick(function() {
- list[kOnTimeout]();
- });
+ process.nextTick(listOnTimeoutNT, list);
process.domain = oldDomain;
}
}
@@ -113,6 +111,11 @@ function listOnTimeout() {
}
+function listOnTimeoutNT(list) {
+ list[kOnTimeout]();
+}
+
+
const unenroll = exports.unenroll = function(item) {
L.remove(item);