@@ -461,7 +461,7 @@ const kFlagResistStopPropagation = 1 << 6;
461461// slower.
462462class Listener {
463463 constructor ( eventTarget , eventType , previous , listener , once , capture , passive ,
464- isNodeStyleListener , weak , resistStopPropagation ) {
464+ isNodeStyleListener , weak , resistStopPropagation , signal , onAbort ) {
465465 this . next = undefined ;
466466 if ( previous !== undefined )
467467 previous . next = this ;
@@ -514,6 +514,8 @@ class Listener {
514514 } ;
515515 this . listener = listener ;
516516 }
517+ this . signalRef = signal === undefined ? undefined : new SafeWeakRef ( signal ) ;
518+ this . onAbort = onAbort ;
517519 }
518520
519521 get once ( ) {
@@ -556,6 +558,7 @@ class Listener {
556558 if ( this . next !== undefined )
557559 this . next . previous = this . previous ;
558560 this . flags |= kFlagRemoved ;
561+ this . detachSignal ( ) ;
559562 if ( ( this . flags & kFlagWeak ) !== 0 ) {
560563 weakListeners ( ) . registry . unregister ( this ) ;
561564 const weakKey = this . weakKeyRef ?. deref ( ) ;
@@ -570,6 +573,19 @@ class Listener {
570573 }
571574 }
572575 }
576+
577+ detachSignal ( ) {
578+ const onAbort = this . onAbort ;
579+ if ( onAbort === undefined )
580+ return ;
581+ this . onAbort = undefined ;
582+ this . signalRef . deref ( ) ?. removeEventListener ( 'abort' , onAbort ) ;
583+ }
584+ }
585+
586+ function detachSignals ( root ) {
587+ for ( let handler = root ?. next ; handler !== undefined ; handler = handler . next )
588+ handler . detachSignal ( ) ;
573589}
574590
575591function initEventTarget ( self ) {
@@ -646,6 +662,8 @@ class EventTarget {
646662 let isNodeStyleListener = false ;
647663 let weak = false ;
648664 let resistStopPropagation = false ;
665+ let signal ;
666+ let onAbort ;
649667
650668 // We flatten the options before the validateListener check because the
651669 // spec requires us to hit getters.
@@ -656,7 +674,7 @@ class EventTarget {
656674 once = Boolean ( options . once ) ;
657675 capture = Boolean ( options . capture ) ;
658676 passive = Boolean ( options . passive ) ;
659- const signal = options . signal ;
677+ signal = options . signal ;
660678 weak = options [ kWeakHandler ] ;
661679 resistStopPropagation = options [ kResistStopPropagation ] ?? false ;
662680 isNodeStyleListener = Boolean ( options [ kIsNodeStyleListener ] ) ;
@@ -670,9 +688,11 @@ class EventTarget {
670688 // Capture a copy so that the closure doesn't move `capture` into the
671689 // function context, which slows down the listener lookup below.
672690 const removeCapture = capture ;
673- signal . addEventListener ( 'abort' , ( ) => {
691+ onAbort = ( ) => {
674692 this . removeEventListener ( type , listener , removeCapture ) ;
675- } , { __proto__ : null , once : true , [ kWeakHandler ] : this , [ kResistStopPropagation ] : true } ) ;
693+ } ;
694+ signal . addEventListener ( 'abort' , onAbort ,
695+ { __proto__ : null , once : true , [ kWeakHandler ] : this , [ kResistStopPropagation ] : true } ) ;
676696 }
677697 }
678698
@@ -698,7 +718,7 @@ class EventTarget {
698718 root = { size : 1 , next : undefined , resistStopPropagation : Boolean ( resistStopPropagation ) } ;
699719 // This is the first handler in our linked list.
700720 new Listener ( this , type , root , listener , once , capture , passive ,
701- isNodeStyleListener , weak , resistStopPropagation ) ;
721+ isNodeStyleListener , weak , resistStopPropagation , signal , onAbort ) ;
702722 this [ kNewListener ] (
703723 root . size ,
704724 type ,
@@ -725,7 +745,7 @@ class EventTarget {
725745 }
726746
727747 new Listener ( this , type , previous , listener , once , capture , passive ,
728- isNodeStyleListener , weak , resistStopPropagation ) ;
748+ isNodeStyleListener , weak , resistStopPropagation , signal , onAbort ) ;
729749 root . size ++ ;
730750 root . resistStopPropagation ||= Boolean ( resistStopPropagation ) ;
731751 this [ kNewListener ] ( root . size , type , listener , once , capture , passive , weak ) ;
@@ -1068,10 +1088,15 @@ class NodeEventTarget extends EventTarget {
10681088 throw new ERR_INVALID_THIS ( 'NodeEventTarget' ) ;
10691089 if ( this [ kEvents ] === undefined )
10701090 return this ;
1071- if ( type !== undefined )
1072- this [ kEvents ] . delete ( String ( type ) ) ;
1073- else
1091+ if ( type !== undefined ) {
1092+ type = String ( type ) ;
1093+ detachSignals ( this [ kEvents ] . get ( type ) ) ;
1094+ this [ kEvents ] . delete ( type ) ;
1095+ } else {
1096+ for ( const { 1 : root } of this [ kEvents ] )
1097+ detachSignals ( root ) ;
10741098 this [ kEvents ] . clear ( ) ;
1099+ }
10751100
10761101 return this ;
10771102 }
0 commit comments