Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v12.x backport] stream: avoid unecessary nextTick #29691

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,25 @@ function destroy(err, cb) {
}

this._destroy(err || null, (err) => {
const emitClose = (this._writableState && this._writableState.emitClose) ||
(this._readableState && this._readableState.emitClose);
if (!cb && err) {
if (!this._writableState) {
process.nextTick(emitErrorAndCloseNT, this, err);
const emitNT = emitClose ? emitErrorAndCloseNT : emitErrorNT;
process.nextTick(emitNT, this, err);
} else if (!this._writableState.errorEmitted) {
this._writableState.errorEmitted = true;
process.nextTick(emitErrorAndCloseNT, this, err);
} else {
const emitNT = emitClose ? emitErrorAndCloseNT : emitErrorNT;
process.nextTick(emitNT, this, err);
} else if (emitClose) {
process.nextTick(emitCloseNT, this);
}
} else if (cb) {
process.nextTick(emitCloseNT, this);
if (emitClose) {
process.nextTick(emitCloseNT, this);
}
cb(err);
} else {
} else if (emitClose) {
process.nextTick(emitCloseNT, this);
}
});
Expand All @@ -56,18 +62,18 @@ function destroy(err, cb) {
}

function emitErrorAndCloseNT(self, err) {
emitErrorNT(self, err);
emitCloseNT(self);
self.emit('error', err);
self.emit('close');
}

function emitCloseNT(self) {
if (self._writableState && !self._writableState.emitClose)
return;
if (self._readableState && !self._readableState.emitClose)
return;
self.emit('close');
}

function emitErrorNT(self, err) {
self.emit('error', err);
}

function undestroy() {
if (this._readableState) {
this._readableState.destroyed = false;
Expand All @@ -87,10 +93,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