Skip to content

Commit

Permalink
alternate fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 16, 2025
1 parent a1e1964 commit 538a4fc
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 119 deletions.
10 changes: 8 additions & 2 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,11 +566,17 @@ const buildStore = (...storeArgs: StoreArgs): Store => {

const mountDependencies = (atom: AnyAtom, atomState: AtomState) => {
if (atomState.m && !isPendingPromise(atomState.v)) {
for (const a of atomState.d.keys()) {
for (const [a, n] of atomState.d) {
if (!atomState.m.d.has(a)) {
const aMounted = mountAtom(a, ensureAtomState(a))
const aState = ensureAtomState(a)
const aMounted = mountAtom(a, aState)
aMounted.t.add(atom)
atomState.m.d.add(a)
if (n !== aState.n) {
changedAtoms.set(a, aState)
aState.u?.()
invalidateDependents(aState)
}
}
}
for (const a of atomState.m.d || []) {
Expand Down
24 changes: 0 additions & 24 deletions tests/testUtils.ts

This file was deleted.

93 changes: 0 additions & 93 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { expect, it, vi } from 'vitest'
import { atom, createStore } from 'jotai/vanilla'
import { createDebugStore } from '../../tests/testUtils'

it('can propagate updates with async atom chains', async () => {
const store = createStore()
Expand Down Expand Up @@ -426,95 +425,3 @@ it('batches sync writes', () => {
expect(fetch).toBeCalledWith(1)
expect(store.get(a)).toBe(1)
})

it('mounts and unmounts sync and async dependencies correctly', async () => {
const mounted = [0, 0, 0, 0, 0] as [number, number, number, number, number]
const a = atom(0)
a.debugLabel = 'a'
a.onMount = () => {
++mounted[0]
return () => {
--mounted[0]
}
}

const b = atom(0)
b.debugLabel = 'b'
b.onMount = () => {
++mounted[1]
return () => {
--mounted[1]
}
}

const c = atom(0)
c.debugLabel = 'c'
c.onMount = () => {
++mounted[2]
return () => {
--mounted[2]
}
}

const d = atom(0)
d.debugLabel = 'd'
d.onMount = () => {
++mounted[3]
return () => {
--mounted[3]
}
}

const e = atom(0)
e.debugLabel = 'e'
e.onMount = () => {
++mounted[4]
return () => {
--mounted[4]
}
}

let resolve: (() => Promise<void>) | undefined
const f = atom((get) => {
if (!get(a)) {
get(b)
} else {
get(c)
}
const promise = new Promise<void>((r) => {
resolve = () => {
r()
return promise
}
}).then(() => {
if (!get(a)) {
get(d)
} else {
get(e)
}
})
return promise
})
f.debugLabel = 'f'

const store = createDebugStore()
// mount a, b synchronously
const unsub = store.sub(f, () => {})
expect(mounted).toEqual([1, 1, 0, 0, 0])

// mount d asynchronously
await resolve!()
expect(mounted).toEqual([1, 1, 0, 1, 0])

// unmount b, mount c synchronously
store.set(a, 1)
expect(mounted).toEqual([1, 0, 1, 1, 0])

// unmount d, mount e asynchronously
await resolve!()
expect(mounted).toEqual([1, 0, 1, 0, 1])

// unmount a, b, d, e synchronously
unsub()
expect(mounted).toEqual([0, 0, 0, 0, 0])
})

0 comments on commit 538a4fc

Please sign in to comment.