Skip to content

Commit 7750eb2

Browse files
committed
stream: improve destroy readability
1 parent 1592d0a commit 7750eb2

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

lib/internal/streams/destroy.js

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@
22

33
// Undocumented cb() API, needed for core, not for public API
44
function destroy(err, cb) {
5-
const readableDestroyed = this._readableState &&
6-
this._readableState.destroyed;
7-
const writableDestroyed = this._writableState &&
8-
this._writableState.destroyed;
5+
const r = this._readableState;
6+
const w = this._writableState;
7+
8+
const readableDestroyed = r && r.destroyed;
9+
const writableDestroyed = w && w.destroyed;
910

1011
if (readableDestroyed || writableDestroyed) {
1112
if (cb) {
1213
cb(err);
1314
} else if (err) {
14-
if (!this._writableState) {
15+
if (!w) {
1516
process.nextTick(emitErrorNT, this, err);
16-
} else if (!this._writableState.errorEmitted) {
17-
this._writableState.errorEmitted = true;
17+
} else if (!w.errorEmitted) {
18+
w.errorEmitted = true;
1819
process.nextTick(emitErrorNT, this, err);
1920
}
2021
}
@@ -25,21 +26,21 @@ function destroy(err, cb) {
2526
// We set destroyed to true before firing error callbacks in order
2627
// to make it re-entrance safe in case destroy() is called within callbacks
2728

28-
if (this._readableState) {
29-
this._readableState.destroyed = true;
29+
if (r) {
30+
r.destroyed = true;
3031
}
3132

3233
// If this is a duplex stream mark the writable part as destroyed as well
33-
if (this._writableState) {
34-
this._writableState.destroyed = true;
34+
if (w) {
35+
w.destroyed = true;
3536
}
3637

3738
this._destroy(err || null, (err) => {
3839
if (!cb && err) {
39-
if (!this._writableState) {
40+
if (!w) {
4041
process.nextTick(emitErrorAndCloseNT, this, err);
41-
} else if (!this._writableState.errorEmitted) {
42-
this._writableState.errorEmitted = true;
42+
} else if (!w.errorEmitted) {
43+
w.errorEmitted = true;
4344
process.nextTick(emitErrorAndCloseNT, this, err);
4445
} else {
4546
process.nextTick(emitCloseNT, this);
@@ -61,29 +62,35 @@ function emitErrorAndCloseNT(self, err) {
6162
}
6263

6364
function emitCloseNT(self) {
64-
if (self._writableState && !self._writableState.emitClose)
65+
const r = self._readableState;
66+
const w = self._writableState;
67+
68+
if (w && !w.emitClose)
6569
return;
66-
if (self._readableState && !self._readableState.emitClose)
70+
if (r && !r.emitClose)
6771
return;
6872
self.emit('close');
6973
}
7074

7175
function undestroy() {
72-
if (this._readableState) {
73-
this._readableState.destroyed = false;
74-
this._readableState.reading = false;
75-
this._readableState.ended = false;
76-
this._readableState.endEmitted = false;
76+
const r = this._readableState;
77+
const w = this._writableState;
78+
79+
if (r) {
80+
r.destroyed = false;
81+
r.reading = false;
82+
r.ended = false;
83+
r.endEmitted = false;
7784
}
7885

79-
if (this._writableState) {
80-
this._writableState.destroyed = false;
81-
this._writableState.ended = false;
82-
this._writableState.ending = false;
83-
this._writableState.finalCalled = false;
84-
this._writableState.prefinished = false;
85-
this._writableState.finished = false;
86-
this._writableState.errorEmitted = false;
86+
if (w) {
87+
w.destroyed = false;
88+
w.ended = false;
89+
w.ending = false;
90+
w.finalCalled = false;
91+
w.prefinished = false;
92+
w.finished = false;
93+
w.errorEmitted = false;
8794
}
8895
}
8996

@@ -98,10 +105,10 @@ function errorOrDestroy(stream, err) {
98105
// the error to be emitted nextTick. In a future
99106
// semver major update we should change the default to this.
100107

101-
const rState = stream._readableState;
102-
const wState = stream._writableState;
108+
const r = stream._readableState;
109+
const w = stream._writableState;
103110

104-
if ((rState && rState.autoDestroy) || (wState && wState.autoDestroy))
111+
if ((r && r.autoDestroy) || (w && w.autoDestroy))
105112
stream.destroy(err);
106113
else
107114
stream.emit('error', err);

0 commit comments

Comments
 (0)