Skip to content

Commit 5a6239a

Browse files
committed
remove dependency on updateQueue for React Cache
1 parent 5318971 commit 5a6239a

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -662,10 +662,13 @@ function updateOffscreenComponent(
662662
nextBaseLanes = mergeLanes(prevBaseLanes, renderLanes);
663663
if (enableCache) {
664664
// Save the cache pool so we can resume later.
665+
const prevCachePool = prevState.cachePool;
666+
if (prevCachePool !== null) {
667+
// push the cache pool even though we're going to bail out
668+
// because otherwise there'd be a context mismatch
669+
restoreSpawnedCachePool(workInProgress, prevCachePool);
670+
}
665671
spawnedCachePool = getOffscreenDeferredCachePool();
666-
// We don't need to push to the cache pool because we're about to
667-
// bail out. There won't be a context mismatch because we only pop
668-
// the cache pool if `updateQueue` is non-null.
669672
}
670673
} else {
671674
nextBaseLanes = renderLanes;
@@ -757,12 +760,6 @@ function updateOffscreenComponent(
757760
pushRenderLanes(workInProgress, subtreeRenderLanes);
758761
}
759762

760-
if (enableCache) {
761-
// If we have a cache pool from a previous render attempt, then this will be
762-
// non-null. We use this to infer whether to push/pop the cache context.
763-
workInProgress.updateQueue = spawnedCachePool;
764-
}
765-
766763
if (enablePersistentOffscreenHostContainer && supportsPersistence) {
767764
// In persistent mode, the offscreen children are wrapped in a host node.
768765
// TODO: Optimize this to use the OffscreenComponent fiber instead of

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type {
2828
} from './ReactFiberSuspenseComponent.new';
2929
import type {SuspenseContext} from './ReactFiberSuspenseContext.new';
3030
import type {OffscreenState} from './ReactFiberOffscreenComponent';
31-
import type {Cache, SpawnedCachePool} from './ReactFiberCacheComponent.new';
31+
import type {Cache} from './ReactFiberCacheComponent.new';
3232
import {
3333
enableClientRenderFallbackOnHydrationMismatch,
3434
enableSuspenseAvoidThisFallback,
@@ -1543,8 +1543,14 @@ function completeWork(
15431543
// Run passive effects to retain/release the cache.
15441544
workInProgress.flags |= Passive;
15451545
}
1546-
const spawnedCachePool: SpawnedCachePool | null = (workInProgress.updateQueue: any);
1547-
if (spawnedCachePool !== null) {
1546+
let prevState: OffscreenState | null = null;
1547+
if (
1548+
workInProgress.alternate !== null &&
1549+
workInProgress.alternate.memoizedState !== null
1550+
) {
1551+
prevState = workInProgress.alternate.memoizedState;
1552+
}
1553+
if (prevState !== null && prevState.cachePool !== null) {
15481554
popCachePool(workInProgress);
15491555
}
15501556
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import type {ReactContext} from 'shared/ReactTypes';
1111
import type {Fiber, FiberRoot} from './ReactInternalTypes';
1212
import type {Lanes} from './ReactFiberLane.new';
1313
import type {SuspenseState} from './ReactFiberSuspenseComponent.new';
14-
import type {Cache, SpawnedCachePool} from './ReactFiberCacheComponent.new';
14+
import type {Cache} from './ReactFiberCacheComponent.new';
15+
import type {OffscreenState} from './ReactFiberOffscreenComponent';
1516

1617
import {resetWorkInProgressVersions as resetMutableSourceWorkInProgressVersions} from './ReactMutableSource.new';
1718
import {
@@ -151,9 +152,15 @@ function unwindWork(workInProgress: Fiber, renderLanes: Lanes) {
151152
case OffscreenComponent:
152153
case LegacyHiddenComponent:
153154
popRenderLanes(workInProgress);
155+
let prevState: OffscreenState | null = null;
154156
if (enableCache) {
155-
const spawnedCachePool: SpawnedCachePool | null = (workInProgress.updateQueue: any);
156-
if (spawnedCachePool !== null) {
157+
if (
158+
workInProgress.alternate !== null &&
159+
workInProgress.alternate.memoizedState !== null
160+
) {
161+
prevState = workInProgress.alternate.memoizedState;
162+
}
163+
if (prevState !== null && prevState.cachePool !== null) {
157164
popCachePool(workInProgress);
158165
}
159166
}
@@ -217,8 +224,14 @@ function unwindInterruptedWork(interruptedWork: Fiber, renderLanes: Lanes) {
217224
case LegacyHiddenComponent:
218225
popRenderLanes(interruptedWork);
219226
if (enableCache) {
220-
const spawnedCachePool: SpawnedCachePool | null = (interruptedWork.updateQueue: any);
221-
if (spawnedCachePool !== null) {
227+
let prevState: OffscreenState | null = null;
228+
if (
229+
interruptedWork.alternate !== null &&
230+
interruptedWork.alternate.memoizedState !== null
231+
) {
232+
prevState = interruptedWork.alternate.memoizedState;
233+
}
234+
if (prevState !== null && prevState.cachePool !== null) {
222235
popCachePool(interruptedWork);
223236
}
224237
}

0 commit comments

Comments
 (0)