stream: avoid unecessary nextTick

If we are not going to emit 'close' then there is no reason to
schedule it.

PR-URL: https://github.com/nodejs/node/pull/29194
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Robert Nagy 2019-08-18 18:08:53 +02:00 committed by Rich Trott
parent 0e715dea84
commit 033037cec9

View File

@ -48,12 +48,15 @@ function destroy(err, cb) {
}
this._destroy(err || null, (err) => {
const emitClose = (w && w.emitClose) || (r && r.emitClose);
if (cb) {
process.nextTick(emitCloseNT, this);
if (emitClose) {
process.nextTick(emitCloseNT, this);
}
cb(err);
} else if (needError(this, err)) {
process.nextTick(emitErrorAndCloseNT, this, err);
} else {
process.nextTick(emitClose ? emitErrorCloseNT : emitErrorNT, this, err);
} else if (emitClose) {
process.nextTick(emitCloseNT, this);
}
});
@ -61,22 +64,19 @@ function destroy(err, cb) {
return this;
}
function emitErrorAndCloseNT(self, err) {
emitErrorNT(self, err);
emitCloseNT(self);
function emitErrorCloseNT(self, err) {
self.emit('error', err);
self.emit('close');
}
function emitCloseNT(self) {
const r = self._readableState;
const w = self._writableState;
if (w && !w.emitClose)
return;
if (r && !r.emitClose)
return;
self.emit('close');
}
function emitErrorNT(self, err) {
self.emit('error', err);
}
function undestroy() {
const r = this._readableState;
const w = this._writableState;
@ -100,10 +100,6 @@ function undestroy() {
}
}
function emitErrorNT(self, err) {
self.emit('error', err);
}
function errorOrDestroy(stream, err) {
// We have tests that rely on errors being emitted
// in the same tick, so changing this is semver major.