Check thenable instead of thenableState
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.
Andrew Clark committed
Nov 3, 2022 at 17:23 UTC
6b4c0314e82e3e56a8af84f3af3c0822a950ad1f
4 files changed
+18
-32
packages/react-reconciler/src/ReactFiberThenable.new.js
+3
-7
@@ -54,13 +54,9 @@ export function getThenableStateAfterSuspending(): ThenableState | null {
54
return state;
55
}
56
57
-export function isThenableStateResolved(thenables: ThenableState): boolean {
58
- const lastThenable = thenables[thenables.length - 1];
59
- if (lastThenable !== undefined) {
60
- const status = lastThenable.status;
61
- return status === 'fulfilled' || status === 'rejected';
62
- }
63
- return true;
57
+export function isThenableResolved(thenable: Thenable<mixed>): boolean {
58
+ const status = thenable.status;
59
+ return status === 'fulfilled' || status === 'rejected';
60
}
61
62
function noop(): void {}
packages/react-reconciler/src/ReactFiberThenable.old.js
+3
-7
@@ -54,13 +54,9 @@ export function getThenableStateAfterSuspending(): ThenableState | null {
54
return state;
55
}
56
57
-export function isThenableStateResolved(thenables: ThenableState): boolean {
58
- const lastThenable = thenables[thenables.length - 1];
59
- if (lastThenable !== undefined) {
60
- const status = lastThenable.status;
61
- return status === 'fulfilled' || status === 'rejected';
62
- }
63
- return true;
57
+export function isThenableResolved(thenable: Thenable<mixed>): boolean {
58
+ const status = thenable.status;
59
+ return status === 'fulfilled' || status === 'rejected';
60
}
61
62
function noop(): void {}
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+6
-9
@@ -276,7 +276,7 @@ import {
276
SuspenseException,
277
getSuspendedThenable,
278
getThenableStateAfterSuspending,
279
- isThenableStateResolved,
279
+ isThenableResolved,
280
} from './ReactFiberThenable.new';
281
import {schedulePostPaintCallback} from './ReactPostPaintCallback';
282
import {
@@ -2204,24 +2204,20 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
2204
break;
2205
}
2206
case SuspendedOnData: {
2207
+ const thenable: Thenable<mixed> = (thrownValue: any);
2208
if (workInProgressSuspendedThenableState !== null) {
2209
const thenableState = workInProgressSuspendedThenableState;
2209
- if (isThenableStateResolved(thenableState)) {
2210
+ if (isThenableResolved(thenable)) {
2211
// The data resolved. Try rendering the component again.
2212
workInProgressSuspendedReason = NotSuspended;
2213
workInProgressThrownValue = null;
2213
- replaySuspendedUnitOfWork(
2214
- unitOfWork,
2215
- thrownValue,
2216
- thenableState,
2217
- );
2214
+ replaySuspendedUnitOfWork(unitOfWork, thenable, thenableState);
2215
break;
2216
}
2217
}
2218
2219
// The work loop is suspended on data. We should wait for it to
2220
// resolve before continuing to render.
2224
- const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2221
const onResolution = () => {
2222
ensureRootIsScheduled(root, now());
2223
};
@@ -2246,7 +2242,8 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
2242
default: {
2243
if (workInProgressSuspendedThenableState !== null) {
2244
const thenableState = workInProgressSuspendedThenableState;
2249
- if (isThenableStateResolved(thenableState)) {
2245
+ const thenable: Thenable<mixed> = (thrownValue: any);
2246
+ if (isThenableResolved(thenable)) {
2247
// The data resolved. Try rendering the component again.
2248
workInProgressSuspendedReason = NotSuspended;
2249
workInProgressThrownValue = null;
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+6
-9
@@ -276,7 +276,7 @@ import {
276
SuspenseException,
277
getSuspendedThenable,
278
getThenableStateAfterSuspending,
279
- isThenableStateResolved,
279
+ isThenableResolved,
280
} from './ReactFiberThenable.old';
281
import {schedulePostPaintCallback} from './ReactPostPaintCallback';
282
import {
@@ -2204,24 +2204,20 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
2204
break;
2205
}
2206
case SuspendedOnData: {
2207
+ const thenable: Thenable<mixed> = (thrownValue: any);
2208
if (workInProgressSuspendedThenableState !== null) {
2209
const thenableState = workInProgressSuspendedThenableState;
2209
- if (isThenableStateResolved(thenableState)) {
2210
+ if (isThenableResolved(thenable)) {
2211
// The data resolved. Try rendering the component again.
2212
workInProgressSuspendedReason = NotSuspended;
2213
workInProgressThrownValue = null;
2213
- replaySuspendedUnitOfWork(
2214
- unitOfWork,
2215
- thrownValue,
2216
- thenableState,
2217
- );
2214
+ replaySuspendedUnitOfWork(unitOfWork, thenable, thenableState);
2215
break;
2216
}
2217
}
2218
2219
// The work loop is suspended on data. We should wait for it to
2220
// resolve before continuing to render.
2224
- const thenable: Thenable<mixed> = (workInProgressThrownValue: any);
2221
const onResolution = () => {
2222
ensureRootIsScheduled(root, now());
2223
};
@@ -2246,7 +2242,8 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) {
2242
default: {
2243
if (workInProgressSuspendedThenableState !== null) {
2244
const thenableState = workInProgressSuspendedThenableState;
2249
- if (isThenableStateResolved(thenableState)) {
2245
+ const thenable: Thenable<mixed> = (thrownValue: any);
2246
+ if (isThenableResolved(thenable)) {
2247
// The data resolved. Try rendering the component again.
2248
workInProgressSuspendedReason = NotSuspended;
2249
workInProgressThrownValue = null;