@@ -109,16 +109,15 @@ const waitForReadyAfterBundleRequest = async (options: {
109109 events : MetroInstance [ 'events' ] ;
110110 readyTimeout : number ;
111111 signal : AbortSignal ;
112- waitForReady : ( signal : AbortSignal ) => Promise < void > ;
112+ readyPromise : Promise < void > ;
113+ cancelReadyWait : ( ) => void ;
113114} ) : Promise < void > => {
114- const { events, readyTimeout, signal, waitForReady } = options ;
115+ const { events, readyTimeout, signal, readyPromise , cancelReadyWait } = options ;
115116
116117 return await new Promise < void > ( ( resolve , reject ) => {
117118 let bundlingInProgress = false ;
118119 let settled = false ;
119120 let timeoutId : ReturnType < typeof setTimeout > | null = null ;
120- const readyController = new AbortController ( ) ;
121- const readySignal = raceAbortSignals ( [ signal , readyController . signal ] ) ;
122121
123122 const clearReadyTimer = ( ) => {
124123 if ( timeoutId ) {
@@ -156,7 +155,7 @@ const waitForReadyAfterBundleRequest = async (options: {
156155 const startReadyTimer = ( ) => {
157156 clearReadyTimer ( ) ;
158157 timeoutId = setTimeout ( ( ) => {
159- readyController . abort ( new DOMException ( 'The operation was aborted' , 'AbortError' ) ) ;
158+ cancelReadyWait ( ) ;
160159 rejectOnce ( new ReadyTimeoutError ( ) ) ;
161160 } , readyTimeout ) ;
162161 } ;
@@ -186,17 +185,20 @@ const waitForReadyAfterBundleRequest = async (options: {
186185 events . addListener ( onMetroEvent ) ;
187186 signal . addEventListener ( 'abort' , onAbort , { once : true } ) ;
188187
189- void waitForReady ( readySignal )
188+ void readyPromise
190189 . then ( ( ) => {
191190 resolveOnce ( ) ;
192191 } )
193192 . catch ( ( error ) => {
194193 if (
195- readyController . signal . aborted &&
196- ! signal . aborted &&
197194 error instanceof DOMException &&
198195 error . name === 'AbortError'
199196 ) {
197+ if ( signal . aborted ) {
198+ rejectOnce (
199+ signal . reason ?? new DOMException ( 'The operation was aborted' , 'AbortError' )
200+ ) ;
201+ }
200202 return ;
201203 }
202204
@@ -246,6 +248,10 @@ export const waitForMetroBackedAppReady = async ({
246248 const attemptController = new AbortController ( ) ;
247249 const attemptSignal = raceAbortSignals ( [ signal , attemptController . signal ] ) ;
248250 const crashPromise = waitForCrash ( attemptSignal ) ;
251+ const readyController = new AbortController ( ) ;
252+ const readyPromise = waitForReady (
253+ raceAbortSignals ( [ attemptSignal , readyController . signal ] )
254+ ) ;
249255
250256 try {
251257 const bundleRequestPromise = waitForBundleRequest ( {
@@ -264,17 +270,25 @@ export const waitForMetroBackedAppReady = async ({
264270 ] ) ;
265271 sawPrewarmRequest = bundleRequestResult . sawPrewarmRequest ;
266272
267- const readyPromise = waitForReadyAfterBundleRequest ( {
273+ const readyAfterBundleRequestPromise = waitForReadyAfterBundleRequest ( {
268274 events : metro . events ,
269275 readyTimeout,
270276 signal : attemptSignal ,
271- waitForReady,
277+ readyPromise,
278+ cancelReadyWait : ( ) => {
279+ readyController . abort (
280+ new DOMException ( 'The operation was aborted' , 'AbortError' )
281+ ) ;
282+ } ,
272283 } ) ;
273- await Promise . race ( [ readyPromise , crashPromise ] ) ;
284+ await Promise . race ( [ readyAfterBundleRequestPromise , crashPromise ] ) ;
274285 attemptController . abort ( ) ;
275286 onAttemptReset ?.( ) ;
276287 return ;
277288 } catch ( error ) {
289+ readyController . abort (
290+ new DOMException ( 'The operation was aborted' , 'AbortError' )
291+ ) ;
278292 attemptController . abort ( ) ;
279293 onAttemptReset ?.( ) ;
280294
0 commit comments