Skip to content

Commit 2318712

Browse files
committed
Remove getCacheForType from dispatcher
1 parent d9c489b commit 2318712

File tree

7 files changed

+74
-57
lines changed

7 files changed

+74
-57
lines changed

packages/react-reconciler/src/ReactFiberAsyncDispatcher.js

+6-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* @flow
88
*/
99

10-
import type {AsyncDispatcher, Fiber} from './ReactInternalTypes';
10+
import type {AsyncCache, AsyncDispatcher, Fiber} from './ReactInternalTypes';
1111
import type {Cache} from './ReactFiberCacheComponent';
1212

1313
import {enableCache} from 'shared/ReactFeatureFlags';
@@ -18,28 +18,18 @@ import {disableStringRefs} from 'shared/ReactFeatureFlags';
1818

1919
import {current as currentOwner} from './ReactCurrentFiber';
2020

21-
function getActiveCache(): Map<Function, mixed> {
22-
const cache: Cache = readContext(CacheContext);
23-
24-
return cache.data;
25-
}
26-
27-
function getCacheForType<T>(resourceType: () => T): T {
21+
function getActiveCache(): AsyncCache {
2822
if (!enableCache) {
2923
throw new Error('Not implemented.');
3024
}
31-
const cache = getActiveCache();
32-
let cacheForType: T | void = (cache.get(resourceType): any);
33-
if (cacheForType === undefined) {
34-
cacheForType = resourceType();
35-
cache.set(resourceType, cacheForType);
36-
}
37-
return cacheForType;
25+
26+
const cache: Cache = readContext(CacheContext);
27+
28+
return cache.data;
3829
}
3930

4031
export const DefaultAsyncDispatcher: AsyncDispatcher = ({
4132
getActiveCache,
42-
getCacheForType,
4333
}: any);
4434

4535
if (__DEV__ || !disableStringRefs) {

packages/react-reconciler/src/ReactFiberCacheComponent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import type {ReactContext} from 'shared/ReactTypes';
11-
import type {Fiber} from 'react-reconciler/src/ReactInternalTypes';
11+
import type {AsyncCache, Fiber} from 'react-reconciler/src/ReactInternalTypes';
1212

1313
import {enableCache} from 'shared/ReactFeatureFlags';
1414
import {REACT_CONTEXT_TYPE} from 'shared/ReactSymbols';
@@ -42,7 +42,7 @@ const AbortControllerLocal: typeof AbortController = enableCache
4242

4343
export type Cache = {
4444
controller: AbortController,
45-
data: Map<() => mixed, mixed>,
45+
data: AsyncCache,
4646
refCount: number,
4747
};
4848

packages/react-reconciler/src/ReactInternalTypes.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -451,9 +451,13 @@ export type Dispatcher = {
451451
) => [Awaited<S>, (P) => void, boolean],
452452
};
453453

454+
export interface AsyncCache {
455+
get(resourceType: Function): mixed;
456+
set(resourceType: Function, value: mixed): AsyncCache;
457+
}
458+
454459
export type AsyncDispatcher = {
455-
getActiveCache: () => Map<Function, mixed> | null,
456-
getCacheForType: <T>(resourceType: () => T) => T,
460+
getActiveCache: () => AsyncCache,
457461
// DEV-only (or !disableStringRefs)
458462
getOwner: () => null | Fiber | ReactComponentInfo | ComponentStackNode,
459463
};

packages/react-server/src/ReactFizzAsyncDispatcher.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@
77
* @flow
88
*/
99

10-
import type {AsyncDispatcher} from 'react-reconciler/src/ReactInternalTypes';
10+
import type {
11+
AsyncCache,
12+
AsyncDispatcher,
13+
} from 'react-reconciler/src/ReactInternalTypes';
1114
import type {ComponentStackNode} from './ReactFizzComponentStack';
1215

1316
import {disableStringRefs} from 'shared/ReactFeatureFlags';
1417

1518
import {currentTaskInDEV} from './ReactFizzCurrentTask';
1619

17-
function getCacheForType<T>(resourceType: () => T): T {
20+
function getActiveCache(): AsyncCache {
1821
throw new Error('Not implemented.');
1922
}
2023

2124
export const DefaultAsyncDispatcher: AsyncDispatcher = ({
22-
getCacheForType,
25+
getActiveCache,
2326
}: any);
2427

2528
if (__DEV__) {

packages/react-server/src/flight/ReactFlightAsyncDispatcher.js

+38-31
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
import type {ReactComponentInfo} from 'shared/ReactTypes';
1111

12-
import type {AsyncDispatcher} from 'react-reconciler/src/ReactInternalTypes';
12+
import type {
13+
AsyncCache,
14+
AsyncDispatcher,
15+
} from 'react-reconciler/src/ReactInternalTypes';
1316

1417
import {resolveRequest, getCache} from '../ReactFlightServer';
1518
import ReactSharedInternals from '../ReactSharedInternalsServer';
@@ -28,38 +31,42 @@ function resolveCache(): Map<Function, mixed> {
2831
return new Map();
2932
}
3033

31-
export const DefaultAsyncDispatcher: AsyncDispatcher = ({
32-
getActiveCache(): Map<Function, mixed> | null {
33-
const request = resolveRequest();
34-
if (request) {
35-
return getCache(request);
36-
}
37-
return null;
38-
},
39-
getCacheForType<T>(resourceType: () => T): T {
40-
const outerCache: Map<Function, mixed> | null =
41-
previousAsyncDispatcher !== null
42-
? previousAsyncDispatcher.getActiveCache()
43-
: null;
44-
if (outerCache !== null) {
45-
const entry: T | void = (outerCache.get(resourceType): any);
46-
if (entry !== undefined) {
47-
return entry;
34+
function getActiveCache(): AsyncCache {
35+
const outerCache: AsyncCache | null =
36+
previousAsyncDispatcher !== null
37+
? previousAsyncDispatcher.getActiveCache()
38+
: null;
39+
40+
const innerCache = resolveCache();
41+
42+
if (outerCache === null) {
43+
return innerCache;
44+
}
45+
46+
// If both caches are active, reads will prefer the outer cache
47+
// Writes will go into both caches.
48+
const chainedCache: AsyncCache = {
49+
get(resourceType: Function) {
50+
const outerEntry = outerCache.get(resourceType);
51+
if (outerEntry !== undefined) {
52+
return outerEntry;
4853
}
49-
}
54+
return innerCache.get(resourceType);
55+
},
56+
set(resourceType: Function, value: mixed) {
57+
if (outerCache !== null) {
58+
outerCache.set(resourceType, value);
59+
}
60+
innerCache.set(resourceType, value);
61+
return chainedCache;
62+
},
63+
};
5064

51-
const cache = resolveCache();
52-
let entry: T | void = (cache.get(resourceType): any);
53-
if (entry === undefined) {
54-
entry = resourceType();
55-
// TODO: Warn if undefined?
56-
cache.set(resourceType, entry);
57-
}
58-
if (outerCache !== null) {
59-
outerCache.set(resourceType, entry);
60-
}
61-
return entry;
62-
},
65+
return chainedCache;
66+
}
67+
68+
export const DefaultAsyncDispatcher: AsyncDispatcher = ({
69+
getActiveCache,
6370
}: any);
6471

6572
if (__DEV__) {

packages/react/src/ReactCacheImpl.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ export function cache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
6060
// $FlowFixMe[incompatible-call]: We don't want to use rest arguments since we transpile the code.
6161
return fn.apply(null, arguments);
6262
}
63-
const fnMap: WeakMap<any, CacheNode<T>> = dispatcher.getCacheForType(
63+
const activeCache = dispatcher.getActiveCache();
64+
let fnMap: WeakMap<any, CacheNode<T>> | void = (activeCache.get(
6465
createCacheRoot,
65-
);
66+
): any);
67+
if (fnMap === undefined) {
68+
fnMap = createCacheRoot();
69+
// TODO: Warn if undefined?
70+
activeCache.set(createCacheRoot, fnMap);
71+
}
6672
const fnNode = fnMap.get(fn);
6773
let cacheNode: CacheNode<T>;
6874
if (fnNode === undefined) {

packages/react/src/ReactHooks.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ export function getCacheForType<T>(resourceType: () => T): T {
5353
// If there is no dispatcher, then we treat this as not being cached.
5454
return resourceType();
5555
}
56-
return dispatcher.getCacheForType(resourceType);
56+
const activeCache = dispatcher.getActiveCache();
57+
let entry: T | void = (activeCache.get(resourceType): any);
58+
if (entry === undefined) {
59+
entry = resourceType();
60+
// TODO: Warn if undefined?
61+
activeCache.set(resourceType, entry);
62+
}
63+
return entry;
5764
}
5865

5966
export function useContext<T>(Context: ReactContext<T>): T {

0 commit comments

Comments
 (0)