Skip to content

Commit 0b38251

Browse files
committed
Reuse active cache
1 parent f140f36 commit 0b38251

File tree

7 files changed

+38
-53
lines changed

7 files changed

+38
-53
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ if (!__EXPERIMENTAL__) {
306306
gate(flags => flags.enableOwnerStacks)
307307
? [
308308
__DEV__
309-
? '\n' +
310-
//
311-
' in Indirection (at **)\n' +
312-
' in App (at **)'
309+
? '\n in Indirection (at **)' +
310+
'\n in App (at **)' +
311+
'\n in Preview (at **)' +
312+
'\n in PreviewApp (at **)'
313313
: null,
314314
]
315315
: [],

packages/react-reconciler/src/ReactInternalTypes.js

+1-1
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

+1-1
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

+20-2
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,
@@ -395,8 +397,6 @@ const {
395397
TaintRegistryPendingRequests,
396398
} = ReactSharedInternals;
397399

398-
ReactSharedInternals.A = DefaultAsyncDispatcher;
399-
400400
function throwTaintViolation(message: string) {
401401
// eslint-disable-next-line react-internal/prod-error-codes
402402
throw new Error(message);
@@ -450,6 +450,24 @@ function RequestInstance(
450450
onAllReady: () => void,
451451
onFatalError: (error: mixed) => void,
452452
) {
453+
const previousAsyncDispatcher = ReactSharedInternals.A;
454+
455+
if (previousAsyncDispatcher !== null) {
456+
const previousActiveCache = previousAsyncDispatcher.getActiveCache();
457+
if (previousActiveCache !== null) {
458+
ReactSharedInternals.A = ({
459+
getActiveCache(): AsyncCache | null {
460+
return previousActiveCache;
461+
},
462+
}: any);
463+
if (__DEV__ || !disableStringRefs) {
464+
ReactSharedInternals.A.getOwner = DefaultAsyncDispatcher.getOwner;
465+
}
466+
}
467+
} else {
468+
ReactSharedInternals.A = DefaultAsyncDispatcher;
469+
}
470+
453471
if (__DEV__) {
454472
// Unlike Fizz or Fiber, we don't reset this and just keep it on permanently.
455473
// This lets it act more like the AsyncDispatcher so that we can get the

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

+2-39
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

+8-5
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,16 @@ 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();
69-
// TODO: Warn if undefined?
70-
activeCache.set(createCacheRoot, fnMap);
70+
if (activeCache !== null) {
71+
// TODO: Warn if undefined?
72+
activeCache.set(createCacheRoot, fnMap);
73+
}
7174
}
7275
const fnNode = fnMap.get(fn);
7376
let cacheNode: CacheNode<T>;

packages/react/src/ReactHooks.js

+2-1
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)