Skip to content

Commit b5499d6

Browse files
committed
WIP Got garbage-collecting selector results working
1 parent dd8feec commit b5499d6

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

test/test_selector.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,13 +1359,20 @@ describe('More perf comparisons', () => {
13591359
console.log('cwCounters2.recomputations()', cwCounters2.recomputations())
13601360
})
13611361

1362-
test('Does something?', async () => {
1362+
test.only('Does something?', async () => {
13631363
const fn = vi.fn()
1364+
1365+
let resolve: () => void
1366+
const promise = new Promise<void>(r => (resolve = r))
1367+
13641368
const registry = new FinalizationRegistry(heldValue => {
1369+
resolve()
13651370
console.log('Garbage-collected value for ID: ', heldValue)
13661371
fn(heldValue)
13671372
})
13681373

1374+
const createSelectorWeakmap = createSelectorCreator(weakMapMemoize)
1375+
13691376
const store = configureStore({
13701377
reducer: {
13711378
counter: counterSlice.reducer,
@@ -1380,24 +1387,57 @@ describe('More perf comparisons', () => {
13801387

13811388
const reduxStates: RootState[] = []
13821389

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))
13851394
const state = store.getState()
13861395
reduxStates.push(state)
13871396
registry.register(state, i)
13881397
}
13891398

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+
13901430
console.log('Before nulling out')
13911431
// @ts-ignore
1392-
reduxStates[3] = null
1432+
reduxStates[0] = null
13931433
console.log('After nulling out')
13941434
if (global.gc) {
13951435
global.gc()
13961436
}
13971437
console.log('After GC')
13981438

1399-
await new Promise(resolve => setTimeout(resolve, 0))
1400-
expect(fn).toHaveBeenCalledWith(3)
1439+
await promise
1440+
expect(fn).toHaveBeenCalledWith(0)
14011441

14021442
// garbage-collected for ID: 3
14031443
})

0 commit comments

Comments
 (0)