Skip to content

Commit f408be7

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 6968480 commit f408be7

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
@@ -2165,24 +2165,29 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
21652165
break;
21662166
}
21672167
case SuspendedOnData: {
2168-
const didResolve =
2169-
workInProgressSuspendedThenableState !== null &&
2170-
isThenableStateResolved(workInProgressSuspendedThenableState);
2171-
if (didResolve) {
2172-
workInProgressSuspendedReason = NotSuspended;
2173-
workInProgressThrownValue = null;
2174-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2175-
} else {
2176-
// The work loop is suspended on data. We should wait for it to
2177-
// resolve before continuing to render.
2178-
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2179-
const onResolution = () => {
2180-
ensureRootIsScheduled(root, now());
2181-
};
2182-
thenable.then(onResolution, onResolution);
2183-
break outer;
2168+
if (workInProgressSuspendedThenableState !== null) {
2169+
const thenableState = workInProgressSuspendedThenableState;
2170+
if (isThenableStateResolved(thenableState)) {
2171+
// The data resolved. Try rendering the component again.
2172+
workInProgressSuspendedReason = NotSuspended;
2173+
workInProgressThrownValue = null;
2174+
replaySuspendedUnitOfWork(
2175+
unitOfWork,
2176+
thrownValue,
2177+
thenableState,
2178+
);
2179+
break;
2180+
}
21842181
}
2185-
break;
2182+
2183+
// The work loop is suspended on data. We should wait for it to
2184+
// resolve before continuing to render.
2185+
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2186+
const onResolution = () => {
2187+
ensureRootIsScheduled(root, now());
2188+
};
2189+
thenable.then(onResolution, onResolution);
2190+
break outer;
21862191
}
21872192
case SuspendedOnImmediate: {
21882193
// If this fiber just suspended, it's possible the data is already
@@ -2192,17 +2197,25 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
21922197
break outer;
21932198
}
21942199
default: {
2200+
if (workInProgressSuspendedThenableState !== null) {
2201+
const thenableState = workInProgressSuspendedThenableState;
2202+
if (isThenableStateResolved(thenableState)) {
2203+
// The data resolved. Try rendering the component again.
2204+
workInProgressSuspendedReason = NotSuspended;
2205+
workInProgressThrownValue = null;
2206+
replaySuspendedUnitOfWork(
2207+
unitOfWork,
2208+
thrownValue,
2209+
thenableState,
2210+
);
2211+
break;
2212+
}
2213+
}
2214+
2215+
// Otherwise, unwind then continue with the normal work loop.
21952216
workInProgressSuspendedReason = NotSuspended;
21962217
workInProgressThrownValue = null;
2197-
const didResolve =
2198-
workInProgressSuspendedThenableState !== null &&
2199-
isThenableStateResolved(workInProgressSuspendedThenableState);
2200-
if (didResolve) {
2201-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2202-
} else {
2203-
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
2204-
}
2205-
// Continue with the normal work loop.
2218+
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
22062219
break;
22072220
}
22082221
}
@@ -2290,6 +2303,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
22902303
function replaySuspendedUnitOfWork(
22912304
unitOfWork: Fiber,
22922305
thrownValue: mixed,
2306+
thenableState: ThenableState,
22932307
): void {
22942308
// This is a fork of performUnitOfWork specifcally for replaying a fiber that
22952309
// just suspended.

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

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,24 +2165,29 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
21652165
break;
21662166
}
21672167
case SuspendedOnData: {
2168-
const didResolve =
2169-
workInProgressSuspendedThenableState !== null &&
2170-
isThenableStateResolved(workInProgressSuspendedThenableState);
2171-
if (didResolve) {
2172-
workInProgressSuspendedReason = NotSuspended;
2173-
workInProgressThrownValue = null;
2174-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2175-
} else {
2176-
// The work loop is suspended on data. We should wait for it to
2177-
// resolve before continuing to render.
2178-
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2179-
const onResolution = () => {
2180-
ensureRootIsScheduled(root, now());
2181-
};
2182-
thenable.then(onResolution, onResolution);
2183-
break outer;
2168+
if (workInProgressSuspendedThenableState !== null) {
2169+
const thenableState = workInProgressSuspendedThenableState;
2170+
if (isThenableStateResolved(thenableState)) {
2171+
// The data resolved. Try rendering the component again.
2172+
workInProgressSuspendedReason = NotSuspended;
2173+
workInProgressThrownValue = null;
2174+
replaySuspendedUnitOfWork(
2175+
unitOfWork,
2176+
thrownValue,
2177+
thenableState,
2178+
);
2179+
break;
2180+
}
21842181
}
2185-
break;
2182+
2183+
// The work loop is suspended on data. We should wait for it to
2184+
// resolve before continuing to render.
2185+
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2186+
const onResolution = () => {
2187+
ensureRootIsScheduled(root, now());
2188+
};
2189+
thenable.then(onResolution, onResolution);
2190+
break outer;
21862191
}
21872192
case SuspendedOnImmediate: {
21882193
// If this fiber just suspended, it's possible the data is already
@@ -2192,17 +2197,25 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
21922197
break outer;
21932198
}
21942199
default: {
2200+
if (workInProgressSuspendedThenableState !== null) {
2201+
const thenableState = workInProgressSuspendedThenableState;
2202+
if (isThenableStateResolved(thenableState)) {
2203+
// The data resolved. Try rendering the component again.
2204+
workInProgressSuspendedReason = NotSuspended;
2205+
workInProgressThrownValue = null;
2206+
replaySuspendedUnitOfWork(
2207+
unitOfWork,
2208+
thrownValue,
2209+
thenableState,
2210+
);
2211+
break;
2212+
}
2213+
}
2214+
2215+
// Otherwise, unwind then continue with the normal work loop.
21952216
workInProgressSuspendedReason = NotSuspended;
21962217
workInProgressThrownValue = null;
2197-
const didResolve =
2198-
workInProgressSuspendedThenableState !== null &&
2199-
isThenableStateResolved(workInProgressSuspendedThenableState);
2200-
if (didResolve) {
2201-
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
2202-
} else {
2203-
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
2204-
}
2205-
// Continue with the normal work loop.
2218+
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
22062219
break;
22072220
}
22082221
}
@@ -2290,6 +2303,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
22902303
function replaySuspendedUnitOfWork(
22912304
unitOfWork: Fiber,
22922305
thrownValue: mixed,
2306+
thenableState: ThenableState,
22932307
): void {
22942308
// This is a fork of performUnitOfWork specifcally for replaying a fiber that
22952309
// just suspended.

0 commit comments

Comments
 (0)