@@ -2345,19 +2345,53 @@ function visitAsyncNode(
23452345 > ,
23462346 cutOff: number,
23472347): void | null | PromiseNode | IONode {
2348- if ( visited . has ( node ) ) {
2349- // It's possible to visit them same node twice when it's part of both an "awaited" path
2350- // and a "previous" path. This also gracefully handles cycles which would be a bug.
2351- return visited . get ( node ) ;
2352- }
2353- // Set it as visited early in case we see ourselves before returning.
2354- visited. set ( node , null ) ;
2355- const result = visitAsyncNodeImpl ( request , task , node , visited , cutOff ) ;
2356- if ( result !== null ) {
2357- // If we ended up with a value, let's use that value for future visits.
2358- visited . set ( node , result ) ;
2348+ // Collect the previous chain iteratively instead of recursively to avoid
2349+ // stack overflow on deep chains. We process from deepest to shallowest so
2350+ // each node has its previousIONode available.
2351+ const chain : Array < AsyncSequence > = [ ] ;
2352+ let current : AsyncSequence | null = node ;
2353+
2354+ while ( current !== null ) {
2355+ if ( visited . has ( current ) ) {
2356+ break ;
2357+ }
2358+ chain . push ( current ) ;
2359+ current = current . previous ;
23592360 }
2360- return result;
2361+
2362+ let previousIONode : void | null | PromiseNode | IONode =
2363+ current !== null ? visited . get ( current ) : null ;
2364+
2365+ // Process from deepest to shallowest (reverse order).
2366+ for ( let i = chain . length - 1 ; i >= 0 ; i -- ) {
2367+ const n = chain [ i ] ;
2368+ // Set it as visited early in case we see the node again before returning.
2369+ visited . set ( n , null ) ;
2370+
2371+ const result = visitAsyncNodeImpl (
2372+ request ,
2373+ task ,
2374+ n ,
2375+ visited ,
2376+ cutOff ,
2377+ previousIONode ,
2378+ ) ;
2379+
2380+ if ( result !== null ) {
2381+ // If we ended up with a value, let's use that value for future visits.
2382+ visited. set ( n , result ) ;
2383+ }
2384+
2385+ if ( result === undefined ) {
2386+ // Undefined is used as a signal that we found a suitable aborted node
2387+ // and we don't have to find further aborted nodes.
2388+ return undefined ;
2389+ }
2390+
2391+ previousIONode = result;
2392+ }
2393+
2394+ return previousIONode ;
23612395}
23622396
23632397function visitAsyncNodeImpl (
@@ -2369,6 +2403,7 @@ function visitAsyncNodeImpl(
23692403 void | null | PromiseNode | IONode ,
23702404 > ,
23712405 cutOff: number,
2406+ previousIONode: void | null | PromiseNode | IONode,
23722407): void | null | PromiseNode | IONode {
23732408 if ( node . end >= 0 && node . end <= request . timeOrigin ) {
23742409 // This was already resolved when we started this render. It must have been either something
@@ -2377,23 +2412,6 @@ function visitAsyncNodeImpl(
23772412 return null ;
23782413 }
23792414
2380- let previousIONode : void | null | PromiseNode | IONode = null ;
2381- // First visit anything that blocked this sequence to start in the first place.
2382- if ( node . previous !== null ) {
2383- previousIONode = visitAsyncNode (
2384- request ,
2385- task ,
2386- node . previous ,
2387- visited ,
2388- cutOff ,
2389- ) ;
2390- if ( previousIONode === undefined ) {
2391- // Undefined is used as a signal that we found a suitable aborted node and we don't have to find
2392- // further aborted nodes.
2393- return undefined ;
2394- }
2395- }
2396-
23972415 // `found` represents the return value of the following switch statement.
23982416 // We can't use multiple `return` statements in the switch statement
23992417 // since that prevents Closure compiler from inlining `visitAsyncImpl`
0 commit comments