Skip to content

Commit d1a4249

Browse files
committed
[flags] Cleanup enableCache
1 parent 2e25ee3 commit d1a4249

22 files changed

+280
-457
lines changed

packages/react-dom/src/__tests__/ReactCompositeComponent-test.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ describe('ReactCompositeComponent', () => {
537537
});
538538

539539
it('should cleanup even if render() fatals', async () => {
540-
const dispatcherEnabled = __DEV__ || gate(flags => flags.enableCache);
541540
const ownerEnabled = __DEV__;
542541

543542
let stashedDispatcher;
@@ -551,7 +550,7 @@ describe('ReactCompositeComponent', () => {
551550
}
552551

553552
const instance = <BadComponent />;
554-
expect(ReactSharedInternals.A).toBe(dispatcherEnabled ? null : undefined);
553+
expect(ReactSharedInternals.A).toBe(null);
555554

556555
const root = ReactDOMClient.createRoot(document.createElement('div'));
557556
await expect(async () => {
@@ -560,15 +559,11 @@ describe('ReactCompositeComponent', () => {
560559
});
561560
}).rejects.toThrow();
562561

563-
expect(ReactSharedInternals.A).toBe(dispatcherEnabled ? null : undefined);
564-
if (dispatcherEnabled) {
565-
if (ownerEnabled) {
566-
expect(stashedDispatcher.getOwner()).toBe(null);
567-
} else {
568-
expect(stashedDispatcher.getOwner).toBe(undefined);
569-
}
562+
expect(ReactSharedInternals.A).toBe(null);
563+
if (ownerEnabled) {
564+
expect(stashedDispatcher.getOwner()).toBe(null);
570565
} else {
571-
expect(stashedDispatcher).toBe(undefined);
566+
expect(stashedDispatcher.getOwner).toBe(undefined);
572567
}
573568
});
574569

packages/react-markup/src/__tests__/ReactMarkupServer-test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ if (!__EXPERIMENTAL__) {
209209
);
210210
});
211211

212-
// @gate enableCache
213212
it('supports cache', async () => {
214213
let counter = 0;
215214
const getCount = React.cache(() => {

packages/react-reconciler/src/ReactFiberAsyncDispatcher.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,12 @@
1010
import type {AsyncDispatcher, Fiber} from './ReactInternalTypes';
1111
import type {Cache} from './ReactFiberCacheComponent';
1212

13-
import {enableCache} from 'shared/ReactFeatureFlags';
1413
import {readContext} from './ReactFiberNewContext';
1514
import {CacheContext} from './ReactFiberCacheComponent';
1615

1716
import {current as currentOwner} from './ReactCurrentFiber';
1817

1918
function getCacheForType<T>(resourceType: () => T): T {
20-
if (!enableCache) {
21-
throw new Error('Not implemented.');
22-
}
2319
const cache: Cache = readContext(CacheContext);
2420
let cacheForType: T | void = (cache.data.get(resourceType): any);
2521
if (cacheForType === undefined) {

packages/react-reconciler/src/ReactFiberBeginWork.js

Lines changed: 47 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ import {
100100
enableProfilerCommitHooks,
101101
enableProfilerTimer,
102102
enableScopeAPI,
103-
enableCache,
104103
enableLazyContextPropagation,
105104
enableSchedulingProfiler,
106105
enableTransitionTracing,
@@ -712,12 +711,10 @@ function updateOffscreenComponent(
712711
cachePool: null,
713712
};
714713
workInProgress.memoizedState = nextState;
715-
if (enableCache) {
716-
// push the cache pool even though we're going to bail out
717-
// because otherwise there'd be a context mismatch
718-
if (current !== null) {
719-
pushTransition(workInProgress, null, null);
720-
}
714+
// push the cache pool even though we're going to bail out
715+
// because otherwise there'd be a context mismatch
716+
if (current !== null) {
717+
pushTransition(workInProgress, null, null);
721718
}
722719
reuseHiddenContextOnStack(workInProgress);
723720
pushOffscreenSuspenseHandler(workInProgress);
@@ -751,7 +748,7 @@ function updateOffscreenComponent(
751748
cachePool: null,
752749
};
753750
workInProgress.memoizedState = nextState;
754-
if (enableCache && current !== null) {
751+
if (current !== null) {
755752
// If the render that spawned this one accessed the cache pool, resume
756753
// using the same cache. Unless the parent changed, since that means
757754
// there was a refresh.
@@ -774,12 +771,10 @@ function updateOffscreenComponent(
774771
if (prevState !== null) {
775772
// We're going from hidden -> visible.
776773
let prevCachePool = null;
777-
if (enableCache) {
778-
// If the render that spawned this one accessed the cache pool, resume
779-
// using the same cache. Unless the parent changed, since that means
780-
// there was a refresh.
781-
prevCachePool = prevState.cachePool;
782-
}
774+
// If the render that spawned this one accessed the cache pool, resume
775+
// using the same cache. Unless the parent changed, since that means
776+
// there was a refresh.
777+
prevCachePool = prevState.cachePool;
783778

784779
let transitions = null;
785780
if (enableTransitionTracing) {
@@ -804,13 +799,11 @@ function updateOffscreenComponent(
804799
// special to do. Need to push to the stack regardless, though, to avoid
805800
// a push/pop misalignment.
806801

807-
if (enableCache) {
808-
// If the render that spawned this one accessed the cache pool, resume
809-
// using the same cache. Unless the parent changed, since that means
810-
// there was a refresh.
811-
if (current !== null) {
812-
pushTransition(workInProgress, null, null);
813-
}
802+
// If the render that spawned this one accessed the cache pool, resume
803+
// using the same cache. Unless the parent changed, since that means
804+
// there was a refresh.
805+
if (current !== null) {
806+
pushTransition(workInProgress, null, null);
814807
}
815808

816809
// We're about to bail out, but we need to push this to the stack anyway
@@ -833,15 +826,13 @@ function deferHiddenOffscreenComponent(
833826
const nextState: OffscreenState = {
834827
baseLanes: nextBaseLanes,
835828
// Save the cache pool so we can resume later.
836-
cachePool: enableCache ? getOffscreenDeferredCache() : null,
829+
cachePool: getOffscreenDeferredCache(),
837830
};
838831
workInProgress.memoizedState = nextState;
839-
if (enableCache) {
840-
// push the cache pool even though we're going to bail out
841-
// because otherwise there'd be a context mismatch
842-
if (current !== null) {
843-
pushTransition(workInProgress, null, null);
844-
}
832+
// push the cache pool even though we're going to bail out
833+
// because otherwise there'd be a context mismatch
834+
if (current !== null) {
835+
pushTransition(workInProgress, null, null);
845836
}
846837

847838
// We're about to bail out, but we need to push this to the stack anyway
@@ -874,10 +865,6 @@ function updateCacheComponent(
874865
workInProgress: Fiber,
875866
renderLanes: Lanes,
876867
) {
877-
if (!enableCache) {
878-
return null;
879-
}
880-
881868
prepareToReadContext(workInProgress, renderLanes);
882869
const parentCache = readContext(CacheContext);
883870

@@ -1478,13 +1465,11 @@ function updateHostRoot(
14781465
pushRootMarkerInstance(workInProgress);
14791466
}
14801467

1481-
if (enableCache) {
1482-
const nextCache: Cache = nextState.cache;
1483-
pushCacheProvider(workInProgress, nextCache);
1484-
if (nextCache !== prevState.cache) {
1485-
// The root cache refreshed.
1486-
propagateContextChange(workInProgress, CacheContext, renderLanes);
1487-
}
1468+
const nextCache: Cache = nextState.cache;
1469+
pushCacheProvider(workInProgress, nextCache);
1470+
if (nextCache !== prevState.cache) {
1471+
// The root cache refreshed.
1472+
propagateContextChange(workInProgress, CacheContext, renderLanes);
14881473
}
14891474

14901475
// This would ideally go inside processUpdateQueue, but because it suspends,
@@ -1988,28 +1973,26 @@ function updateSuspenseOffscreenState(
19881973
renderLanes: Lanes,
19891974
): OffscreenState {
19901975
let cachePool: SpawnedCachePool | null = null;
1991-
if (enableCache) {
1992-
const prevCachePool: SpawnedCachePool | null = prevOffscreenState.cachePool;
1993-
if (prevCachePool !== null) {
1994-
const parentCache = isPrimaryRenderer
1995-
? CacheContext._currentValue
1996-
: CacheContext._currentValue2;
1997-
if (prevCachePool.parent !== parentCache) {
1998-
// Detected a refresh in the parent. This overrides any previously
1999-
// suspended cache.
2000-
cachePool = {
2001-
parent: parentCache,
2002-
pool: parentCache,
2003-
};
2004-
} else {
2005-
// We can reuse the cache from last time. The only thing that would have
2006-
// overridden it is a parent refresh, which we checked for above.
2007-
cachePool = prevCachePool;
2008-
}
1976+
const prevCachePool: SpawnedCachePool | null = prevOffscreenState.cachePool;
1977+
if (prevCachePool !== null) {
1978+
const parentCache = isPrimaryRenderer
1979+
? CacheContext._currentValue
1980+
: CacheContext._currentValue2;
1981+
if (prevCachePool.parent !== parentCache) {
1982+
// Detected a refresh in the parent. This overrides any previously
1983+
// suspended cache.
1984+
cachePool = {
1985+
parent: parentCache,
1986+
pool: parentCache,
1987+
};
20091988
} else {
2010-
// If there's no previous cache pool, grab the current one.
2011-
cachePool = getSuspendedCache();
1989+
// We can reuse the cache from last time. The only thing that would have
1990+
// overridden it is a parent refresh, which we checked for above.
1991+
cachePool = prevCachePool;
20121992
}
1993+
} else {
1994+
// If there's no previous cache pool, grab the current one.
1995+
cachePool = getSuspendedCache();
20131996
}
20141997
return {
20151998
baseLanes: mergeLanes(prevOffscreenState.baseLanes, renderLanes),
@@ -3610,10 +3593,8 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
36103593
pushRootMarkerInstance(workInProgress);
36113594
}
36123595

3613-
if (enableCache) {
3614-
const cache: Cache = current.memoizedState.cache;
3615-
pushCacheProvider(workInProgress, cache);
3616-
}
3596+
const cache: Cache = current.memoizedState.cache;
3597+
pushCacheProvider(workInProgress, cache);
36173598
resetHydrationState();
36183599
break;
36193600
case HostSingleton:
@@ -3797,10 +3778,8 @@ function attemptEarlyBailoutIfNoScheduledUpdate(
37973778
return updateOffscreenComponent(current, workInProgress, renderLanes);
37983779
}
37993780
case CacheComponent: {
3800-
if (enableCache) {
3801-
const cache: Cache = current.memoizedState.cache;
3802-
pushCacheProvider(workInProgress, cache);
3803-
}
3781+
const cache: Cache = current.memoizedState.cache;
3782+
pushCacheProvider(workInProgress, cache);
38043783
break;
38053784
}
38063785
case TracingMarkerComponent: {
@@ -4087,10 +4066,7 @@ function beginWork(
40874066
break;
40884067
}
40894068
case CacheComponent: {
4090-
if (enableCache) {
4091-
return updateCacheComponent(current, workInProgress, renderLanes);
4092-
}
4093-
break;
4069+
return updateCacheComponent(current, workInProgress, renderLanes);
40944070
}
40954071
case TracingMarkerComponent: {
40964072
if (enableTransitionTracing) {

packages/react-reconciler/src/ReactFiberCacheComponent.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,15 @@
1010
import type {ReactContext} from 'shared/ReactTypes';
1111
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
1212

13-
import {enableCache} from 'shared/ReactFeatureFlags';
1413
import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
1514

1615
import {pushProvider, popProvider} from './ReactFiberNewContext';
1716
import * as Scheduler from 'scheduler';
1817

1918
// In environments without AbortController (e.g. tests)
2019
// replace it with a lightweight shim that only has the features we use.
21-
const AbortControllerLocal: typeof AbortController = enableCache
22-
? typeof AbortController !== 'undefined'
20+
const AbortControllerLocal: typeof AbortController =
21+
typeof AbortController !== 'undefined'
2322
? AbortController
2423
: // $FlowFixMe[missing-this-annot]
2524
// $FlowFixMe[prop-missing]
@@ -36,9 +35,7 @@ const AbortControllerLocal: typeof AbortController = enableCache
3635
signal.aborted = true;
3736
listeners.forEach(listener => listener());
3837
};
39-
}
40-
: // $FlowFixMe[incompatible-type]
41-
null;
38+
};
4239

4340
export type Cache = {
4441
controller: AbortController,
@@ -63,20 +60,18 @@ const {
6360
unstable_NormalPriority: NormalPriority,
6461
} = Scheduler;
6562

66-
export const CacheContext: ReactContext<Cache> = enableCache
67-
? {
68-
$$typeof: REACT_CONTEXT_TYPE,
69-
// We don't use Consumer/Provider for Cache components. So we'll cheat.
70-
Consumer: (null: any),
71-
Provider: (null: any),
72-
// We'll initialize these at the root.
73-
_currentValue: (null: any),
74-
_currentValue2: (null: any),
75-
_threadCount: 0,
76-
}
77-
: (null: any);
63+
export const CacheContext: ReactContext<Cache> = {
64+
$$typeof: REACT_CONTEXT_TYPE,
65+
// We don't use Consumer/Provider for Cache components. So we'll cheat.
66+
Consumer: (null: any),
67+
Provider: (null: any),
68+
// We'll initialize these at the root.
69+
_currentValue: (null: any),
70+
_currentValue2: (null: any),
71+
_threadCount: 0,
72+
};
7873

79-
if (__DEV__ && enableCache) {
74+
if (__DEV__) {
8075
CacheContext._currentRenderer = null;
8176
CacheContext._currentRenderer2 = null;
8277
}
@@ -85,22 +80,14 @@ if (__DEV__ && enableCache) {
8580
// for retaining the cache once it is in use (retainCache), and releasing the cache
8681
// once it is no longer needed (releaseCache).
8782
export function createCache(): Cache {
88-
if (!enableCache) {
89-
return (null: any);
90-
}
91-
const cache: Cache = {
83+
return {
9284
controller: new AbortControllerLocal(),
9385
data: new Map(),
9486
refCount: 0,
9587
};
96-
97-
return cache;
9888
}
9989

10090
export function retainCache(cache: Cache) {
101-
if (!enableCache) {
102-
return;
103-
}
10491
if (__DEV__) {
10592
if (cache.controller.signal.aborted) {
10693
console.warn(
@@ -114,9 +101,6 @@ export function retainCache(cache: Cache) {
114101

115102
// Cleanup a cache instance, potentially freeing it if there are no more references
116103
export function releaseCache(cache: Cache) {
117-
if (!enableCache) {
118-
return;
119-
}
120104
cache.refCount--;
121105
if (__DEV__) {
122106
if (cache.refCount < 0) {
@@ -134,15 +118,9 @@ export function releaseCache(cache: Cache) {
134118
}
135119

136120
export function pushCacheProvider(workInProgress: Fiber, cache: Cache) {
137-
if (!enableCache) {
138-
return;
139-
}
140121
pushProvider(workInProgress, CacheContext, cache);
141122
}
142123

143124
export function popCacheProvider(workInProgress: Fiber, cache: Cache) {
144-
if (!enableCache) {
145-
return;
146-
}
147125
popProvider(CacheContext, workInProgress);
148126
}

0 commit comments

Comments
 (0)