Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 47 additions & 29 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2330,19 +2330,53 @@ function visitAsyncNode(
>,
cutOff: number,
): void | null | PromiseNode | IONode {
if (visited.has(node)) {
// It's possible to visit them same node twice when it's part of both an "awaited" path
// and a "previous" path. This also gracefully handles cycles which would be a bug.
return visited.get(node);
}
// Set it as visited early in case we see ourselves before returning.
visited.set(node, null);
const result = visitAsyncNodeImpl(request, task, node, visited, cutOff);
if (result !== null) {
// If we ended up with a value, let's use that value for future visits.
visited.set(node, result);
// Collect the previous chain iteratively instead of recursively to avoid
// stack overflow on deep chains. We process from deepest to shallowest so
// each node has its previousIONode available.
const chain: Array<AsyncSequence> = [];
let current: AsyncSequence | null = node;

while (current !== null) {
if (visited.has(current)) {
break;
}
chain.push(current);
current = current.previous;
}
return result;

let previousIONode: void | null | PromiseNode | IONode =
current !== null ? visited.get(current) : null;

// Process from deepest to shallowest (reverse order).
for (let i = chain.length - 1; i >= 0; i--) {
const n = chain[i];
// Set it as visited early in case we see the node again before returning.
visited.set(n, null);

const result = visitAsyncNodeImpl(
request,
task,
n,
visited,
cutOff,
previousIONode,
);

if (result !== null) {
// If we ended up with a value, let's use that value for future visits.
visited.set(n, result);
}

if (result === undefined) {
// Undefined is used as a signal that we found a suitable aborted node
// and we don't have to find further aborted nodes.
return undefined;
}

previousIONode = result;
}

return previousIONode;
}

function visitAsyncNodeImpl(
Expand All @@ -2354,6 +2388,7 @@ function visitAsyncNodeImpl(
void | null | PromiseNode | IONode,
>,
cutOff: number,
previousIONode: void | null | PromiseNode | IONode,
): void | null | PromiseNode | IONode {
if (node.end >= 0 && node.end <= request.timeOrigin) {
// This was already resolved when we started this render. It must have been either something
Expand All @@ -2362,23 +2397,6 @@ function visitAsyncNodeImpl(
return null;
}

let previousIONode: void | null | PromiseNode | IONode = null;
// First visit anything that blocked this sequence to start in the first place.
if (node.previous !== null) {
previousIONode = visitAsyncNode(
request,
task,
node.previous,
visited,
cutOff,
);
if (previousIONode === undefined) {
// Undefined is used as a signal that we found a suitable aborted node and we don't have to find
// further aborted nodes.
return undefined;
}
}

// `found` represents the return value of the following switch statement.
// We can't use multiple `return` statements in the switch statement
// since that prevents Closure compiler from inlining `visitAsyncImpl`
Expand Down
Loading
Loading