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:
parent
0e715dea84
commit
033037cec9
@ -48,12 +48,15 @@ function destroy(err, cb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._destroy(err || null, (err) => {
|
this._destroy(err || null, (err) => {
|
||||||
|
const emitClose = (w && w.emitClose) || (r && r.emitClose);
|
||||||
if (cb) {
|
if (cb) {
|
||||||
process.nextTick(emitCloseNT, this);
|
if (emitClose) {
|
||||||
|
process.nextTick(emitCloseNT, this);
|
||||||
|
}
|
||||||
cb(err);
|
cb(err);
|
||||||
} else if (needError(this, err)) {
|
} else if (needError(this, err)) {
|
||||||
process.nextTick(emitErrorAndCloseNT, this, err);
|
process.nextTick(emitClose ? emitErrorCloseNT : emitErrorNT, this, err);
|
||||||
} else {
|
} else if (emitClose) {
|
||||||
process.nextTick(emitCloseNT, this);
|
process.nextTick(emitCloseNT, this);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -61,22 +64,19 @@ function destroy(err, cb) {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
function emitErrorAndCloseNT(self, err) {
|
function emitErrorCloseNT(self, err) {
|
||||||
emitErrorNT(self, err);
|
self.emit('error', err);
|
||||||
emitCloseNT(self);
|
self.emit('close');
|
||||||
}
|
}
|
||||||
|
|
||||||
function emitCloseNT(self) {
|
function emitCloseNT(self) {
|
||||||
const r = self._readableState;
|
|
||||||
const w = self._writableState;
|
|
||||||
|
|
||||||
if (w && !w.emitClose)
|
|
||||||
return;
|
|
||||||
if (r && !r.emitClose)
|
|
||||||
return;
|
|
||||||
self.emit('close');
|
self.emit('close');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function emitErrorNT(self, err) {
|
||||||
|
self.emit('error', err);
|
||||||
|
}
|
||||||
|
|
||||||
function undestroy() {
|
function undestroy() {
|
||||||
const r = this._readableState;
|
const r = this._readableState;
|
||||||
const w = this._writableState;
|
const w = this._writableState;
|
||||||
@ -100,10 +100,6 @@ function undestroy() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function emitErrorNT(self, err) {
|
|
||||||
self.emit('error', err);
|
|
||||||
}
|
|
||||||
|
|
||||||
function errorOrDestroy(stream, err) {
|
function errorOrDestroy(stream, err) {
|
||||||
// We have tests that rely on errors being emitted
|
// We have tests that rely on errors being emitted
|
||||||
// in the same tick, so changing this is semver major.
|
// in the same tick, so changing this is semver major.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user