Skip to content

Commit

Permalink
handle mounting and unmounting of sync and async dependencies separately
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 8, 2025
1 parent b975cd3 commit 7addd25
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 10 deletions.
27 changes: 17 additions & 10 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ type Mounted = {
readonly l: Set<() => void>
/** Set of mounted atoms that the atom depends on. */
readonly d: Set<AnyAtom>
/** Set of dependencies added asynchronously */
readonly q: Set<AnyAtom>
/** Set of mounted atoms that depends on the atom. */
readonly t: Set<AnyAtom>
/** Function to run when the atom is unmounted. */
Expand Down Expand Up @@ -310,10 +312,8 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
for (const a of atomState.d.keys()) {
addPendingPromiseToDependency(atom, valueOrPromise, ensureAtomState(a))
}
atomState.v = valueOrPromise
} else {
atomState.v = valueOrPromise
}
atomState.v = valueOrPromise
delete atomState.e
delete atomState.x
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
Expand Down Expand Up @@ -375,6 +375,9 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
addDependency(atom, atomState, a, aState)
} else {
const batch = createBatch()
if (isPendingPromise(atomState.v) && !atomState.m?.d.has(a)) {
atomState.m?.q.add(a)
}
addDependency(atom, atomState, a, aState)
mountDependencies(batch, atom, atomState)
flushBatch(batch)
Expand Down Expand Up @@ -414,6 +417,11 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
const valueOrPromise = atomRead(atom, getter, options as never)
setAtomStateValueOrPromise(atom, atomState, valueOrPromise)
if (isPromiseLike(valueOrPromise)) {
if (batch) {
addBatchFunc(batch, 0, () => atomState.m?.q.clear())
} else {
atomState.m?.q.clear()
}
valueOrPromise.onCancel?.(() => controller?.abort())
const complete = () => {
if (atomState.m) {
Expand Down Expand Up @@ -602,13 +610,11 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
atomState.m.d.add(a)
}
}
if (!isPendingPromise(atomState.v)) {
for (const a of atomState.m.d || []) {
if (!atomState.d.has(a)) {
atomState.m.d.delete(a)
const aMounted = unmountAtom(batch, a, ensureAtomState(a))
aMounted?.t.delete(atom)
}
for (const a of atomState.m.d || []) {
if (!atomState.d.has(a) && !atomState.m.q.has(a)) {
atomState.m.d.delete(a)
const aMounted = unmountAtom(batch, a, ensureAtomState(a))
aMounted?.t.delete(atom)
}
}
}
Expand All @@ -631,6 +637,7 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
atomState.m = {
l: new Set(),
d: new Set(atomState.d.keys()),
q: new Set(),
t: new Set(),
}
atomState.h?.(batch)
Expand Down
86 changes: 86 additions & 0 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,89 @@ 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.onMount = () => {
++mounted[0]
return () => {
--mounted[0]
}
}

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

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

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

const e = atom(0)
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
})

const store = createStore()
// 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 7addd25

Please sign in to comment.