@@ -97,8 +97,21 @@ const dependantSignalsCleanupRegistry = new SafeFinalizationRegistry((signalWeak
97
97
}
98
98
} ) ;
99
99
} ) ;
100
+
100
101
const gcPersistentSignals = new SafeSet ( ) ;
101
102
103
+ const sourceSignalsCleanupRegistry = new SafeFinalizationRegistry ( ( { sourceSignalRef, composedSignalRef } ) => {
104
+ const composedSignal = composedSignalRef . deref ( ) ;
105
+ if ( composedSignal !== undefined ) {
106
+ composedSignal [ kSourceSignals ] . delete ( sourceSignalRef ) ;
107
+
108
+ if ( composedSignal [ kSourceSignals ] . size === 0 ) {
109
+ // This signal will no longer abort. There's no need to keep it in the gcPersistentSignals set.
110
+ gcPersistentSignals . delete ( composedSignal ) ;
111
+ }
112
+ }
113
+ } ) ;
114
+
102
115
const kAborted = Symbol ( 'kAborted' ) ;
103
116
const kReason = Symbol ( 'kReason' ) ;
104
117
const kCloneData = Symbol ( 'kCloneData' ) ;
@@ -261,6 +274,10 @@ class AbortSignal extends EventTarget {
261
274
resultSignal [ kSourceSignals ] . add ( signalWeakRef ) ;
262
275
signal [ kDependantSignals ] . add ( resultSignalWeakRef ) ;
263
276
dependantSignalsCleanupRegistry . register ( resultSignal , signalWeakRef ) ;
277
+ sourceSignalsCleanupRegistry . register ( signal , {
278
+ sourceSignalRef : signalWeakRef ,
279
+ composedSignalRef : resultSignalWeakRef ,
280
+ } ) ;
264
281
} else if ( ! signal [ kSourceSignals ] ) {
265
282
continue ;
266
283
} else {
@@ -278,6 +295,10 @@ class AbortSignal extends EventTarget {
278
295
resultSignal [ kSourceSignals ] . add ( sourceSignalWeakRef ) ;
279
296
sourceSignal [ kDependantSignals ] . add ( resultSignalWeakRef ) ;
280
297
dependantSignalsCleanupRegistry . register ( resultSignal , sourceSignalWeakRef ) ;
298
+ sourceSignalsCleanupRegistry . register ( signal , {
299
+ sourceSignalRef : sourceSignalWeakRef ,
300
+ composedSignalRef : resultSignalWeakRef ,
301
+ } ) ;
281
302
}
282
303
}
283
304
}
@@ -434,6 +455,7 @@ class AbortController {
434
455
*/
435
456
get signal ( ) {
436
457
this . #signal ??= new AbortSignal ( kDontThrowSymbol ) ;
458
+
437
459
return this . #signal;
438
460
}
439
461
0 commit comments