Skip to content

Commit 10e31ba

Browse files
committed
node: allow multiple arguments passed to nextTick
PR-URL: #1077 Reviewed-by: Colin Ihrig <[email protected]>
1 parent 12e51d5 commit 10e31ba

18 files changed

+202
-153
lines changed

doc/api/process.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ This will generate:
687687
`heapTotal` and `heapUsed` refer to V8's memory usage.
688688

689689

690-
## process.nextTick(callback)
690+
## process.nextTick(callback[, arg][, ...])
691691

692692
* `callback` {Function}
693693

lib/_debugger.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -587,9 +587,7 @@ Client.prototype.mirrorObject = function(handle, depth, cb) {
587587
} else {
588588
val = handle;
589589
}
590-
process.nextTick(function() {
591-
cb(null, val);
592-
});
590+
process.nextTick(cb, null, val);
593591
};
594592

595593

lib/_http_client.js

+21-17
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,8 @@ ClientRequest.prototype._implicitHeader = function() {
166166
};
167167

168168
ClientRequest.prototype.abort = function() {
169-
var self = this;
170169
if (this.aborted === undefined) {
171-
process.nextTick(function() {
172-
self.emit('abort');
173-
});
170+
process.nextTick(emitAbortNT, this);
174171
}
175172
// Mark as aborting so we can avoid sending queued request data
176173
// This is used as a truthy flag elsewhere. The use of Date.now is for
@@ -194,6 +191,11 @@ ClientRequest.prototype.abort = function() {
194191
};
195192

196193

194+
function emitAbortNT(self) {
195+
self.emit('abort');
196+
}
197+
198+
197199
function createHangUpError() {
198200
var error = new Error('socket hang up');
199201
error.code = 'ECONNRESET';
@@ -440,12 +442,14 @@ function responseOnEnd() {
440442
socket.removeListener('error', socketErrorListener);
441443
// Mark this socket as available, AFTER user-added end
442444
// handlers have a chance to run.
443-
process.nextTick(function() {
444-
socket.emit('free');
445-
});
445+
process.nextTick(emitFreeNT, socket);
446446
}
447447
}
448448

449+
function emitFreeNT(socket) {
450+
socket.emit('free');
451+
}
452+
449453
function tickOnSocket(req, socket) {
450454
var parser = parsers.alloc();
451455
req.socket = socket;
@@ -478,18 +482,18 @@ function tickOnSocket(req, socket) {
478482
}
479483

480484
ClientRequest.prototype.onSocket = function(socket) {
481-
var req = this;
482-
483-
process.nextTick(function() {
484-
if (req.aborted) {
485-
// If we were aborted while waiting for a socket, skip the whole thing.
486-
socket.emit('free');
487-
} else {
488-
tickOnSocket(req, socket);
489-
}
490-
});
485+
process.nextTick(onSocketNT, this, socket);
491486
};
492487

488+
function onSocketNT(req, socket) {
489+
if (req.aborted) {
490+
// If we were aborted while waiting for a socket, skip the whole thing.
491+
socket.emit('free');
492+
} else {
493+
tickOnSocket(req, socket);
494+
}
495+
}
496+
493497
ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
494498
// This function is for calls that need to happen once the socket is
495499
// connected and writable. It's an important promisy thing for all the socket

lib/_http_outgoing.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,9 @@ Object.defineProperty(OutgoingMessage.prototype, 'headersSent', {
406406

407407

408408
OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
409-
var self = this;
410-
411409
if (this.finished) {
412410
var err = new Error('write after end');
413-
process.nextTick(function() {
414-
self.emit('error', err);
415-
if (callback) callback(err);
416-
});
411+
process.nextTick(writeAfterEndNT, this, err, callback);
417412

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

456451
if (this.connection && !this.connection.corked) {
457452
this.connection.cork();
458-
var conn = this.connection;
459-
process.nextTick(function connectionCork() {
460-
if (conn)
461-
conn.uncork();
462-
});
453+
process.nextTick(connectionCorkNT, this.connection);
463454
}
464455
this._send(len.toString(16), 'binary', null);
465456
this._send(crlf_buf, null, null);
@@ -475,6 +466,18 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) {
475466
};
476467

477468

469+
function writeAfterEndNT(self, err, callback) {
470+
self.emit('error', err);
471+
if (callback) callback(err);
472+
}
473+
474+
475+
function connectionCorkNT(conn) {
476+
if (conn)
477+
conn.uncork();
478+
}
479+
480+
478481
OutgoingMessage.prototype.addTrailers = function(headers) {
479482
this._trailer = '';
480483
var keys = Object.keys(headers);

lib/_stream_duplex.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ function onend() {
4949

5050
// no more data can be written.
5151
// But allow more writes to happen in this tick.
52-
process.nextTick(this.end.bind(this));
52+
process.nextTick(onEndNT, this);
53+
}
54+
55+
function onEndNT(self) {
56+
self.end();
5357
}

lib/_stream_readable.js

+19-22
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,7 @@ function emitReadable(stream) {
395395
debug('emitReadable', state.flowing);
396396
state.emittedReadable = true;
397397
if (state.sync)
398-
process.nextTick(function() {
399-
emitReadable_(stream);
400-
});
398+
process.nextTick(emitReadable_, stream);
401399
else
402400
emitReadable_(stream);
403401
}
@@ -419,9 +417,7 @@ function emitReadable_(stream) {
419417
function maybeReadMore(stream, state) {
420418
if (!state.readingMore) {
421419
state.readingMore = true;
422-
process.nextTick(function() {
423-
maybeReadMore_(stream, state);
424-
});
420+
process.nextTick(maybeReadMore_, stream, state);
425421
}
426422
}
427423

@@ -667,11 +663,7 @@ Readable.prototype.on = function(ev, fn) {
667663
state.emittedReadable = false;
668664
state.needReadable = true;
669665
if (!state.reading) {
670-
var self = this;
671-
process.nextTick(function() {
672-
debug('readable nexttick read 0');
673-
self.read(0);
674-
});
666+
process.nextTick(nReadingNextTick, this);
675667
} else if (state.length) {
676668
emitReadable(this, state);
677669
}
@@ -682,6 +674,11 @@ Readable.prototype.on = function(ev, fn) {
682674
};
683675
Readable.prototype.addListener = Readable.prototype.on;
684676

677+
function nReadingNextTick(self) {
678+
debug('readable nexttick read 0');
679+
self.read(0);
680+
}
681+
685682
// pause() and resume() are remnants of the legacy readable stream API
686683
// If the user uses them, then switch into old mode.
687684
Readable.prototype.resume = function() {
@@ -697,9 +694,7 @@ Readable.prototype.resume = function() {
697694
function resume(stream, state) {
698695
if (!state.resumeScheduled) {
699696
state.resumeScheduled = true;
700-
process.nextTick(function() {
701-
resume_(stream, state);
702-
});
697+
process.nextTick(resume_, stream, state);
703698
}
704699
}
705700

@@ -883,13 +878,15 @@ function endReadable(stream) {
883878

884879
if (!state.endEmitted) {
885880
state.ended = true;
886-
process.nextTick(function() {
887-
// Check that we didn't get one last unshift.
888-
if (!state.endEmitted && state.length === 0) {
889-
state.endEmitted = true;
890-
stream.readable = false;
891-
stream.emit('end');
892-
}
893-
});
881+
process.nextTick(endReadableNT, state, stream);
882+
}
883+
}
884+
885+
function endReadableNT(state, stream) {
886+
// Check that we didn't get one last unshift.
887+
if (!state.endEmitted && state.length === 0) {
888+
state.endEmitted = true;
889+
stream.readable = false;
890+
stream.emit('end');
894891
}
895892
}

lib/_stream_writable.js

+9-13
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,7 @@ function writeAfterEnd(stream, cb) {
158158
var er = new Error('write after end');
159159
// TODO: defer error events consistently everywhere, not just the cb
160160
stream.emit('error', er);
161-
process.nextTick(function() {
162-
cb(er);
163-
});
161+
process.nextTick(cb, er);
164162
}
165163

166164
// If we get something that is not a buffer, string, null, or undefined,
@@ -178,9 +176,7 @@ function validChunk(stream, state, chunk, cb) {
178176
!state.objectMode) {
179177
var er = new TypeError('Invalid non-string/buffer chunk');
180178
stream.emit('error', er);
181-
process.nextTick(function() {
182-
cb(er);
183-
});
179+
process.nextTick(cb, er);
184180
valid = false;
185181
}
186182
return valid;
@@ -298,10 +294,7 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) {
298294

299295
function onwriteError(stream, state, sync, er, cb) {
300296
if (sync)
301-
process.nextTick(function() {
302-
state.pendingcb--;
303-
cb(er);
304-
});
297+
process.nextTick(onwriteErrorNT, state, cb, er);
305298
else {
306299
state.pendingcb--;
307300
cb(er);
@@ -311,6 +304,11 @@ function onwriteError(stream, state, sync, er, cb) {
311304
stream.emit('error', er);
312305
}
313306

307+
function onwriteErrorNT(state, cb, er) {
308+
state.pendingcb--;
309+
cb(er);
310+
}
311+
314312
function onwriteStateUpdate(state) {
315313
state.writing = false;
316314
state.writecb = null;
@@ -339,9 +337,7 @@ function onwrite(stream, er) {
339337
}
340338

341339
if (sync) {
342-
process.nextTick(function() {
343-
afterWrite(stream, state, finished, cb);
344-
});
340+
process.nextTick(afterWrite, stream, state, finished, cb);
345341
} else {
346342
afterWrite(stream, state, finished, cb);
347343
}

lib/_tls_legacy.js

+34-29
Original file line numberDiff line numberDiff line change
@@ -448,17 +448,19 @@ CryptoStream.prototype.destroy = function(err) {
448448
}
449449
this._opposite.destroy();
450450

451-
var self = this;
452-
process.nextTick(function() {
453-
// Force EOF
454-
self.push(null);
455-
456-
// Emit 'close' event
457-
self.emit('close', err ? true : false);
458-
});
451+
process.nextTick(destroyNT, this, err);
459452
};
460453

461454

455+
function destroyNT(self, err) {
456+
// Force EOF
457+
self.push(null);
458+
459+
// Emit 'close' event
460+
self.emit('close', err ? true : false);
461+
}
462+
463+
462464
CryptoStream.prototype._done = function() {
463465
this._doneFlag = true;
464466

@@ -667,8 +669,6 @@ function SecurePair(context, isServer, requestCert, rejectUnauthorized,
667669
options);
668670
}
669671

670-
var self = this;
671-
672672
options || (options = {});
673673

674674
events.EventEmitter.call(this);
@@ -737,23 +737,25 @@ function SecurePair(context, isServer, requestCert, rejectUnauthorized,
737737
this.cleartext.init();
738738
this.encrypted.init();
739739

740-
process.nextTick(function() {
741-
/* The Connection may be destroyed by an abort call */
742-
if (self.ssl) {
743-
self.ssl.start();
744-
745-
if (options.requestOCSP)
746-
self.ssl.requestOCSP();
747-
748-
/* In case of cipher suite failures - SSL_accept/SSL_connect may fail */
749-
if (self.ssl && self.ssl.error)
750-
self.error();
751-
}
752-
});
740+
process.nextTick(securePairNT, this, options);
753741
}
754742

755743
util.inherits(SecurePair, events.EventEmitter);
756744

745+
function securePairNT(self, options) {
746+
/* The Connection may be destroyed by an abort call */
747+
if (self.ssl) {
748+
self.ssl.start();
749+
750+
if (options.requestOCSP)
751+
self.ssl.requestOCSP();
752+
753+
/* In case of cipher suite failures - SSL_accept/SSL_connect may fail */
754+
if (self.ssl && self.ssl.error)
755+
self.error();
756+
}
757+
}
758+
757759

758760
exports.createSecurePair = function(context,
759761
isServer,
@@ -835,12 +837,7 @@ exports.pipe = function pipe(pair, socket) {
835837
socket.pipe(pair.encrypted);
836838

837839
pair.encrypted.on('close', function() {
838-
process.nextTick(function() {
839-
// Encrypted should be unpiped from socket to prevent possible
840-
// write after destroy.
841-
pair.encrypted.unpipe(socket);
842-
socket.destroySoon();
843-
});
840+
process.nextTick(pipeCloseNT, pair, socket);
844841
});
845842

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

887884
return cleartext;
888885
};
886+
887+
888+
function pipeCloseNT(pair, socket) {
889+
// Encrypted should be unpiped from socket to prevent possible
890+
// write after destroy.
891+
pair.encrypted.unpipe(socket);
892+
socket.destroySoon();
893+
}

0 commit comments

Comments
 (0)