Skip to content

Commit

Permalink
node: allow multiple arguments passed to nextTick
Browse files Browse the repository at this point in the history
PR-URL: #1077
Reviewed-by: Colin Ihrig <[email protected]>
  • Loading branch information
trevnorris committed Apr 15, 2015
1 parent 12e51d5 commit 10e31ba
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 153 deletions.
2 changes: 1 addition & 1 deletion doc/api/process.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ This will generate:
`heapTotal` and `heapUsed` refer to V8's memory usage.


## process.nextTick(callback)
## process.nextTick(callback[, arg][, ...])

* `callback` {Function}

Expand Down
4 changes: 1 addition & 3 deletions lib/_debugger.js
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) {
} else {
val = handle;
}
process.nextTick(function() {
cb(null, val);
});
process.nextTick(cb, null, val);
};


Expand Down
38 changes: 21 additions & 17 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,8 @@ ClientRequest.prototype._implicitHeader = function() {
};

ClientRequest.prototype.abort = function() {
var self = this;
if (this.aborted === undefined) {
process.nextTick(function() {
self.emit('abort');
});
process.nextTick(emitAbortNT, this);
}
// Mark as aborting so we can avoid sending queued request data
// This is used as a truthy flag elsewhere. The use of Date.now is for
Expand All @@ -194,6 +191,11 @@ ClientRequest.prototype.abort = function() {
};


function emitAbortNT(self) {
self.emit('abort');
}


function createHangUpError() {
var error = new Error('socket hang up');
error.code = 'ECONNRESET';
Expand Down Expand Up @@ -440,12 +442,14 @@ function responseOnEnd() {
socket.removeListener('error', socketErrorListener);
// Mark this socket as available, AFTER user-added end
// handlers have a chance to run.
process.nextTick(function() {
socket.emit('free');
});
process.nextTick(emitFreeNT, socket);
}
}

function emitFreeNT(socket) {
socket.emit('free');
}

function tickOnSocket(req, socket) {
var parser = parsers.alloc();
req.socket = socket;
Expand Down Expand Up @@ -478,18 +482,18 @@ function tickOnSocket(req, socket) {
}

ClientRequest.prototype.onSocket = function(socket) {
var req = this;

process.nextTick(function() {
if (req.aborted) {
// If we were aborted while waiting for a socket, skip the whole thing.
socket.emit('free');
} else {
tickOnSocket(req, socket);
}
});
process.nextTick(onSocketNT, this, socket);
};

function onSocketNT(req, socket) {
if (req.aborted) {
// If we were aborted while waiting for a socket, skip the whole thing.
socket.emit('free');
} else {
tickOnSocket(req, socket);
}
}

ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
// This function is for calls that need to happen once the socket is
// connected and writable. It's an important promisy thing for all the socket
Expand Down
25 changes: 14 additions & 11 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,9 @@ Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {


OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
var self = this;

if (this.finished) {
var err = new Error('write after end');
process.nextTick(function() {
self.emit('error', err);
if (callback) callback(err);
});
process.nextTick(writeAfterEndNT, this, err, callback);

return true;
}
Expand Down Expand Up @@ -455,11 +450,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {

if (this.connection && !this.connection.corked) {
this.connection.cork();
var conn = this.connection;
process.nextTick(function connectionCork() {
if (conn)
conn.uncork();
});
process.nextTick(connectionCorkNT, this.connection);
}
this._send(len.toString(16), 'binary', null);
this._send(crlf_buf, null, null);
Expand All @@ -475,6 +466,18 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
};


function writeAfterEndNT(self, err, callback) {
self.emit('error', err);
if (callback) callback(err);
}


function connectionCorkNT(conn) {
if (conn)
conn.uncork();
}


OutgoingMessage.prototype.addTrailers = function(headers) {
this._trailer = '';
var keys = Object.keys(headers);
Expand Down
6 changes: 5 additions & 1 deletion lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,9 @@ function onend() {

// no more data can be written.
// But allow more writes to happen in this tick.
process.nextTick(this.end.bind(this));
process.nextTick(onEndNT, this);
}

function onEndNT(self) {
self.end();
}
41 changes: 19 additions & 22 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,7 @@ function emitReadable(stream) {
debug('emitReadable', state.flowing);
state.emittedReadable = true;
if (state.sync)
process.nextTick(function() {
emitReadable_(stream);
});
process.nextTick(emitReadable_, stream);
else
emitReadable_(stream);
}
Expand All @@ -419,9 +417,7 @@ function emitReadable_(stream) {
function maybeReadMore(stream, state) {
if (!state.readingMore) {
state.readingMore = true;
process.nextTick(function() {
maybeReadMore_(stream, state);
});
process.nextTick(maybeReadMore_, stream, state);
}
}

Expand Down Expand Up @@ -667,11 +663,7 @@ Readable.prototype.on = function(ev, fn) {
state.emittedReadable = false;
state.needReadable = true;
if (!state.reading) {
var self = this;
process.nextTick(function() {
debug('readable nexttick read 0');
self.read(0);
});
process.nextTick(nReadingNextTick, this);
} else if (state.length) {
emitReadable(this, state);
}
Expand All @@ -682,6 +674,11 @@ Readable.prototype.on = function(ev, fn) {
};
Readable.prototype.addListener = Readable.prototype.on;

function nReadingNextTick(self) {
debug('readable nexttick read 0');
self.read(0);
}

// pause() and resume() are remnants of the legacy readable stream API
// If the user uses them, then switch into old mode.
Readable.prototype.resume = function() {
Expand All @@ -697,9 +694,7 @@ Readable.prototype.resume = function() {
function resume(stream, state) {
if (!state.resumeScheduled) {
state.resumeScheduled = true;
process.nextTick(function() {
resume_(stream, state);
});
process.nextTick(resume_, stream, state);
}
}

Expand Down Expand Up @@ -883,13 +878,15 @@ function endReadable(stream) {

if (!state.endEmitted) {
state.ended = true;
process.nextTick(function() {
// Check that we didn't get one last unshift.
if (!state.endEmitted && state.length === 0) {
state.endEmitted = true;
stream.readable = false;
stream.emit('end');
}
});
process.nextTick(endReadableNT, state, stream);
}
}

function endReadableNT(state, stream) {
// Check that we didn't get one last unshift.
if (!state.endEmitted && state.length === 0) {
state.endEmitted = true;
stream.readable = false;
stream.emit('end');
}
}
22 changes: 9 additions & 13 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ function writeAfterEnd(stream, cb) {
var er = new Error('write after end');
// TODO: defer error events consistently everywhere, not just the cb
stream.emit('error', er);
process.nextTick(function() {
cb(er);
});
process.nextTick(cb, er);
}

// If we get something that is not a buffer, string, null, or undefined,
Expand All @@ -178,9 +176,7 @@ function validChunk(stream, state, chunk, cb) {
!state.objectMode) {
var er = new TypeError('Invalid non-string/buffer chunk');
stream.emit('error', er);
process.nextTick(function() {
cb(er);
});
process.nextTick(cb, er);
valid = false;
}
return valid;
Expand Down Expand Up @@ -298,10 +294,7 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {

function onwriteError(stream, state, sync, er, cb) {
if (sync)
process.nextTick(function() {
state.pendingcb--;
cb(er);
});
process.nextTick(onwriteErrorNT, state, cb, er);
else {
state.pendingcb--;
cb(er);
Expand All @@ -311,6 +304,11 @@ function onwriteError(stream, state, sync, er, cb) {
stream.emit('error', er);
}

function onwriteErrorNT(state, cb, er) {
state.pendingcb--;
cb(er);
}

function onwriteStateUpdate(state) {
state.writing = false;
state.writecb = null;
Expand Down Expand Up @@ -339,9 +337,7 @@ function onwrite(stream, er) {
}

if (sync) {
process.nextTick(function() {
afterWrite(stream, state, finished, cb);
});
process.nextTick(afterWrite, stream, state, finished, cb);
} else {
afterWrite(stream, state, finished, cb);
}
Expand Down
63 changes: 34 additions & 29 deletions lib/_tls_legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,17 +448,19 @@ CryptoStream.prototype.destroy = function(err) {
}
this._opposite.destroy();

var self = this;
process.nextTick(function() {
// Force EOF
self.push(null);

// Emit 'close' event
self.emit('close', err ? true : false);
});
process.nextTick(destroyNT, this, err);
};


function destroyNT(self, err) {
// Force EOF
self.push(null);

// Emit 'close' event
self.emit('close', err ? true : false);
}


CryptoStream.prototype._done = function() {
this._doneFlag = true;

Expand Down Expand Up @@ -667,8 +669,6 @@ function SecurePair(context, isServer, requestCert, rejectUnauthorized,
options);
}

var self = this;

options || (options = {});

events.EventEmitter.call(this);
Expand Down Expand Up @@ -737,23 +737,25 @@ function SecurePair(context, isServer, requestCert, rejectUnauthorized,
this.cleartext.init();
this.encrypted.init();

process.nextTick(function() {
/* The Connection may be destroyed by an abort call */
if (self.ssl) {
self.ssl.start();

if (options.requestOCSP)
self.ssl.requestOCSP();

/* In case of cipher suite failures - SSL_accept/SSL_connect may fail */
if (self.ssl && self.ssl.error)
self.error();
}
});
process.nextTick(securePairNT, this, options);
}

util.inherits(SecurePair, events.EventEmitter);

function securePairNT(self, options) {
/* The Connection may be destroyed by an abort call */
if (self.ssl) {
self.ssl.start();

if (options.requestOCSP)
self.ssl.requestOCSP();

/* In case of cipher suite failures - SSL_accept/SSL_connect may fail */
if (self.ssl && self.ssl.error)
self.error();
}
}


exports.createSecurePair = function(context,
isServer,
Expand Down Expand Up @@ -835,12 +837,7 @@ exports.pipe = function pipe(pair, socket) {
socket.pipe(pair.encrypted);

pair.encrypted.on('close', function() {
process.nextTick(function() {
// Encrypted should be unpiped from socket to prevent possible
// write after destroy.
pair.encrypted.unpipe(socket);
socket.destroySoon();
});
process.nextTick(pipeCloseNT, pair, socket);
});

pair.fd = socket.fd;
Expand Down Expand Up @@ -886,3 +883,11 @@ exports.pipe = function pipe(pair, socket) {

return cleartext;
};


function pipeCloseNT(pair, socket) {
// Encrypted should be unpiped from socket to prevent possible
// write after destroy.
pair.encrypted.unpipe(socket);
socket.destroySoon();
}
Loading

0 comments on commit 10e31ba

Please sign in to comment.