@@ -1359,13 +1359,20 @@ describe('More perf comparisons', () => {
1359
1359
console . log ( 'cwCounters2.recomputations()' , cwCounters2 . recomputations ( ) )
1360
1360
} )
1361
1361
1362
- test ( 'Does something?' , async ( ) => {
1362
+ test . only ( 'Does something?' , async ( ) => {
1363
1363
const fn = vi . fn ( )
1364
+
1365
+ let resolve : ( ) => void
1366
+ const promise = new Promise < void > ( r => ( resolve = r ) )
1367
+
1364
1368
const registry = new FinalizationRegistry ( heldValue => {
1369
+ resolve ( )
1365
1370
console . log ( 'Garbage-collected value for ID: ' , heldValue )
1366
1371
fn ( heldValue )
1367
1372
} )
1368
1373
1374
+ const createSelectorWeakmap = createSelectorCreator ( weakMapMemoize )
1375
+
1369
1376
const store = configureStore ( {
1370
1377
reducer : {
1371
1378
counter : counterSlice . reducer ,
@@ -1380,24 +1387,57 @@ describe('More perf comparisons', () => {
1380
1387
1381
1388
const reduxStates : RootState [ ] = [ ]
1382
1389
1383
- for ( let i = 0 ; i < 10 ; i ++ ) {
1384
- store . dispatch ( counterSlice . actions . increment2 ( ) )
1390
+ const NUM_ITEMS = 10
1391
+
1392
+ for ( let i = 0 ; i < NUM_ITEMS ; i ++ ) {
1393
+ store . dispatch ( todosSlice . actions . toggleCompleted ( 1 ) )
1385
1394
const state = store . getState ( )
1386
1395
reduxStates . push ( state )
1387
1396
registry . register ( state , i )
1388
1397
}
1389
1398
1399
+ const cdTodoIdsAndNames = createSelectorWeakmap (
1400
+ ( state : RootState ) => state . todos ,
1401
+ todos => {
1402
+ // console.log('Recalculating todo IDs')
1403
+ return todos . map ( todo => ( { id : todo . id , name : todo . name } ) )
1404
+ }
1405
+ )
1406
+
1407
+ for ( const state of reduxStates ) {
1408
+ cdTodoIdsAndNames ( state )
1409
+ }
1410
+
1411
+ expect ( cdTodoIdsAndNames . recomputations ( ) ) . toBe ( NUM_ITEMS )
1412
+
1413
+ for ( const state of reduxStates ) {
1414
+ cdTodoIdsAndNames ( state )
1415
+ }
1416
+
1417
+ expect ( cdTodoIdsAndNames . recomputations ( ) ) . toBe ( NUM_ITEMS )
1418
+
1419
+ console . log ( 'clearCache: ' , cdTodoIdsAndNames . clearCache )
1420
+ cdTodoIdsAndNames . memoizedResultFunc . clearCache ( )
1421
+
1422
+ cdTodoIdsAndNames ( reduxStates [ 0 ] )
1423
+
1424
+ expect ( cdTodoIdsAndNames . recomputations ( ) ) . toBe ( NUM_ITEMS + 1 )
1425
+
1426
+ cdTodoIdsAndNames ( reduxStates [ 1 ] )
1427
+
1428
+ expect ( cdTodoIdsAndNames . recomputations ( ) ) . toBe ( NUM_ITEMS + 2 )
1429
+
1390
1430
console . log ( 'Before nulling out' )
1391
1431
// @ts -ignore
1392
- reduxStates [ 3 ] = null
1432
+ reduxStates [ 0 ] = null
1393
1433
console . log ( 'After nulling out' )
1394
1434
if ( global . gc ) {
1395
1435
global . gc ( )
1396
1436
}
1397
1437
console . log ( 'After GC' )
1398
1438
1399
- await new Promise ( resolve => setTimeout ( resolve , 0 ) )
1400
- expect ( fn ) . toHaveBeenCalledWith ( 3 )
1439
+ await promise
1440
+ expect ( fn ) . toHaveBeenCalledWith ( 0 )
1401
1441
1402
1442
// garbage-collected for ID: 3
1403
1443
} )
0 commit comments