diff options
author | Robert Nagy <ronagy@icloud.com> | 2019-08-18 18:08:53 +0200 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-08-23 15:23:09 -0700 |
commit | 033037cec99b3b20cfec25fcebd5ee68fbb30ad4 (patch) | |
tree | 75a0355d7847e79b2c17745ffdc7befa2666d8f5 /lib | |
parent | 0e715dea84d67a24624516c730aa71ed04971727 (diff) | |
download | android-node-v8-033037cec99b3b20cfec25fcebd5ee68fbb30ad4.tar.gz android-node-v8-033037cec99b3b20cfec25fcebd5ee68fbb30ad4.tar.bz2 android-node-v8-033037cec99b3b20cfec25fcebd5ee68fbb30ad4.zip |
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>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/internal/streams/destroy.js | 30 |
1 files changed, 13 insertions, 17 deletions
diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 0d3eb6327b..27985482ce 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -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. |