Skip to content

Commit

Permalink
stream: avoid unecessary nextTick
Browse files Browse the repository at this point in the history
If we are not going to emit 'close' then there is no reason to
schedule it.

PR-URL: #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>
  • Loading branch information
ronag authored and Trott committed Aug 23, 2019
1 parent 0e715de commit 033037c
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,35 @@ 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);
}
});

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;
Expand All @@ -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.
Expand Down

0 comments on commit 033037c

Please sign in to comment.