File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed
Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -525,7 +525,9 @@ class Stream extends EventEmitter {
525525 if ( opts . signal . aborted ) {
526526 throw new Error ( 'Stream aborted.' )
527527 }
528- opts . signal . addEventListener ( 'abort' , abort . bind ( this ) )
528+ const handler = abort . bind ( this )
529+ this . _clearSignal = clearSignal . bind ( null , opts . signal , handler )
530+ opts . signal . addEventListener ( 'abort' , handler )
529531 }
530532 }
531533 }
@@ -571,6 +573,9 @@ class Stream extends EventEmitter {
571573 this . _writableState . updateNextTick ( )
572574 }
573575 this . _predestroy ( )
576+ if ( this . _clearSignal !== undefined ) {
577+ this . _clearSignal ( )
578+ }
574579 }
575580 }
576581
@@ -976,6 +981,10 @@ function abort () {
976981 this . destroy ( new Error ( 'Stream aborted.' ) )
977982}
978983
984+ function clearSignal ( signal , handler ) {
985+ signal . removeEventListener ( 'abort' , handler )
986+ }
987+
979988module . exports = {
980989 pipeline,
981990 pipelinePromise,
Original file line number Diff line number Diff line change @@ -158,6 +158,8 @@ tape('using abort controller', async function (t) {
158158 return r
159159 }
160160 const controller = new AbortController ( )
161+ let removeCalled = false
162+ beforeFn ( controller . signal , 'removeEventListener' , ( ) => { removeCalled = true } )
161163 const inc = [ ]
162164 setTimeout ( ( ) => controller . abort ( ) , 10 )
163165 try {
@@ -167,8 +169,17 @@ tape('using abort controller', async function (t) {
167169 } catch ( err ) {
168170 t . same ( err . message , 'Stream aborted.' )
169171 }
172+ t . same ( removeCalled , true )
170173 t . same ( inc , [ 0 ] )
171174 t . end ( )
175+
176+ function beforeFn ( obj , fnName , handler ) {
177+ const orig = obj [ fnName ]
178+ obj [ fnName ] = function ( ) {
179+ handler . apply ( this , arguments )
180+ orig . apply ( this , arguments )
181+ }
182+ }
172183} )
173184tape ( 'using aborted abort controller' , async function ( t ) {
174185 const controller = new AbortController ( )
You can’t perform that action at this time.
0 commit comments