Skip to content

Commit a8c16a0

Browse files
authored
Split Cache into its own Dispatcher (#25474)
* Missing Hooks * Remove www forks. These can use __SECRET... instead. * Move cache to separate dispatcher These will be available in more contexts than just render.
1 parent 2cf4352 commit a8c16a0

24 files changed

+293
-229
lines changed

packages/react-debug-tools/src/ReactDebugHooks.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ function nextHook(): null | Hook {
101101
return hook;
102102
}
103103

104-
function getCacheForType<T>(resourceType: () => T): T {
105-
throw new Error('Not implemented.');
106-
}
107-
108104
function readContext<T>(context: ReactContext<T>): T {
109105
// For now we don't expose readContext usage in the hooks debugging info.
110106
return context._currentValue;
@@ -331,7 +327,6 @@ function useId(): string {
331327
}
332328

333329
const Dispatcher: DispatcherType = {
334-
getCacheForType,
335330
readContext,
336331
useCacheRefresh,
337332
useCallback,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import type {CacheDispatcher} from './ReactInternalTypes';
11+
import type {Cache} from './ReactFiberCacheComponent.new';
12+
13+
import {enableCache} from 'shared/ReactFeatureFlags';
14+
import {readContext} from './ReactFiberNewContext.new';
15+
import {CacheContext} from './ReactFiberCacheComponent.new';
16+
17+
function getCacheSignal(): AbortSignal {
18+
if (!enableCache) {
19+
throw new Error('Not implemented.');
20+
}
21+
const cache: Cache = readContext(CacheContext);
22+
return cache.controller.signal;
23+
}
24+
25+
function getCacheForType<T>(resourceType: () => T): T {
26+
if (!enableCache) {
27+
throw new Error('Not implemented.');
28+
}
29+
const cache: Cache = readContext(CacheContext);
30+
let cacheForType: T | void = (cache.data.get(resourceType): any);
31+
if (cacheForType === undefined) {
32+
cacheForType = resourceType();
33+
cache.data.set(resourceType, cacheForType);
34+
}
35+
return cacheForType;
36+
}
37+
38+
export const DefaultCacheDispatcher: CacheDispatcher = {
39+
getCacheSignal,
40+
getCacheForType,
41+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import type {CacheDispatcher} from './ReactInternalTypes';
11+
import type {Cache} from './ReactFiberCacheComponent.old';
12+
13+
import {enableCache} from 'shared/ReactFeatureFlags';
14+
import {readContext} from './ReactFiberNewContext.old';
15+
import {CacheContext} from './ReactFiberCacheComponent.old';
16+
17+
function getCacheSignal(): AbortSignal {
18+
if (!enableCache) {
19+
throw new Error('Not implemented.');
20+
}
21+
const cache: Cache = readContext(CacheContext);
22+
return cache.controller.signal;
23+
}
24+
25+
function getCacheForType<T>(resourceType: () => T): T {
26+
if (!enableCache) {
27+
throw new Error('Not implemented.');
28+
}
29+
const cache: Cache = readContext(CacheContext);
30+
let cacheForType: T | void = (cache.data.get(resourceType): any);
31+
if (cacheForType === undefined) {
32+
cacheForType = resourceType();
33+
cache.data.set(resourceType, cacheForType);
34+
}
35+
return cacheForType;
36+
}
37+
38+
export const DefaultCacheDispatcher: CacheDispatcher = {
39+
getCacheSignal,
40+
getCacheForType,
41+
};

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

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import type {
2626
import type {Lanes, Lane} from './ReactFiberLane.new';
2727
import type {HookFlags} from './ReactHookEffectTags';
2828
import type {FiberRoot} from './ReactInternalTypes';
29-
import type {Cache} from './ReactFiberCacheComponent.new';
3029
import type {Flags} from './ReactFiberFlags';
3130

3231
import ReactSharedInternals from 'shared/ReactSharedInternals';
@@ -122,7 +121,7 @@ import {
122121
} from './ReactMutableSource.new';
123122
import {logStateUpdateScheduled} from './DebugTracing';
124123
import {markStateUpdateScheduled} from './ReactFiberDevToolsHook.new';
125-
import {createCache, CacheContext} from './ReactFiberCacheComponent.new';
124+
import {createCache} from './ReactFiberCacheComponent.new';
126125
import {
127126
createUpdate as createLegacyQueueUpdate,
128127
enqueueUpdate as enqueueLegacyQueueUpdate,
@@ -2600,27 +2599,6 @@ function markUpdateInDevTools<A>(fiber, lane, action: A) {
26002599
}
26012600
}
26022601

2603-
function getCacheSignal(): AbortSignal {
2604-
if (!enableCache) {
2605-
throw new Error('Not implemented.');
2606-
}
2607-
const cache: Cache = readContext(CacheContext);
2608-
return cache.controller.signal;
2609-
}
2610-
2611-
function getCacheForType<T>(resourceType: () => T): T {
2612-
if (!enableCache) {
2613-
throw new Error('Not implemented.');
2614-
}
2615-
const cache: Cache = readContext(CacheContext);
2616-
let cacheForType: T | void = (cache.data.get(resourceType): any);
2617-
if (cacheForType === undefined) {
2618-
cacheForType = resourceType();
2619-
cache.data.set(resourceType, cacheForType);
2620-
}
2621-
return cacheForType;
2622-
}
2623-
26242602
export const ContextOnlyDispatcher: Dispatcher = {
26252603
readContext,
26262604

@@ -2644,8 +2622,6 @@ export const ContextOnlyDispatcher: Dispatcher = {
26442622
unstable_isNewReconciler: enableNewReconciler,
26452623
};
26462624
if (enableCache) {
2647-
(ContextOnlyDispatcher: Dispatcher).getCacheSignal = getCacheSignal;
2648-
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
26492625
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
26502626
}
26512627
if (enableUseHook) {
@@ -2681,8 +2657,6 @@ const HooksDispatcherOnMount: Dispatcher = {
26812657
unstable_isNewReconciler: enableNewReconciler,
26822658
};
26832659
if (enableCache) {
2684-
(HooksDispatcherOnMount: Dispatcher).getCacheSignal = getCacheSignal;
2685-
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
26862660
// $FlowFixMe[escaped-generic] discovered when updating Flow
26872661
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
26882662
}
@@ -2718,8 +2692,6 @@ const HooksDispatcherOnUpdate: Dispatcher = {
27182692
unstable_isNewReconciler: enableNewReconciler,
27192693
};
27202694
if (enableCache) {
2721-
(HooksDispatcherOnUpdate: Dispatcher).getCacheSignal = getCacheSignal;
2722-
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
27232695
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
27242696
}
27252697
if (enableUseMemoCacheHook) {
@@ -2755,8 +2727,6 @@ const HooksDispatcherOnRerender: Dispatcher = {
27552727
unstable_isNewReconciler: enableNewReconciler,
27562728
};
27572729
if (enableCache) {
2758-
(HooksDispatcherOnRerender: Dispatcher).getCacheSignal = getCacheSignal;
2759-
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
27602730
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
27612731
}
27622732
if (enableUseHook) {
@@ -2935,8 +2905,6 @@ if (__DEV__) {
29352905
unstable_isNewReconciler: enableNewReconciler,
29362906
};
29372907
if (enableCache) {
2938-
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
2939-
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
29402908
(HooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
29412909
currentHookNameInDev = 'useCacheRefresh';
29422910
mountHookTypesDev();
@@ -3094,8 +3062,6 @@ if (__DEV__) {
30943062
unstable_isNewReconciler: enableNewReconciler,
30953063
};
30963064
if (enableCache) {
3097-
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3098-
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheForType = getCacheForType;
30993065
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
31003066
currentHookNameInDev = 'useCacheRefresh';
31013067
updateHookTypesDev();
@@ -3253,8 +3219,6 @@ if (__DEV__) {
32533219
unstable_isNewReconciler: enableNewReconciler,
32543220
};
32553221
if (enableCache) {
3256-
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3257-
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
32583222
(HooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
32593223
currentHookNameInDev = 'useCacheRefresh';
32603224
updateHookTypesDev();
@@ -3413,8 +3377,6 @@ if (__DEV__) {
34133377
unstable_isNewReconciler: enableNewReconciler,
34143378
};
34153379
if (enableCache) {
3416-
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3417-
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
34183380
(HooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
34193381
currentHookNameInDev = 'useCacheRefresh';
34203382
updateHookTypesDev();
@@ -3589,8 +3551,6 @@ if (__DEV__) {
35893551
unstable_isNewReconciler: enableNewReconciler,
35903552
};
35913553
if (enableCache) {
3592-
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3593-
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
35943554
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
35953555
currentHookNameInDev = 'useCacheRefresh';
35963556
mountHookTypesDev();
@@ -3776,8 +3736,6 @@ if (__DEV__) {
37763736
unstable_isNewReconciler: enableNewReconciler,
37773737
};
37783738
if (enableCache) {
3779-
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3780-
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
37813739
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
37823740
currentHookNameInDev = 'useCacheRefresh';
37833741
updateHookTypesDev();
@@ -3964,8 +3922,6 @@ if (__DEV__) {
39643922
unstable_isNewReconciler: enableNewReconciler,
39653923
};
39663924
if (enableCache) {
3967-
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3968-
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
39693925
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
39703926
currentHookNameInDev = 'useCacheRefresh';
39713927
updateHookTypesDev();

packages/react-reconciler/src/ReactFiberHooks.old.js

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import type {
2626
import type {Lanes, Lane} from './ReactFiberLane.old';
2727
import type {HookFlags} from './ReactHookEffectTags';
2828
import type {FiberRoot} from './ReactInternalTypes';
29-
import type {Cache} from './ReactFiberCacheComponent.old';
3029
import type {Flags} from './ReactFiberFlags';
3130

3231
import ReactSharedInternals from 'shared/ReactSharedInternals';
@@ -122,7 +121,7 @@ import {
122121
} from './ReactMutableSource.old';
123122
import {logStateUpdateScheduled} from './DebugTracing';
124123
import {markStateUpdateScheduled} from './ReactFiberDevToolsHook.old';
125-
import {createCache, CacheContext} from './ReactFiberCacheComponent.old';
124+
import {createCache} from './ReactFiberCacheComponent.old';
126125
import {
127126
createUpdate as createLegacyQueueUpdate,
128127
enqueueUpdate as enqueueLegacyQueueUpdate,
@@ -2600,27 +2599,6 @@ function markUpdateInDevTools<A>(fiber, lane, action: A) {
26002599
}
26012600
}
26022601

2603-
function getCacheSignal(): AbortSignal {
2604-
if (!enableCache) {
2605-
throw new Error('Not implemented.');
2606-
}
2607-
const cache: Cache = readContext(CacheContext);
2608-
return cache.controller.signal;
2609-
}
2610-
2611-
function getCacheForType<T>(resourceType: () => T): T {
2612-
if (!enableCache) {
2613-
throw new Error('Not implemented.');
2614-
}
2615-
const cache: Cache = readContext(CacheContext);
2616-
let cacheForType: T | void = (cache.data.get(resourceType): any);
2617-
if (cacheForType === undefined) {
2618-
cacheForType = resourceType();
2619-
cache.data.set(resourceType, cacheForType);
2620-
}
2621-
return cacheForType;
2622-
}
2623-
26242602
export const ContextOnlyDispatcher: Dispatcher = {
26252603
readContext,
26262604

@@ -2644,8 +2622,6 @@ export const ContextOnlyDispatcher: Dispatcher = {
26442622
unstable_isNewReconciler: enableNewReconciler,
26452623
};
26462624
if (enableCache) {
2647-
(ContextOnlyDispatcher: Dispatcher).getCacheSignal = getCacheSignal;
2648-
(ContextOnlyDispatcher: Dispatcher).getCacheForType = getCacheForType;
26492625
(ContextOnlyDispatcher: Dispatcher).useCacheRefresh = throwInvalidHookError;
26502626
}
26512627
if (enableUseHook) {
@@ -2681,8 +2657,6 @@ const HooksDispatcherOnMount: Dispatcher = {
26812657
unstable_isNewReconciler: enableNewReconciler,
26822658
};
26832659
if (enableCache) {
2684-
(HooksDispatcherOnMount: Dispatcher).getCacheSignal = getCacheSignal;
2685-
(HooksDispatcherOnMount: Dispatcher).getCacheForType = getCacheForType;
26862660
// $FlowFixMe[escaped-generic] discovered when updating Flow
26872661
(HooksDispatcherOnMount: Dispatcher).useCacheRefresh = mountRefresh;
26882662
}
@@ -2718,8 +2692,6 @@ const HooksDispatcherOnUpdate: Dispatcher = {
27182692
unstable_isNewReconciler: enableNewReconciler,
27192693
};
27202694
if (enableCache) {
2721-
(HooksDispatcherOnUpdate: Dispatcher).getCacheSignal = getCacheSignal;
2722-
(HooksDispatcherOnUpdate: Dispatcher).getCacheForType = getCacheForType;
27232695
(HooksDispatcherOnUpdate: Dispatcher).useCacheRefresh = updateRefresh;
27242696
}
27252697
if (enableUseMemoCacheHook) {
@@ -2755,8 +2727,6 @@ const HooksDispatcherOnRerender: Dispatcher = {
27552727
unstable_isNewReconciler: enableNewReconciler,
27562728
};
27572729
if (enableCache) {
2758-
(HooksDispatcherOnRerender: Dispatcher).getCacheSignal = getCacheSignal;
2759-
(HooksDispatcherOnRerender: Dispatcher).getCacheForType = getCacheForType;
27602730
(HooksDispatcherOnRerender: Dispatcher).useCacheRefresh = updateRefresh;
27612731
}
27622732
if (enableUseHook) {
@@ -2935,8 +2905,6 @@ if (__DEV__) {
29352905
unstable_isNewReconciler: enableNewReconciler,
29362906
};
29372907
if (enableCache) {
2938-
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
2939-
(HooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
29402908
(HooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
29412909
currentHookNameInDev = 'useCacheRefresh';
29422910
mountHookTypesDev();
@@ -3094,8 +3062,6 @@ if (__DEV__) {
30943062
unstable_isNewReconciler: enableNewReconciler,
30953063
};
30963064
if (enableCache) {
3097-
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3098-
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).getCacheForType = getCacheForType;
30993065
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
31003066
currentHookNameInDev = 'useCacheRefresh';
31013067
updateHookTypesDev();
@@ -3253,8 +3219,6 @@ if (__DEV__) {
32533219
unstable_isNewReconciler: enableNewReconciler,
32543220
};
32553221
if (enableCache) {
3256-
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3257-
(HooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
32583222
(HooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
32593223
currentHookNameInDev = 'useCacheRefresh';
32603224
updateHookTypesDev();
@@ -3413,8 +3377,6 @@ if (__DEV__) {
34133377
unstable_isNewReconciler: enableNewReconciler,
34143378
};
34153379
if (enableCache) {
3416-
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3417-
(HooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
34183380
(HooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
34193381
currentHookNameInDev = 'useCacheRefresh';
34203382
updateHookTypesDev();
@@ -3589,8 +3551,6 @@ if (__DEV__) {
35893551
unstable_isNewReconciler: enableNewReconciler,
35903552
};
35913553
if (enableCache) {
3592-
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3593-
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).getCacheForType = getCacheForType;
35943554
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
35953555
currentHookNameInDev = 'useCacheRefresh';
35963556
mountHookTypesDev();
@@ -3776,8 +3736,6 @@ if (__DEV__) {
37763736
unstable_isNewReconciler: enableNewReconciler,
37773737
};
37783738
if (enableCache) {
3779-
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3780-
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).getCacheForType = getCacheForType;
37813739
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
37823740
currentHookNameInDev = 'useCacheRefresh';
37833741
updateHookTypesDev();
@@ -3964,8 +3922,6 @@ if (__DEV__) {
39643922
unstable_isNewReconciler: enableNewReconciler,
39653923
};
39663924
if (enableCache) {
3967-
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheSignal = getCacheSignal;
3968-
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).getCacheForType = getCacheForType;
39693925
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useCacheRefresh = function useCacheRefresh() {
39703926
currentHookNameInDev = 'useCacheRefresh';
39713927
updateHookTypesDev();

0 commit comments

Comments
 (0)