Skip to content

Commit 5eb78d0

Browse files
committed
Pass ThenableState to replaySuspendedUnitOfWork
Tiny refactor to refine the work loop variable so Flow knows it's not null when we access it in replaySuspendedUnitOfWork.
1 parent 4a2d86b commit 5eb78d0

File tree

2 files changed

+80
-52
lines changed

2 files changed

+80
-52
lines changed

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,24 +2202,29 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
22022202
break;
22032203
}
22042204
case SuspendedOnData: {
2205-
const didResolve =
2206-
workInProgressSuspendedThenableState !== null &&
2207-
isThenableStateResolved(workInProgressSuspendedThenableState);
2208-
if (didResolve) {
2209-
workInProgressSuspendedReason = NotSuspended;
2210-
workInProgressThrownValue = null;
2211-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2212-
} else {
2213-
// The work loop is suspended on data. We should wait for it to
2214-
// resolve before continuing to render.
2215-
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2216-
const onResolution = () => {
2217-
ensureRootIsScheduled(root, now());
2218-
};
2219-
thenable.then(onResolution, onResolution);
2220-
break outer;
2205+
if (workInProgressSuspendedThenableState !== null) {
2206+
const thenableState = workInProgressSuspendedThenableState;
2207+
if (isThenableStateResolved(thenableState)) {
2208+
// The data resolved. Try rendering the component again.
2209+
workInProgressSuspendedReason = NotSuspended;
2210+
workInProgressThrownValue = null;
2211+
replaySuspendedUnitOfWork(
2212+
unitOfWork,
2213+
thrownValue,
2214+
thenableState,
2215+
);
2216+
break;
2217+
}
22212218
}
2222-
break;
2219+
2220+
// The work loop is suspended on data. We should wait for it to
2221+
// resolve before continuing to render.
2222+
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2223+
const onResolution = () => {
2224+
ensureRootIsScheduled(root, now());
2225+
};
2226+
thenable.then(onResolution, onResolution);
2227+
break outer;
22232228
}
22242229
case SuspendedOnImmediate: {
22252230
// If this fiber just suspended, it's possible the data is already
@@ -2237,17 +2242,25 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
22372242
break outer;
22382243
}
22392244
default: {
2245+
if (workInProgressSuspendedThenableState !== null) {
2246+
const thenableState = workInProgressSuspendedThenableState;
2247+
if (isThenableStateResolved(thenableState)) {
2248+
// The data resolved. Try rendering the component again.
2249+
workInProgressSuspendedReason = NotSuspended;
2250+
workInProgressThrownValue = null;
2251+
replaySuspendedUnitOfWork(
2252+
unitOfWork,
2253+
thrownValue,
2254+
thenableState,
2255+
);
2256+
break;
2257+
}
2258+
}
2259+
2260+
// Otherwise, unwind then continue with the normal work loop.
22402261
workInProgressSuspendedReason = NotSuspended;
22412262
workInProgressThrownValue = null;
2242-
const didResolve =
2243-
workInProgressSuspendedThenableState !== null &&
2244-
isThenableStateResolved(workInProgressSuspendedThenableState);
2245-
if (didResolve) {
2246-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2247-
} else {
2248-
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
2249-
}
2250-
// Continue with the normal work loop.
2263+
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
22512264
break;
22522265
}
22532266
}
@@ -2335,6 +2348,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
23352348
function replaySuspendedUnitOfWork(
23362349
unitOfWork: Fiber,
23372350
thrownValue: mixed,
2351+
thenableState: ThenableState,
23382352
): void {
23392353
// This is a fork of performUnitOfWork specifcally for replaying a fiber that
23402354
// just suspended.

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,24 +2202,29 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
22022202
break;
22032203
}
22042204
case SuspendedOnData: {
2205-
const didResolve =
2206-
workInProgressSuspendedThenableState !== null &&
2207-
isThenableStateResolved(workInProgressSuspendedThenableState);
2208-
if (didResolve) {
2209-
workInProgressSuspendedReason = NotSuspended;
2210-
workInProgressThrownValue = null;
2211-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2212-
} else {
2213-
// The work loop is suspended on data. We should wait for it to
2214-
// resolve before continuing to render.
2215-
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2216-
const onResolution = () => {
2217-
ensureRootIsScheduled(root, now());
2218-
};
2219-
thenable.then(onResolution, onResolution);
2220-
break outer;
2205+
if (workInProgressSuspendedThenableState !== null) {
2206+
const thenableState = workInProgressSuspendedThenableState;
2207+
if (isThenableStateResolved(thenableState)) {
2208+
// The data resolved. Try rendering the component again.
2209+
workInProgressSuspendedReason = NotSuspended;
2210+
workInProgressThrownValue = null;
2211+
replaySuspendedUnitOfWork(
2212+
unitOfWork,
2213+
thrownValue,
2214+
thenableState,
2215+
);
2216+
break;
2217+
}
22212218
}
2222-
break;
2219+
2220+
// The work loop is suspended on data. We should wait for it to
2221+
// resolve before continuing to render.
2222+
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2223+
const onResolution = () => {
2224+
ensureRootIsScheduled(root, now());
2225+
};
2226+
thenable.then(onResolution, onResolution);
2227+
break outer;
22232228
}
22242229
case SuspendedOnImmediate: {
22252230
// If this fiber just suspended, it's possible the data is already
@@ -2237,17 +2242,25 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
22372242
break outer;
22382243
}
22392244
default: {
2245+
if (workInProgressSuspendedThenableState !== null) {
2246+
const thenableState = workInProgressSuspendedThenableState;
2247+
if (isThenableStateResolved(thenableState)) {
2248+
// The data resolved. Try rendering the component again.
2249+
workInProgressSuspendedReason = NotSuspended;
2250+
workInProgressThrownValue = null;
2251+
replaySuspendedUnitOfWork(
2252+
unitOfWork,
2253+
thrownValue,
2254+
thenableState,
2255+
);
2256+
break;
2257+
}
2258+
}
2259+
2260+
// Otherwise, unwind then continue with the normal work loop.
22402261
workInProgressSuspendedReason = NotSuspended;
22412262
workInProgressThrownValue = null;
2242-
const didResolve =
2243-
workInProgressSuspendedThenableState !== null &&
2244-
isThenableStateResolved(workInProgressSuspendedThenableState);
2245-
if (didResolve) {
2246-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2247-
} else {
2248-
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
2249-
}
2250-
// Continue with the normal work loop.
2263+
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
22512264
break;
22522265
}
22532266
}
@@ -2335,6 +2348,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
23352348
function replaySuspendedUnitOfWork(
23362349
unitOfWork: Fiber,
23372350
thrownValue: mixed,
2351+
thenableState: ThenableState,
23382352
): void {
23392353
// This is a fork of performUnitOfWork specifcally for replaying a fiber that
23402354
// just suspended.

0 commit comments

Comments
 (0)