Skip to content

Commit bd9468d

Browse files
dmaskaskydai-shi
andauthored
fix: derived atom is not recomputed after its dependencies changed #2906 (#2907)
* add failing test * a single recomputeDependents in flushBatch * centralize finishRecompute * cleanup * fix test * simplify test * remove redundant changedAtoms set * mark original atom changed * flushBatch while condition includes dependents size * refactor: pass recompute as arg to flushBatch * rename getMountedDependents and simplify * remove unnecessary delete dirtybit * add single recomputeDependents to batch --------- Co-authored-by: daishi <[email protected]>
1 parent efb4573 commit bd9468d

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

tests/vanilla/store.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,24 @@ it('recomputes dependents of unmounted atoms', () => {
11231123
expect(bRead).not.toHaveBeenCalled()
11241124
})
11251125

1126+
it('recomputes all changed atom dependents together', async () => {
1127+
const a = atom([0])
1128+
const b = atom([0])
1129+
const a0 = atom((get) => get(a)[0]!)
1130+
const b0 = atom((get) => get(b)[0]!)
1131+
const a0b0 = atom((get) => [get(a0), get(b0)])
1132+
const w = atom(null, (_, set) => {
1133+
set(a, [0])
1134+
set(b, [1])
1135+
})
1136+
const store = createStore()
1137+
store.sub(a0b0, () => {})
1138+
store.set(w)
1139+
expect(store.get(a0)).toBe(0)
1140+
expect(store.get(b0)).toBe(1)
1141+
expect(store.get(a0b0)).toEqual([0, 1])
1142+
})
1143+
11261144
it('should not inf on subscribe or unsubscribe', async () => {
11271145
const store = createStore()
11281146
const countAtom = atom(0)

0 commit comments

Comments
 (0)