Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): recompute dependents in edge cases #2742

Merged
merged 9 commits into from
Oct 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add failing test
  • Loading branch information
dai-shi committed Sep 19, 2024
commit d0a67accd0d52b747438402b24601856293b374d
34 changes: 33 additions & 1 deletion tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, it } from 'vitest'
import { expect, it, vi } from 'vitest'
import { atom, createStore } from 'jotai/vanilla'

it('can propagate updates with async atom chains', async () => {
Expand Down Expand Up @@ -288,3 +288,35 @@ it('refreshes deps for each async read', async () => {
resolve.splice(0).forEach((fn) => fn())
expect(values).toEqual([0, 1])
})

it('should not re-evaluate stable derived atom values in situations where dependencies are re-ordered (#2738)', () => {
const callCounter = vi.fn()
const rootAtom = atom(false)
const stableDep = atom((get) => {
get(rootAtom)
return 1
})
const stableDepDep = atom((get) => {
get(stableDep)
callCounter()
return 2
})

const newAtom = atom((get) => {
if (get(rootAtom)) {
return get(stableDepDep)
}

return get(stableDep)
})

const store = createStore()
store.sub(stableDepDep, () => {})
store.sub(newAtom, () => {})
expect(store.get(stableDepDep)).toBe(2)
expect(callCounter).toHaveBeenCalledTimes(1)

store.set(rootAtom, true)
expect(store.get(newAtom)).toBe(2)
expect(callCounter).toHaveBeenCalledTimes(1)
})
Loading