@@ -70,12 +70,16 @@ function destroy(err, cb) {
7070
7171function _destroy ( self , err , cb ) {
7272 let called = false ;
73- const result = self . _destroy ( err || null , ( err ) => {
74- const r = self . _readableState ;
75- const w = self . _writableState ;
7673
74+ function onDestroy ( err ) {
75+ if ( called ) {
76+ return ;
77+ }
7778 called = true ;
7879
80+ const r = self . _readableState ;
81+ const w = self . _writableState ;
82+
7983 checkError ( err , w , r ) ;
8084
8185 if ( w ) {
@@ -94,64 +98,24 @@ function _destroy(self, err, cb) {
9498 } else {
9599 process . nextTick ( emitCloseNT , self ) ;
96100 }
97- } ) ;
98- if ( result !== undefined && result !== null ) {
99- try {
101+ }
102+ try {
103+ const result = self . _destroy ( err || null , onDestroy ) ;
104+ if ( result != null ) {
100105 const then = result . then ;
101106 if ( typeof then === 'function' ) {
102107 then . call (
103108 result ,
104109 function ( ) {
105- if ( called )
106- return ;
107-
108- const r = self . _readableState ;
109- const w = self . _writableState ;
110-
111- if ( w ) {
112- w . closed = true ;
113- }
114- if ( r ) {
115- r . closed = true ;
116- }
117-
118- if ( typeof cb === 'function' ) {
119- process . nextTick ( cb ) ;
120- }
121-
122- process . nextTick ( emitCloseNT , self ) ;
110+ process . nextTick ( onDestroy , null ) ;
123111 } ,
124112 function ( err ) {
125- const r = self . _readableState ;
126- const w = self . _writableState ;
127- err . stack ; // eslint-disable-line no-unused-expressions
128-
129- called = true ;
130-
131- if ( w && ! w . errored ) {
132- w . errored = err ;
133- }
134- if ( r && ! r . errored ) {
135- r . errored = err ;
136- }
137-
138- if ( w ) {
139- w . closed = true ;
140- }
141- if ( r ) {
142- r . closed = true ;
143- }
144-
145- if ( typeof cb === 'function' ) {
146- process . nextTick ( cb , err ) ;
147- }
148-
149- process . nextTick ( emitErrorCloseNT , self , err ) ;
113+ process . nextTick ( onDestroy , err ) ;
150114 } ) ;
151115 }
152- } catch ( err ) {
153- process . nextTick ( emitErrorNT , self , err ) ;
154116 }
117+ } catch ( err ) {
118+ onDestroy ( err ) ;
155119 }
156120}
157121
@@ -285,74 +249,52 @@ function construct(stream, cb) {
285249}
286250
287251function constructNT ( stream ) {
288- const r = stream . _readableState ;
289- const w = stream . _writableState ;
290- // With duplex streams we use the writable side for state.
291- const s = w || r ;
292-
293252 let called = false ;
294- const result = stream . _construct ( ( err ) => {
253+
254+ function onConstruct ( err ) {
255+ if ( called ) {
256+ errorOrDestroy ( stream , err ?? new ERR_MULTIPLE_CALLBACK ( ) ) ;
257+ return ;
258+ }
259+ called = true ;
260+
261+ const r = stream . _readableState ;
262+ const w = stream . _writableState ;
263+ const s = w || r ;
264+
295265 if ( r ) {
296266 r . constructed = true ;
297267 }
298268 if ( w ) {
299269 w . constructed = true ;
300270 }
301271
302- if ( called ) {
303- err = new ERR_MULTIPLE_CALLBACK ( ) ;
304- } else {
305- called = true ;
306- }
307-
308272 if ( s . destroyed ) {
309273 stream . emit ( kDestroy , err ) ;
310274 } else if ( err ) {
311275 errorOrDestroy ( stream , err , true ) ;
312276 } else {
313277 process . nextTick ( emitConstructNT , stream ) ;
314278 }
315- } ) ;
316- if ( result !== undefined && result !== null ) {
317- try {
279+ }
280+
281+ try {
282+ const result = stream . _construct ( onConstruct ) ;
283+ if ( result != null ) {
318284 const then = result . then ;
319285 if ( typeof then === 'function' ) {
320286 then . call (
321287 result ,
322288 function ( ) {
323- // If the callback was invoked, do nothing further.
324- if ( called )
325- return ;
326- if ( r ) {
327- r . constructed = true ;
328- }
329- if ( w ) {
330- w . constructed = true ;
331- }
332- if ( s . destroyed ) {
333- process . nextTick ( ( ) => stream . emit ( kDestroy ) ) ;
334- } else {
335- process . nextTick ( emitConstructNT , stream ) ;
336- }
289+ process . nextTick ( onConstruct , null ) ;
337290 } ,
338291 function ( err ) {
339- if ( r ) {
340- r . constructed = true ;
341- }
342- if ( w ) {
343- w . constructed = true ;
344- }
345- called = true ;
346- if ( s . destroyed ) {
347- process . nextTick ( ( ) => stream . emit ( kDestroy , err ) ) ;
348- } else {
349- process . nextTick ( errorOrDestroy , stream , err ) ;
350- }
292+ process . nextTick ( onConstruct , err ) ;
351293 } ) ;
352294 }
353- } catch ( err ) {
354- process . nextTick ( emitErrorNT , stream , err ) ;
355295 }
296+ } catch ( err ) {
297+ onConstruct ( err ) ;
356298 }
357299}
358300
0 commit comments