Skip to content

Commit

Permalink
fix(core): derived atom is not re-evaluated if it read between multip…
Browse files Browse the repository at this point in the history
…le set (#2960)

* add failing test

* fix it
  • Loading branch information
dai-shi authored Jan 31, 2025
1 parent 4636c08 commit 1a2bbb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,8 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
while (stack.length) {
const aState = stack.pop()!
for (const [d, s] of getMountedOrPendingDependents(aState)) {
if (!invalidatedAtoms.has(d)) {
invalidatedAtoms.set(d, s.n)
stack.push(s)
}
invalidatedAtoms.set(d, s.n)
stack.push(s)
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1209,3 +1209,17 @@ it('allows subcribing to atoms during mount', () => {
store.sub(a, () => {})
expect(bMounted).toBe(true)
})

it('updates with reading derived atoms (#2959)', () => {
const store = createStore()
const countAtom = atom(0)
const countDerivedAtom = atom((get) => get(countAtom))
const countUpAtom = atom(null, (get, set) => {
set(countAtom, 1)
get(countDerivedAtom)
set(countAtom, 2)
})
store.sub(countDerivedAtom, () => {})
store.set(countUpAtom)
expect(store.get(countDerivedAtom)).toBe(2)
})

0 comments on commit 1a2bbb5

Please sign in to comment.