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(vanilla): bail out notifying with same values (v2 API) #1654

Merged
merged 2 commits into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 15 additions & 1 deletion src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,21 @@ export const createStore = () => {
mountDependencies(atom, atomState, prevAtomState?.d)
}
const mounted = mountedMap.get(atom)
mounted?.l.forEach((listener) => listener())
if (
mounted &&
!(
// TODO This seems pretty hacky. Hope to fix it.
// Maybe we could `mountDependencies` in `setAtomState`?
(
prevAtomState &&
!hasPromiseAtomValue(prevAtomState) &&
(isEqualAtomValue(prevAtomState, atomState) ||
isEqualAtomError(prevAtomState, atomState))
)
)
) {
mounted.l.forEach((listener) => listener())
}
} else if (__DEV__) {
console.warn('[Bug] no atom state to flush')
}
Expand Down
22 changes: 22 additions & 0 deletions tests/vanilla/store.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { atom, createStore } from 'jotai/vanilla'

it('should not fire subscription if primitive atom value is the same', async () => {
const store = createStore()
const countAtom = atom(0)
const callback = jest.fn()
store.sub(countAtom, callback)
const calledTimes = callback.mock.calls.length
store.set(countAtom, 0)
expect(callback).toBeCalledTimes(calledTimes)
})

it('should not fire subscription if derived atom value is the same', async () => {
const store = createStore()
const countAtom = atom(0)
const derivedAtom = atom((get) => get(countAtom) * 0)
const callback = jest.fn()
store.sub(derivedAtom, callback)
const calledTimes = callback.mock.calls.length
store.set(countAtom, 1)
expect(callback).toBeCalledTimes(calledTimes)
})