Skip to content

Commit 7489d30

Browse files
committed
Reuse active cache
1 parent f140f36 commit 7489d30

File tree

6 files changed

+30
-45
lines changed

6 files changed

+30
-45
lines changed

packages/react-reconciler/src/ReactInternalTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ export interface AsyncCache {
457457
}
458458

459459
export type AsyncDispatcher = {
460-
getActiveCache: () => AsyncCache,
460+
getActiveCache: () => AsyncCache | null,
461461
// DEV-only (or !disableStringRefs)
462462
getOwner: () => null | Fiber | ReactComponentInfo | ComponentStackNode,
463463
};

packages/react-server/src/ReactFizzAsyncDispatcher.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {disableStringRefs} from 'shared/ReactFeatureFlags';
1717

1818
import {currentTaskInDEV} from './ReactFizzCurrentTask';
1919

20-
function getActiveCache(): AsyncCache {
20+
function getActiveCache(): AsyncCache | null {
2121
throw new Error('Not implemented.');
2222
}
2323

packages/react-server/src/ReactFlightServer.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import type {Chunk, BinaryChunk, Destination} from './ReactServerStreamConfig';
1111

1212
import type {Postpone} from 'react/src/ReactPostpone';
13+
import type {AsyncCache} from 'react-reconciler/src/ReactInternalTypes';
1314

1415
import type {TemporaryReferenceSet} from './ReactFlightServerTemporaryReferences';
1516

1617
import {
18+
disableStringRefs,
1719
enableBinaryFlight,
1820
enablePostpone,
1921
enableHalt,
@@ -450,6 +452,24 @@ function RequestInstance(
450452
onAllReady: () => void,
451453
onFatalError: (error: mixed) => void,
452454
) {
455+
const previousAsyncDispatcher = ReactSharedInternals.A;
456+
457+
if (previousAsyncDispatcher !== null) {
458+
const previousActiveCache = previousAsyncDispatcher.getActiveCache();
459+
if (previousActiveCache !== null) {
460+
ReactSharedInternals.A = ({
461+
getActiveCache(): AsyncCache | null {
462+
return previousActiveCache;
463+
},
464+
}: any);
465+
if (__DEV__ || !disableStringRefs) {
466+
ReactSharedInternals.A.getOwner = previousAsyncDispatcher.getOwner;
467+
}
468+
}
469+
} else {
470+
ReactSharedInternals.A = DefaultAsyncDispatcher;
471+
}
472+
453473
if (__DEV__) {
454474
// Unlike Fizz or Fiber, we don't reset this and just keep it on permanently.
455475
// This lets it act more like the AsyncDispatcher so that we can get the

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

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,54 +15,17 @@ import type {
1515
} from 'react-reconciler/src/ReactInternalTypes';
1616

1717
import {resolveRequest, getCache} from '../ReactFlightServer';
18-
import ReactSharedInternals from '../ReactSharedInternalsServer';
1918

2019
import {disableStringRefs} from 'shared/ReactFeatureFlags';
2120

2221
import {resolveOwner} from './ReactFlightCurrentOwner';
2322

24-
const previousAsyncDispatcher = ReactSharedInternals.A;
25-
26-
function resolveCache(): Map<Function, mixed> {
23+
function getActiveCache(): AsyncCache | null {
2724
const request = resolveRequest();
2825
if (request) {
2926
return getCache(request);
3027
}
31-
return new Map();
32-
}
33-
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;
53-
}
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-
};
64-
65-
return chainedCache;
28+
return null;
6629
}
6730

6831
export const DefaultAsyncDispatcher: AsyncDispatcher = ({

packages/react/src/ReactCacheImpl.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ export function cache<A: Iterable<mixed>, T>(fn: (...A) => T): (...A) => T {
6161
return fn.apply(null, arguments);
6262
}
6363
const activeCache = dispatcher.getActiveCache();
64-
let fnMap: WeakMap<any, CacheNode<T>> | void = (activeCache.get(
65-
createCacheRoot,
66-
): any);
64+
let fnMap: Map<any, CacheNode<T>> | void =
65+
activeCache !== null
66+
? (activeCache.get(createCacheRoot): any)
67+
: undefined;
6768
if (fnMap === undefined) {
6869
fnMap = createCacheRoot();
6970
// TODO: Warn if undefined?

packages/react/src/ReactHooks.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export function getCacheForType<T>(resourceType: () => T): T {
5454
return resourceType();
5555
}
5656
const activeCache = dispatcher.getActiveCache();
57-
let entry: T | void = (activeCache.get(resourceType): any);
57+
let entry: T | void =
58+
activeCache !== null ? (activeCache.get(resourceType): any) : undefined;
5859
if (entry === undefined) {
5960
entry = resourceType();
6061
// TODO: Warn if undefined?

0 commit comments

Comments
 (0)