9
9
10
10
import type { ReactComponentInfo } from 'shared/ReactTypes' ;
11
11
12
- import type { AsyncDispatcher } from 'react-reconciler/src/ReactInternalTypes' ;
12
+ import type {
13
+ AsyncCache ,
14
+ AsyncDispatcher ,
15
+ } from 'react-reconciler/src/ReactInternalTypes' ;
13
16
14
17
import { resolveRequest , getCache } from '../ReactFlightServer' ;
15
18
import ReactSharedInternals from '../ReactSharedInternalsServer' ;
@@ -28,38 +31,42 @@ function resolveCache(): Map<Function, mixed> {
28
31
return new Map ( ) ;
29
32
}
30
33
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 ;
48
53
}
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
+ } ;
50
64
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,
63
70
} : any ) ;
64
71
65
72
if ( __DEV__ ) {
0 commit comments