From db2611959ba716024f03f06b8f20bc4bd7088d9b Mon Sep 17 00:00:00 2001 From: daishi Date: Tue, 28 Jan 2025 00:13:43 +0900 Subject: [PATCH 1/2] add failing test --- tests/vanilla/store.test.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/vanilla/store.test.tsx b/tests/vanilla/store.test.tsx index 1e392bef86..062938dce5 100644 --- a/tests/vanilla/store.test.tsx +++ b/tests/vanilla/store.test.tsx @@ -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) +}) From adba60b83e9e95a0ecf53beff60ec6282b538377 Mon Sep 17 00:00:00 2001 From: daishi Date: Tue, 28 Jan 2025 00:17:50 +0900 Subject: [PATCH 2/2] fix it --- src/vanilla/store.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/vanilla/store.ts b/src/vanilla/store.ts index a5eb556dc7..15276af798 100644 --- a/src/vanilla/store.ts +++ b/src/vanilla/store.ts @@ -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) } } }