Skip to content

Commit

Permalink
Pass ThenableState to replaySuspendedUnitOfWork
Browse files Browse the repository at this point in the history
Tiny refactor to refine the work loop variable so Flow knows it's not
null when we access it in replaySuspendedUnitOfWork.
  • Loading branch information
acdlite committed Nov 17, 2022
1 parent 4a2d86b commit 5eb78d0
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 52 deletions.
66 changes: 40 additions & 26 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2202,24 +2202,29 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
break;
}
case SuspendedOnData: {
const didResolve =
workInProgressSuspendedThenableState !== null &&
isThenableStateResolved(workInProgressSuspendedThenableState);
if (didResolve) {
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
} else {
// The work loop is suspended on data. We should wait for it to
// resolve before continuing to render.
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
const onResolution = () => {
ensureRootIsScheduled(root, now());
};
thenable.then(onResolution, onResolution);
break outer;
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(
unitOfWork,
thrownValue,
thenableState,
);
break;
}
}
break;

// The work loop is suspended on data. We should wait for it to
// resolve before continuing to render.
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
const onResolution = () => {
ensureRootIsScheduled(root, now());
};
thenable.then(onResolution, onResolution);
break outer;
}
case SuspendedOnImmediate: {
// If this fiber just suspended, it's possible the data is already
Expand All @@ -2237,17 +2242,25 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
break outer;
}
default: {
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(
unitOfWork,
thrownValue,
thenableState,
);
break;
}
}

// Otherwise, unwind then continue with the normal work loop.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
const didResolve =
workInProgressSuspendedThenableState !== null &&
isThenableStateResolved(workInProgressSuspendedThenableState);
if (didResolve) {
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
} else {
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
}
// Continue with the normal work loop.
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
break;
}
}
Expand Down Expand Up @@ -2335,6 +2348,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
function replaySuspendedUnitOfWork(
unitOfWork: Fiber,
thrownValue: mixed,
thenableState: ThenableState,
): void {
// This is a fork of performUnitOfWork specifcally for replaying a fiber that
// just suspended.
Expand Down
66 changes: 40 additions & 26 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -2202,24 +2202,29 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
break;
}
case SuspendedOnData: {
const didResolve =
workInProgressSuspendedThenableState !== null &&
isThenableStateResolved(workInProgressSuspendedThenableState);
if (didResolve) {
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
} else {
// The work loop is suspended on data. We should wait for it to
// resolve before continuing to render.
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
const onResolution = () => {
ensureRootIsScheduled(root, now());
};
thenable.then(onResolution, onResolution);
break outer;
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(
unitOfWork,
thrownValue,
thenableState,
);
break;
}
}
break;

// The work loop is suspended on data. We should wait for it to
// resolve before continuing to render.
const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
const onResolution = () => {
ensureRootIsScheduled(root, now());
};
thenable.then(onResolution, onResolution);
break outer;
}
case SuspendedOnImmediate: {
// If this fiber just suspended, it's possible the data is already
Expand All @@ -2237,17 +2242,25 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
break outer;
}
default: {
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(
unitOfWork,
thrownValue,
thenableState,
);
break;
}
}

// Otherwise, unwind then continue with the normal work loop.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
const didResolve =
workInProgressSuspendedThenableState !== null &&
isThenableStateResolved(workInProgressSuspendedThenableState);
if (didResolve) {
replaySuspendedUnitOfWork(unitOfWork, thrownValue);
} else {
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
}
// Continue with the normal work loop.
unwindSuspendedUnitOfWork(unitOfWork, thrownValue);
break;
}
}
Expand Down Expand Up @@ -2335,6 +2348,7 @@ function performUnitOfWork(unitOfWork: Fiber): void {
function replaySuspendedUnitOfWork(
unitOfWork: Fiber,
thrownValue: mixed,
thenableState: ThenableState,
): void {
// This is a fork of performUnitOfWork specifcally for replaying a fiber that
// just suspended.
Expand Down

0 comments on commit 5eb78d0

Please sign in to comment.