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 2750e2f commit b060677
Show file tree
Hide file tree
Showing 2 changed files with 91 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 @@ -311,10 +313,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
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
++atomState.n
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?.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 @@ -614,13 +622,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 @@ -643,6 +649,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
74 changes: 74 additions & 0 deletions tests/vanilla/dependency.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,77 @@ 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] as [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]
}
}

let resolve

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.5.5)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (5.0.4)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.7.4)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.6.4)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.9.5)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.3.5)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.4.4)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 463 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.8.4)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.
const e = atom((get) => {

Check failure on line 464 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 464 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 464 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 464 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.1.5)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.

Check failure on line 464 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.2.3)

Variable 'resolve' implicitly has type 'any' in some locations where its type cannot be determined.
if (!get(a)) {
get(b)
} else {
get(c)
}
const promise = new Promise<void>((r) => {
resolve = () => {
r()
return promise
}
}).then(() => {
get(d)
})
return promise
})

const store = createStore()
// mounts a, b synchronously
const unsub = store.sub(e, () => {})
expect(mounted).toEqual([1, 1, 0, 0])

// mounts d asynchronously
await resolve!()

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.5.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (5.0.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.7.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.6.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.9.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.3.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.4.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 487 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.8.4)

Variable 'resolve' implicitly has an 'any' type.
expect(mounted).toEqual([1, 1, 0, 1])

Check failure on line 488 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 488 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 488 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 488 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.1.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 488 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.2.3)

Variable 'resolve' implicitly has an 'any' type.

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

// no change
await resolve!()

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.5.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (5.0.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.7.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.6.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.9.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.3.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.4.4)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 495 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.8.4)

Variable 'resolve' implicitly has an 'any' type.
expect(mounted).toEqual([1, 0, 1, 1])

Check failure on line 496 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.9.7)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 496 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.0.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 496 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (3.8.3)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 496 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.1.5)

Variable 'resolve' implicitly has an 'any' type.

Check failure on line 496 in tests/vanilla/dependency.test.tsx

View workflow job for this annotation

GitHub Actions / test_matrix (4.2.3)

Variable 'resolve' implicitly has an 'any' type.

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

0 comments on commit b060677

Please sign in to comment.