@@ -2,6 +2,7 @@ let React;
2
2
let ReactNoop ;
3
3
let Cache ;
4
4
let getCacheSignal ;
5
+ let getCacheForType ;
5
6
let Scheduler ;
6
7
let assertLog ;
7
8
let act ;
@@ -10,13 +11,11 @@ let Activity;
10
11
let useCacheRefresh ;
11
12
let startTransition ;
12
13
let useState ;
13
- let cache ;
14
14
15
- let getTextCache ;
16
15
let textCaches ;
17
16
let seededCache ;
18
17
19
- describe ( 'ReactCache ' , ( ) => {
18
+ describe ( 'ReactCacheElement ' , ( ) => {
20
19
beforeEach ( ( ) => {
21
20
jest . resetModules ( ) ;
22
21
@@ -26,9 +25,9 @@ describe('ReactCache', () => {
26
25
Scheduler = require ( 'scheduler' ) ;
27
26
act = require ( 'internal-test-utils' ) . act ;
28
27
Suspense = React . Suspense ;
29
- cache = React . cache ;
30
28
Activity = React . unstable_Activity ;
31
29
getCacheSignal = React . unstable_getCacheSignal ;
30
+ getCacheForType = React . unstable_getCacheForType ;
32
31
useCacheRefresh = React . unstable_useCacheRefresh ;
33
32
startTransition = React . startTransition ;
34
33
useState = React . useState ;
@@ -38,59 +37,57 @@ describe('ReactCache', () => {
38
37
39
38
textCaches = [ ] ;
40
39
seededCache = null ;
40
+ } ) ;
41
41
42
- if ( gate ( flags => flags . enableCache ) ) {
43
- getTextCache = cache ( ( ) => {
44
- if ( seededCache !== null ) {
45
- // Trick to seed a cache before it exists.
46
- // TODO: Need a built-in API to seed data before the initial render (i.e.
47
- // not a refresh because nothing has mounted yet).
48
- const textCache = seededCache ;
49
- seededCache = null ;
50
- return textCache ;
51
- }
52
-
53
- const data = new Map ( ) ;
54
- const version = textCaches . length + 1 ;
55
- const textCache = {
56
- version,
57
- data,
58
- resolve ( text ) {
59
- const record = data . get ( text ) ;
60
- if ( record === undefined ) {
61
- const newRecord = {
62
- status : 'resolved' ,
63
- value : text ,
64
- cleanupScheduled : false ,
65
- } ;
66
- data . set ( text , newRecord ) ;
67
- } else if ( record . status === 'pending' ) {
68
- record . value . resolve ( ) ;
69
- }
70
- } ,
71
- reject ( text , error ) {
72
- const record = data . get ( text ) ;
73
- if ( record === undefined ) {
74
- const newRecord = {
75
- status : 'rejected' ,
76
- value : error ,
77
- cleanupScheduled : false ,
78
- } ;
79
- data . set ( text , newRecord ) ;
80
- } else if ( record . status === 'pending' ) {
81
- record . value . reject ( ) ;
82
- }
83
- } ,
84
- } ;
85
- textCaches . push ( textCache ) ;
86
- return textCache ;
87
- } ) ;
42
+ function createTextCache ( ) {
43
+ if ( seededCache !== null ) {
44
+ // Trick to seed a cache before it exists.
45
+ // TODO: Need a built-in API to seed data before the initial render (i.e.
46
+ // not a refresh because nothing has mounted yet).
47
+ const textCache = seededCache ;
48
+ seededCache = null ;
49
+ return textCache ;
88
50
}
89
- } ) ;
51
+
52
+ const data = new Map ( ) ;
53
+ const version = textCaches . length + 1 ;
54
+ const textCache = {
55
+ version,
56
+ data,
57
+ resolve ( text ) {
58
+ const record = data . get ( text ) ;
59
+ if ( record === undefined ) {
60
+ const newRecord = {
61
+ status : 'resolved' ,
62
+ value : text ,
63
+ cleanupScheduled : false ,
64
+ } ;
65
+ data . set ( text , newRecord ) ;
66
+ } else if ( record . status === 'pending' ) {
67
+ record . value . resolve ( ) ;
68
+ }
69
+ } ,
70
+ reject ( text , error ) {
71
+ const record = data . get ( text ) ;
72
+ if ( record === undefined ) {
73
+ const newRecord = {
74
+ status : 'rejected' ,
75
+ value : error ,
76
+ cleanupScheduled : false ,
77
+ } ;
78
+ data . set ( text , newRecord ) ;
79
+ } else if ( record . status === 'pending' ) {
80
+ record . value . reject ( ) ;
81
+ }
82
+ } ,
83
+ } ;
84
+ textCaches . push ( textCache ) ;
85
+ return textCache ;
86
+ }
90
87
91
88
function readText ( text ) {
92
89
const signal = getCacheSignal ? getCacheSignal ( ) : null ;
93
- const textCache = getTextCache ( ) ;
90
+ const textCache = getCacheForType ( createTextCache ) ;
94
91
const record = textCache . data . get ( text ) ;
95
92
if ( record !== undefined ) {
96
93
if ( ! record . cleanupScheduled ) {
@@ -167,7 +164,7 @@ describe('ReactCache', () => {
167
164
168
165
function seedNextTextCache ( text ) {
169
166
if ( seededCache === null ) {
170
- seededCache = getTextCache ( ) ;
167
+ seededCache = createTextCache ( ) ;
171
168
}
172
169
seededCache . resolve ( text ) ;
173
170
}
@@ -182,7 +179,7 @@ describe('ReactCache', () => {
182
179
}
183
180
}
184
181
185
- // @gate enableCacheElement && enableCache
182
+ // @gate enableCacheElement
186
183
test ( 'render Cache component' , async ( ) => {
187
184
const root = ReactNoop . createRoot ( ) ;
188
185
await act ( ( ) => {
@@ -191,7 +188,7 @@ describe('ReactCache', () => {
191
188
expect ( root ) . toMatchRenderedOutput ( 'Hi' ) ;
192
189
} ) ;
193
190
194
- // @gate enableCacheElement && enableCache
191
+ // @gate enableCacheElement
195
192
test ( 'mount new data' , async ( ) => {
196
193
const root = ReactNoop . createRoot ( ) ;
197
194
await act ( ( ) => {
@@ -247,7 +244,7 @@ describe('ReactCache', () => {
247
244
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
248
245
} ) ;
249
246
250
- // @gate enableCacheElement && enableCache
247
+ // @gate enableCacheElement
251
248
test ( 'multiple new Cache boundaries in the same mount share the same, fresh root cache' , async ( ) => {
252
249
function App ( ) {
253
250
return (
@@ -290,7 +287,7 @@ describe('ReactCache', () => {
290
287
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
291
288
} ) ;
292
289
293
- // @gate enableCacheElement && enableCache
290
+ // @gate enableCacheElement
294
291
test ( 'multiple new Cache boundaries in the same update share the same, fresh cache' , async ( ) => {
295
292
function App ( { showMore} ) {
296
293
return showMore ? (
@@ -342,7 +339,7 @@ describe('ReactCache', () => {
342
339
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
343
340
} ) ;
344
341
345
- // @gate enableCacheElement && enableCache
342
+ // @gate enableCacheElement
346
343
test (
347
344
'nested cache boundaries share the same cache as the root during ' +
348
345
'the initial render' ,
@@ -382,7 +379,7 @@ describe('ReactCache', () => {
382
379
} ,
383
380
) ;
384
381
385
- // @gate enableCacheElement && enableCache
382
+ // @gate enableCacheElement
386
383
test ( 'new content inside an existing Cache boundary should re-use already cached data' , async ( ) => {
387
384
function App ( { showMore} ) {
388
385
return (
@@ -426,7 +423,7 @@ describe('ReactCache', () => {
426
423
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
427
424
} ) ;
428
425
429
- // @gate enableCacheElement && enableCache
426
+ // @gate enableCacheElement
430
427
test ( 'a new Cache boundary uses fresh cache' , async ( ) => {
431
428
// The only difference from the previous test is that the "Show More"
432
429
// content is wrapped in a nested <Cache /> boundary
@@ -484,7 +481,7 @@ describe('ReactCache', () => {
484
481
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
485
482
} ) ;
486
483
487
- // @gate enableCacheElement && enableCache
484
+ // @gate enableCacheElement
488
485
test ( 'inner/outer cache boundaries uses the same cache instance on initial render' , async ( ) => {
489
486
const root = ReactNoop . createRoot ( ) ;
490
487
@@ -566,7 +563,7 @@ describe('ReactCache', () => {
566
563
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
567
564
} ) ;
568
565
569
- // @gate enableCacheElement && enableCache
566
+ // @gate enableCacheElement
570
567
test ( 'inner/ outer cache boundaries added in the same update use the same cache instance' , async ( ) => {
571
568
const root = ReactNoop . createRoot ( ) ;
572
569
@@ -655,7 +652,6 @@ describe('ReactCache', () => {
655
652
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
656
653
} ) ;
657
654
658
- // @gate enableCache
659
655
test ( 'refresh a cache boundary' , async ( ) => {
660
656
let refresh ;
661
657
function App ( ) {
@@ -705,7 +701,7 @@ describe('ReactCache', () => {
705
701
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
706
702
} ) ;
707
703
708
- // @gate enableCacheElement && enableCache
704
+ // @gate enableCacheElement
709
705
test ( 'refresh the root cache' , async ( ) => {
710
706
let refresh ;
711
707
function App ( ) {
@@ -753,7 +749,7 @@ describe('ReactCache', () => {
753
749
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
754
750
} ) ;
755
751
756
- // @gate enableCacheElement && enableCache
752
+ // @gate enableCacheElement
757
753
test ( 'refresh the root cache without a transition' , async ( ) => {
758
754
let refresh ;
759
755
function App ( ) {
@@ -808,20 +804,11 @@ describe('ReactCache', () => {
808
804
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
809
805
} ) ;
810
806
811
- // @gate enableCacheElement && enableCache
807
+ // @gate enableCacheElement
812
808
test ( 'refresh a cache with seed data' , async ( ) => {
813
- let refreshWithSeed ;
809
+ let refresh ;
814
810
function App ( ) {
815
- const refresh = useCacheRefresh ( ) ;
816
- const [ seed , setSeed ] = useState ( { fn : null } ) ;
817
- if ( seed . fn ) {
818
- seed . fn ( ) ;
819
- seed . fn = null ;
820
- }
821
- refreshWithSeed = fn => {
822
- setSeed ( { fn} ) ;
823
- refresh ( ) ;
824
- } ;
811
+ refresh = useCacheRefresh ( ) ;
825
812
return < AsyncText showVersion = { true } text = "A" /> ;
826
813
}
827
814
@@ -851,12 +838,11 @@ describe('ReactCache', () => {
851
838
// server mutation.
852
839
// TODO: Seeding multiple typed textCaches. Should work by calling `refresh`
853
840
// multiple times with different key/value pairs
854
- startTransition ( ( ) =>
855
- refreshWithSeed ( ( ) => {
856
- const textCache = getTextCache ( ) ;
857
- textCache . resolve ( 'A' ) ;
858
- } ) ,
859
- ) ;
841
+ startTransition ( ( ) => {
842
+ const textCache = createTextCache ( ) ;
843
+ textCache . resolve ( 'A' ) ;
844
+ startTransition ( ( ) => refresh ( createTextCache , textCache ) ) ;
845
+ } ) ;
860
846
} ) ;
861
847
// The root should re-render without a cache miss.
862
848
// The cache is not cleared up yet, since it's still reference by the root
@@ -871,7 +857,7 @@ describe('ReactCache', () => {
871
857
expect ( root ) . toMatchRenderedOutput ( 'Bye' ) ;
872
858
} ) ;
873
859
874
- // @gate enableCacheElement && enableCache
860
+ // @gate enableCacheElement
875
861
test ( 'refreshing a parent cache also refreshes its children' , async ( ) => {
876
862
let refreshShell ;
877
863
function RefreshShell ( ) {
@@ -946,7 +932,7 @@ describe('ReactCache', () => {
946
932
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
947
933
} ) ;
948
934
949
- // @gate enableCacheElement && enableCache
935
+ // @gate enableCacheElement
950
936
test (
951
937
'refreshing a cache boundary does not refresh the other boundaries ' +
952
938
'that mounted at the same time (i.e. the ones that share the same cache)' ,
@@ -1027,7 +1013,7 @@ describe('ReactCache', () => {
1027
1013
} ,
1028
1014
) ;
1029
1015
1030
- // @gate enableCacheElement && enableCache
1016
+ // @gate enableCacheElement
1031
1017
test (
1032
1018
'mount a new Cache boundary in a sibling while simultaneously ' +
1033
1019
'resolving a Suspense boundary' ,
@@ -1096,7 +1082,7 @@ describe('ReactCache', () => {
1096
1082
} ,
1097
1083
) ;
1098
1084
1099
- // @gate enableCacheElement && enableCache
1085
+ // @gate enableCacheElement
1100
1086
test ( 'cache pool is cleared once transitions that depend on it commit their shell' , async ( ) => {
1101
1087
function Child ( { text} ) {
1102
1088
return (
@@ -1189,7 +1175,7 @@ describe('ReactCache', () => {
1189
1175
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
1190
1176
} ) ;
1191
1177
1192
- // @gate enableCacheElement && enableCache
1178
+ // @gate enableCacheElement
1193
1179
test ( 'cache pool is not cleared by arbitrary commits' , async ( ) => {
1194
1180
function App ( ) {
1195
1181
return (
@@ -1268,7 +1254,7 @@ describe('ReactCache', () => {
1268
1254
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
1269
1255
} ) ;
1270
1256
1271
- // @gate enableCacheElement && enableCache
1257
+ // @gate enableCacheElement
1272
1258
test ( 'cache boundary uses a fresh cache when its key changes' , async ( ) => {
1273
1259
const root = ReactNoop . createRoot ( ) ;
1274
1260
seedNextTextCache ( 'A' ) ;
@@ -1307,7 +1293,7 @@ describe('ReactCache', () => {
1307
1293
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
1308
1294
} ) ;
1309
1295
1310
- // @gate enableCacheElement && enableCache
1296
+ // @gate enableCacheElement
1311
1297
test ( 'overlapping transitions after an initial mount use the same fresh cache' , async ( ) => {
1312
1298
const root = ReactNoop . createRoot ( ) ;
1313
1299
await act ( ( ) => {
@@ -1375,7 +1361,7 @@ describe('ReactCache', () => {
1375
1361
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
1376
1362
} ) ;
1377
1363
1378
- // @gate enableCacheElement && enableCache
1364
+ // @gate enableCacheElement
1379
1365
test ( 'overlapping updates after an initial mount use the same fresh cache' , async ( ) => {
1380
1366
const root = ReactNoop . createRoot ( ) ;
1381
1367
await act ( ( ) => {
@@ -1438,7 +1424,7 @@ describe('ReactCache', () => {
1438
1424
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
1439
1425
} ) ;
1440
1426
1441
- // @gate enableCacheElement && enableCache
1427
+ // @gate enableCacheElement
1442
1428
test ( 'cleans up cache only used in an aborted transition' , async ( ) => {
1443
1429
const root = ReactNoop . createRoot ( ) ;
1444
1430
seedNextTextCache ( 'A' ) ;
@@ -1493,7 +1479,7 @@ describe('ReactCache', () => {
1493
1479
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
1494
1480
} ) ;
1495
1481
1496
- // @gate enableCacheElement && enableCache
1482
+ // @gate enableCacheElement
1497
1483
test . skip ( 'if a root cache refresh never commits its fresh cache is released' , async ( ) => {
1498
1484
const root = ReactNoop . createRoot ( ) ;
1499
1485
let refresh ;
@@ -1535,7 +1521,7 @@ describe('ReactCache', () => {
1535
1521
expect ( root ) . toMatchRenderedOutput ( 'Bye!' ) ;
1536
1522
} ) ;
1537
1523
1538
- // @gate enableCacheElement && enableCache
1524
+ // @gate enableCacheElement
1539
1525
test . skip ( 'if a cache boundary refresh never commits its fresh cache is released' , async ( ) => {
1540
1526
const root = ReactNoop . createRoot ( ) ;
1541
1527
let refresh ;
0 commit comments