@@ -44,6 +44,12 @@ import {AbortController} from './AbortController';
4444const ABORTED_KEY : symbol = Symbol ( 'aborted' ) ;
4545const REASON_KEY : symbol = Symbol ( 'reason' ) ;
4646
47+ const dependentSignals : WeakMap < AbortSignal , Set < AbortSignal > > = new WeakMap ( ) ;
48+ const sourceSignalRefs : WeakMap <
49+ AbortSignal ,
50+ ReadonlyArray < WeakRef < AbortSignal > > ,
51+ > = new WeakMap ( ) ;
52+
4753/**
4854 * The signal class.
4955 * @see https://dom.spec.whatwg.org/#abortsignal
@@ -113,22 +119,37 @@ export class AbortSignal extends EventTarget {
113119 }
114120 }
115121
116- // Third pass: none of the signals is aborted, so subscribe to all of them
117- // and abort the resulting signal when any of them aborts.
118- const controller = new AbortController ( ) ;
119- const listeners = [ ] ;
120- const cleanup = ( ) => listeners . forEach ( unsubscribe => unsubscribe ( ) ) ;
122+ const resultSignal = createAbortSignal ( ) ;
123+ const resultSources : Set < AbortSignal > = new Set();
121124
122125 for (let i = 0; i < signals . length ; i ++ ) {
123126 const signal = signals [ i ] ;
124- const onAbort = ( ) => {
125- controller . abort ( signal . reason ) ;
126- cleanup ( ) ;
127- } ;
128- signal . addEventListener ( 'abort' , onAbort ) ;
129- listeners . push ( ( ) => signal . removeEventListener ( 'abort' , onAbort ) ) ;
127+ const sourceRefs = sourceSignalRefs . get ( signal ) ;
128+ if ( sourceRefs == null ) {
129+ resultSources . add ( signal ) ;
130+ } else {
131+ for ( const sourceRef of sourceRefs ) {
132+ const sourceSignal = sourceRef . deref ( ) ;
133+ if ( sourceSignal != null ) {
134+ resultSources . add ( sourceSignal ) ;
135+ }
136+ }
137+ }
130138 }
131- return controller . signal ;
139+
140+ const resultSourceRefs : Array < WeakRef < AbortSignal > > = [ ] ;
141+ for ( const sourceSignal of resultSources ) {
142+ let dependents = dependentSignals . get ( sourceSignal ) ;
143+ if ( dependents == null ) {
144+ dependents = new Set ( ) ;
145+ dependentSignals . set ( sourceSignal , dependents ) ;
146+ }
147+ dependents . add ( resultSignal ) ;
148+ resultSourceRefs . push ( new WeakRef ( sourceSignal ) ) ;
149+ }
150+ sourceSignalRefs . set ( resultSignal , resultSourceRefs ) ;
151+
152+ return resultSignal ;
132153 }
133154
134155 // $FlowExpectedError[unsupported-syntax]
@@ -201,7 +222,43 @@ export function abortSignal(
201222 signal [ ABORTED_KEY ] = true ;
202223 // $FlowExpectedError[prop-missing]
203224 signal [ REASON_KEY ] = reason ;
225+
226+ const dependentSignalsToAbort : Array < AbortSignal > = [];
227+ const dependents = dependentSignals.get(signal);
228+ if (dependents != null) {
229+ dependentSignals . delete ( signal ) ;
230+ for ( const dependentSignal of dependents ) {
231+ // $FlowExpectedError[invalid-compare]
232+ // $FlowExpectedError[prop-missing]
233+ if ( dependentSignal [ ABORTED_KEY ] === false ) {
234+ // $FlowExpectedError[prop-missing]
235+ dependentSignal [ ABORTED_KEY ] = true ;
236+ // $FlowExpectedError[prop-missing]
237+ dependentSignal [ REASON_KEY ] = reason ;
238+ unregisterDependentSignal ( dependentSignal ) ;
239+ dependentSignalsToAbort . push ( dependentSignal ) ;
240+ }
241+ }
242+ }
243+
204244 dispatchTrustedEvent ( signal , new Event ( 'abort' ) ) ;
245+ for ( const dependentSignal of dependentSignalsToAbort ) {
246+ dispatchTrustedEvent ( dependentSignal , new Event ( 'abort' ) ) ;
247+ }
248+ }
249+
250+ function unregisterDependentSignal ( dependentSignal : AbortSignal ) : void {
251+ const sourceRefs = sourceSignalRefs . get ( dependentSignal ) ;
252+ if ( sourceRefs == null ) {
253+ return ;
254+ }
255+ sourceSignalRefs . delete ( dependentSignal ) ;
256+ for ( const sourceRef of sourceRefs ) {
257+ const sourceSignal = sourceRef . deref ( ) ;
258+ if ( sourceSignal != null ) {
259+ dependentSignals . get ( sourceSignal ) ?. delete ( dependentSignal ) ;
260+ }
261+ }
205262}
206263
207264// Properties should be enumerable.
0 commit comments