Skip to content

Commit

Permalink
Check thenable instead of thenableState
Browse files Browse the repository at this point in the history
Now that hook state is preserved while the work loop is suspended, we
don't need to track the thenable state in the work loop. We can track
it alongside the rest of the hook state.

Before deleting the thenable state variable from the work loop, I need
to remove the other places where it's referenced.

One of them is `isThenableStateResolved`. This grabs the last thenable
from the array and checks if it has resolved.

This was a pointless indirection anyway. The thenable is already stored
as `workInProgressThrownValue`. So we can check that directly.
  • Loading branch information
acdlite committed Nov 17, 2022
1 parent 33e3d28 commit 6b4c031
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
10 changes: 3 additions & 7 deletions packages/react-reconciler/src/ReactFiberThenable.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ export function getThenableStateAfterSuspending(): ThenableState | null {
return state;
}

export function isThenableStateResolved(thenables: ThenableState): boolean {
const lastThenable = thenables[thenables.length - 1];
if (lastThenable !== undefined) {
const status = lastThenable.status;
return status === 'fulfilled' || status === 'rejected';
}
return true;
export function isThenableResolved(thenable: Thenable<mixed>): boolean {
const status = thenable.status;
return status === 'fulfilled' || status === 'rejected';
}

function noop(): void {}
Expand Down
10 changes: 3 additions & 7 deletions packages/react-reconciler/src/ReactFiberThenable.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ export function getThenableStateAfterSuspending(): ThenableState | null {
return state;
}

export function isThenableStateResolved(thenables: ThenableState): boolean {
const lastThenable = thenables[thenables.length - 1];
if (lastThenable !== undefined) {
const status = lastThenable.status;
return status === 'fulfilled' || status === 'rejected';
}
return true;
export function isThenableResolved(thenable: Thenable<mixed>): boolean {
const status = thenable.status;
return status === 'fulfilled' || status === 'rejected';
}

function noop(): void {}
Expand Down
15 changes: 6 additions & 9 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ import {
SuspenseException,
getSuspendedThenable,
getThenableStateAfterSuspending,
isThenableStateResolved,
isThenableResolved,
} from './ReactFiberThenable.new';
import {schedulePostPaintCallback} from './ReactPostPaintCallback';
import {
Expand Down Expand Up @@ -2204,24 +2204,20 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
break;
}
case SuspendedOnData: {
const thenable: Thenable<mixed> = (thrownValue: any);
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
if (isThenableResolved(thenable)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(
unitOfWork,
thrownValue,
thenableState,
);
replaySuspendedUnitOfWork(unitOfWork, thenable, thenableState);
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());
};
Expand All @@ -2246,7 +2242,8 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
default: {
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
const thenable: Thenable<mixed> = (thrownValue: any);
if (isThenableResolved(thenable)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
Expand Down
15 changes: 6 additions & 9 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ import {
SuspenseException,
getSuspendedThenable,
getThenableStateAfterSuspending,
isThenableStateResolved,
isThenableResolved,
} from './ReactFiberThenable.old';
import {schedulePostPaintCallback} from './ReactPostPaintCallback';
import {
Expand Down Expand Up @@ -2204,24 +2204,20 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
break;
}
case SuspendedOnData: {
const thenable: Thenable<mixed> = (thrownValue: any);
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
if (isThenableResolved(thenable)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
replaySuspendedUnitOfWork(
unitOfWork,
thrownValue,
thenableState,
);
replaySuspendedUnitOfWork(unitOfWork, thenable, thenableState);
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());
};
Expand All @@ -2246,7 +2242,8 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
default: {
if (workInProgressSuspendedThenableState !== null) {
const thenableState = workInProgressSuspendedThenableState;
if (isThenableStateResolved(thenableState)) {
const thenable: Thenable<mixed> = (thrownValue: any);
if (isThenableResolved(thenable)) {
// The data resolved. Try rendering the component again.
workInProgressSuspendedReason = NotSuspended;
workInProgressThrownValue = null;
Expand Down

0 comments on commit 6b4c031

Please sign in to comment.