@@ -16,8 +16,9 @@ let React;
16
16
let ReactFeatureFlags ;
17
17
let ReactNoop ;
18
18
let Scheduler ;
19
- let useMutableSource ;
20
19
let act ;
20
+ let createMutableSource ;
21
+ let useMutableSource ;
21
22
22
23
function loadModules ( ) {
23
24
jest . resetModules ( ) ;
@@ -30,8 +31,9 @@ function loadModules() {
30
31
React = require ( 'react' ) ;
31
32
ReactNoop = require ( 'react-noop-renderer' ) ;
32
33
Scheduler = require ( 'scheduler' ) ;
33
- useMutableSource = React . useMutableSource ;
34
34
act = ReactNoop . act ;
35
+ createMutableSource = React . unstable_createMutableSource ;
36
+ useMutableSource = React . unstable_useMutableSource ;
35
37
}
36
38
37
39
describe ( 'useMutableSource' , ( ) => {
@@ -129,10 +131,6 @@ describe('useMutableSource', () => {
129
131
} ;
130
132
}
131
133
132
- function createMutableSource ( source ) {
133
- return React . createMutableSource ( source , param => param . version ) ;
134
- }
135
-
136
134
function Component ( { getSnapshot, label, mutableSource, subscribe} ) {
137
135
const snapshot = useMutableSource ( mutableSource , getSnapshot , subscribe ) ;
138
136
Scheduler . unstable_yieldValue ( `${ label } :${ snapshot } ` ) ;
@@ -144,7 +142,7 @@ describe('useMutableSource', () => {
144
142
// @gate experimental
145
143
it ( 'should subscribe to a source and schedule updates when it changes' , ( ) => {
146
144
const source = createSource ( 'one' ) ;
147
- const mutableSource = createMutableSource ( source ) ;
145
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
148
146
149
147
act ( ( ) => {
150
148
ReactNoop . renderToRootWithID (
@@ -212,7 +210,7 @@ describe('useMutableSource', () => {
212
210
// @gate experimental
213
211
it ( 'should restart work if a new source is mutated during render' , ( ) => {
214
212
const source = createSource ( 'one' ) ;
215
- const mutableSource = createMutableSource ( source ) ;
213
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
216
214
217
215
act ( ( ) => {
218
216
ReactNoop . render (
@@ -247,7 +245,7 @@ describe('useMutableSource', () => {
247
245
// @gate experimental
248
246
it ( 'should schedule an update if a new source is mutated between render and commit (subscription)' , ( ) => {
249
247
const source = createSource ( 'one' ) ;
250
- const mutableSource = createMutableSource ( source ) ;
248
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
251
249
252
250
act ( ( ) => {
253
251
ReactNoop . render (
@@ -287,10 +285,16 @@ describe('useMutableSource', () => {
287
285
// @gate experimental
288
286
it ( 'should unsubscribe and resubscribe if a new source is used' , ( ) => {
289
287
const sourceA = createSource ( 'a-one' ) ;
290
- const mutableSourceA = createMutableSource ( sourceA ) ;
288
+ const mutableSourceA = createMutableSource (
289
+ sourceA ,
290
+ param => param . versionA ,
291
+ ) ;
291
292
292
293
const sourceB = createSource ( 'b-one' ) ;
293
- const mutableSourceB = createMutableSource ( sourceB ) ;
294
+ const mutableSourceB = createMutableSource (
295
+ sourceB ,
296
+ param => param . versionB ,
297
+ ) ;
294
298
295
299
act ( ( ) => {
296
300
ReactNoop . render (
@@ -338,7 +342,7 @@ describe('useMutableSource', () => {
338
342
// @gate experimental
339
343
it ( 'should unsubscribe and resubscribe if a new subscribe function is provided' , ( ) => {
340
344
const source = createSource ( 'a-one' ) ;
341
- const mutableSource = createMutableSource ( source ) ;
345
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
342
346
343
347
const unsubscribeA = jest . fn ( ) ;
344
348
const subscribeA = jest . fn ( s => {
@@ -403,7 +407,7 @@ describe('useMutableSource', () => {
403
407
// @gate experimental
404
408
it ( 'should re-use previously read snapshot value when reading is unsafe' , ( ) => {
405
409
const source = createSource ( 'one' ) ;
406
- const mutableSource = createMutableSource ( source ) ;
410
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
407
411
408
412
act ( ( ) => {
409
413
ReactNoop . render (
@@ -460,7 +464,7 @@ describe('useMutableSource', () => {
460
464
// @gate experimental
461
465
it ( 'should read from source on newly mounted subtree if no pending updates are scheduled for source' , ( ) => {
462
466
const source = createSource ( 'one' ) ;
463
- const mutableSource = createMutableSource ( source ) ;
467
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
464
468
465
469
act ( ( ) => {
466
470
ReactNoop . render (
@@ -500,7 +504,7 @@ describe('useMutableSource', () => {
500
504
// @gate experimental
501
505
it ( 'should throw and restart render if source and snapshot are unavailable during an update' , ( ) => {
502
506
const source = createSource ( 'one' ) ;
503
- const mutableSource = createMutableSource ( source ) ;
507
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
504
508
505
509
act ( ( ) => {
506
510
ReactNoop . render (
@@ -567,7 +571,7 @@ describe('useMutableSource', () => {
567
571
// @gate experimental
568
572
it ( 'should throw and restart render if source and snapshot are unavailable during a sync update' , ( ) => {
569
573
const source = createSource ( 'one' ) ;
570
- const mutableSource = createMutableSource ( source ) ;
574
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
571
575
572
576
act ( ( ) => {
573
577
ReactNoop . render (
@@ -631,7 +635,7 @@ describe('useMutableSource', () => {
631
635
// @gate experimental
632
636
it ( 'should only update components whose subscriptions fire' , ( ) => {
633
637
const source = createComplexSource ( 'a:one' , 'b:one' ) ;
634
- const mutableSource = createMutableSource ( source ) ;
638
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
635
639
636
640
// Subscribe to part of the store.
637
641
const getSnapshotA = s => s . valueA ;
@@ -670,7 +674,7 @@ describe('useMutableSource', () => {
670
674
// @gate experimental
671
675
it ( 'should detect tearing in part of the store not yet subscribed to' , ( ) => {
672
676
const source = createComplexSource ( 'a:one' , 'b:one' ) ;
673
- const mutableSource = createMutableSource ( source ) ;
677
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
674
678
675
679
// Subscribe to part of the store.
676
680
const getSnapshotA = s => s . valueA ;
@@ -737,7 +741,7 @@ describe('useMutableSource', () => {
737
741
const MockComponent = jest . fn ( Component ) ;
738
742
739
743
const source = createSource ( 'one' ) ;
740
- const mutableSource = createMutableSource ( source ) ;
744
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
741
745
742
746
act ( ( ) => {
743
747
ReactNoop . render (
@@ -762,7 +766,7 @@ describe('useMutableSource', () => {
762
766
// @gate experimental
763
767
it ( 'should throw and restart if getSnapshot changes between scheduled update and re-render' , ( ) => {
764
768
const source = createSource ( 'one' ) ;
765
- const mutableSource = createMutableSource ( source ) ;
769
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
766
770
767
771
const newGetSnapshot = s => 'new:' + defaultGetSnapshot ( s ) ;
768
772
@@ -808,7 +812,7 @@ describe('useMutableSource', () => {
808
812
// @gate experimental
809
813
it ( 'should recover from a mutation during yield when other work is scheduled' , ( ) => {
810
814
const source = createSource ( 'one' ) ;
811
- const mutableSource = createMutableSource ( source ) ;
815
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
812
816
813
817
act ( ( ) => {
814
818
// Start a render that uses the mutable source.
@@ -842,7 +846,7 @@ describe('useMutableSource', () => {
842
846
// @gate experimental
843
847
it ( 'should not throw if the new getSnapshot returns the same snapshot value' , ( ) => {
844
848
const source = createSource ( 'one' ) ;
845
- const mutableSource = createMutableSource ( source ) ;
849
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
846
850
847
851
const onRenderA = jest . fn ( ) ;
848
852
const onRenderB = jest . fn ( ) ;
@@ -897,7 +901,7 @@ describe('useMutableSource', () => {
897
901
// @gate experimental
898
902
it ( 'should not throw if getSnapshot changes but the source can be safely read from anyway' , ( ) => {
899
903
const source = createSource ( 'one' ) ;
900
- const mutableSource = createMutableSource ( source ) ;
904
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
901
905
902
906
const newGetSnapshot = s => 'new:' + defaultGetSnapshot ( s ) ;
903
907
@@ -942,7 +946,7 @@ describe('useMutableSource', () => {
942
946
{ id : 2 , name : 'Bar' } ,
943
947
] ,
944
948
} ) ;
945
- const mutableSource = createMutableSource ( source ) ;
949
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
946
950
947
951
function FriendsList ( ) {
948
952
const getSnapshot = React . useCallback (
@@ -1004,7 +1008,7 @@ describe('useMutableSource', () => {
1004
1008
// @gate experimental
1005
1009
it ( 'should not warn about updates that fire between unmount and passive unsubscribe' , ( ) => {
1006
1010
const source = createSource ( 'one' ) ;
1007
- const mutableSource = createMutableSource ( source ) ;
1011
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1008
1012
1009
1013
function Wrapper ( ) {
1010
1014
React . useLayoutEffect ( ( ) => ( ) => {
@@ -1044,7 +1048,7 @@ describe('useMutableSource', () => {
1044
1048
a : 'initial' ,
1045
1049
b : 'initial' ,
1046
1050
} ) ;
1047
- const mutableSource = createMutableSource ( source ) ;
1051
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1048
1052
1049
1053
const getSnapshotA = ( ) => source . value . a ;
1050
1054
const getSnapshotB = ( ) => source . value . b ;
@@ -1089,7 +1093,7 @@ describe('useMutableSource', () => {
1089
1093
a : 'initial' ,
1090
1094
b : 'initial' ,
1091
1095
} ) ;
1092
- const mutableSource = createMutableSource ( source ) ;
1096
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1093
1097
1094
1098
const getSnapshotA = ( ) => source . value . a ;
1095
1099
const getSnapshotB = ( ) => source . value . b ;
@@ -1144,8 +1148,14 @@ describe('useMutableSource', () => {
1144
1148
it ( 'should clear the update queue when source changes with pending lower priority updates' , async ( ) => {
1145
1149
const sourceA = createSource ( 'initial' ) ;
1146
1150
const sourceB = createSource ( 'initial' ) ;
1147
- const mutableSourceA = createMutableSource ( sourceA ) ;
1148
- const mutableSourceB = createMutableSource ( sourceB ) ;
1151
+ const mutableSourceA = createMutableSource (
1152
+ sourceA ,
1153
+ param => param . versionA ,
1154
+ ) ;
1155
+ const mutableSourceB = createMutableSource (
1156
+ sourceB ,
1157
+ param => param . versionB ,
1158
+ ) ;
1149
1159
1150
1160
function App ( { toggle} ) {
1151
1161
const state = useMutableSource (
@@ -1185,7 +1195,7 @@ describe('useMutableSource', () => {
1185
1195
a : 'foo' ,
1186
1196
b : 'bar' ,
1187
1197
} ) ;
1188
- const mutableSource = createMutableSource ( source ) ;
1198
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1189
1199
1190
1200
const getSnapshotA = ( ) => source . value . a ;
1191
1201
const getSnapshotB = ( ) => source . value . b ;
@@ -1275,7 +1285,7 @@ describe('useMutableSource', () => {
1275
1285
a : 'foo' ,
1276
1286
b : 'bar' ,
1277
1287
} ) ;
1278
- const mutableSource = createMutableSource ( source ) ;
1288
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1279
1289
1280
1290
function mutateB ( newB ) {
1281
1291
source . value = {
@@ -1334,7 +1344,7 @@ describe('useMutableSource', () => {
1334
1344
a : 'a0' ,
1335
1345
b : 'b0' ,
1336
1346
} ) ;
1337
- const mutableSource = createMutableSource ( source ) ;
1347
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1338
1348
1339
1349
const getSnapshotA = ( ) => source . value . a ;
1340
1350
const getSnapshotB = ( ) => source . value . b ;
@@ -1408,7 +1418,7 @@ describe('useMutableSource', () => {
1408
1418
a : 'a0' ,
1409
1419
b : 'b0' ,
1410
1420
} ) ;
1411
- const mutableSource = createMutableSource ( source ) ;
1421
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1412
1422
1413
1423
const getSnapshotA = ( ) => source . value . a ;
1414
1424
const getSnapshotB = ( ) => source . value . b ;
@@ -1496,7 +1506,7 @@ describe('useMutableSource', () => {
1496
1506
// @gate experimental
1497
1507
it ( 'warns about functions being used as snapshot values' , async ( ) => {
1498
1508
const source = createSource ( ( ) => 'a' ) ;
1499
- const mutableSource = createMutableSource ( source ) ;
1509
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1500
1510
1501
1511
const getSnapshot = ( ) => source . value ;
1502
1512
@@ -1526,7 +1536,7 @@ describe('useMutableSource', () => {
1526
1536
const { useEffect} = React ;
1527
1537
1528
1538
const source = createComplexSource ( '1' , '2' ) ;
1529
- const mutableSource = createMutableSource ( source ) ;
1539
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1530
1540
1531
1541
// Subscribe to part of the store.
1532
1542
const getSnapshotA = s => s . valueA ;
@@ -1630,7 +1640,7 @@ describe('useMutableSource', () => {
1630
1640
// @gate experimental
1631
1641
it ( 'should not tear with newly mounted component when updates were scheduled at a lower priority' , async ( ) => {
1632
1642
const source = createSource ( 'one' ) ;
1633
- const mutableSource = createMutableSource ( source ) ;
1643
+ const mutableSource = createMutableSource ( source , param => param . version ) ;
1634
1644
1635
1645
let committedA = null ;
1636
1646
let committedB = null ;
@@ -1713,7 +1723,10 @@ describe('useMutableSource', () => {
1713
1723
// @gate experimental
1714
1724
it ( 'should warn if the subscribe function does not return an unsubscribe function' , ( ) => {
1715
1725
const source = createSource ( 'one' ) ;
1716
- const mutableSource = createMutableSource ( source ) ;
1726
+ const mutableSource = createMutableSource (
1727
+ source ,
1728
+ param => param . version ,
1729
+ ) ;
1717
1730
1718
1731
const brokenSubscribe = ( ) => { } ;
1719
1732
@@ -1736,7 +1749,10 @@ describe('useMutableSource', () => {
1736
1749
// @gate experimental
1737
1750
it ( 'should error if multiple renderers of the same type use a mutable source at the same time' , ( ) => {
1738
1751
const source = createSource ( 'one' ) ;
1739
- const mutableSource = createMutableSource ( source ) ;
1752
+ const mutableSource = createMutableSource (
1753
+ source ,
1754
+ param => param . version ,
1755
+ ) ;
1740
1756
1741
1757
act ( ( ) => {
1742
1758
// Start a render that uses the mutable source.
@@ -1793,7 +1809,10 @@ describe('useMutableSource', () => {
1793
1809
// @gate experimental
1794
1810
it ( 'should error if multiple renderers of the same type use a mutable source at the same time with mutation between' , ( ) => {
1795
1811
const source = createSource ( 'one' ) ;
1796
- const mutableSource = createMutableSource ( source ) ;
1812
+ const mutableSource = createMutableSource (
1813
+ source ,
1814
+ param => param . version ,
1815
+ ) ;
1797
1816
1798
1817
act ( ( ) => {
1799
1818
// Start a render that uses the mutable source.
0 commit comments