Skip to content

Commit 7addd25

Browse files
committed
handle mounting and unmounting of sync and async dependencies separately
1 parent b975cd3 commit 7addd25

File tree

2 files changed

+103
-10
lines changed

2 files changed

+103
-10
lines changed

src/vanilla/store.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type Mounted = {
7474
readonly l: Set<() => void>
7575
/** Set of mounted atoms that the atom depends on. */
7676
readonly d: Set<AnyAtom>
77+
/** Set of dependencies added asynchronously */
78+
readonly q: Set<AnyAtom>
7779
/** Set of mounted atoms that depends on the atom. */
7880
readonly t: Set<AnyAtom>
7981
/** Function to run when the atom is unmounted. */
@@ -310,10 +312,8 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
310312
for (const a of atomState.d.keys()) {
311313
addPendingPromiseToDependency(atom, valueOrPromise, ensureAtomState(a))
312314
}
313-
atomState.v = valueOrPromise
314-
} else {
315-
atomState.v = valueOrPromise
316315
}
316+
atomState.v = valueOrPromise
317317
delete atomState.e
318318
delete atomState.x
319319
if (!hasPrevValue || !Object.is(prevValue, atomState.v)) {
@@ -375,6 +375,9 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
375375
addDependency(atom, atomState, a, aState)
376376
} else {
377377
const batch = createBatch()
378+
if (isPendingPromise(atomState.v) && !atomState.m?.d.has(a)) {
379+
atomState.m?.q.add(a)
380+
}
378381
addDependency(atom, atomState, a, aState)
379382
mountDependencies(batch, atom, atomState)
380383
flushBatch(batch)
@@ -414,6 +417,11 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
414417
const valueOrPromise = atomRead(atom, getter, options as never)
415418
setAtomStateValueOrPromise(atom, atomState, valueOrPromise)
416419
if (isPromiseLike(valueOrPromise)) {
420+
if (batch) {
421+
addBatchFunc(batch, 0, () => atomState.m?.q.clear())
422+
} else {
423+
atomState.m?.q.clear()
424+
}
417425
valueOrPromise.onCancel?.(() => controller?.abort())
418426
const complete = () => {
419427
if (atomState.m) {
@@ -602,13 +610,11 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
602610
atomState.m.d.add(a)
603611
}
604612
}
605-
if (!isPendingPromise(atomState.v)) {
606-
for (const a of atomState.m.d || []) {
607-
if (!atomState.d.has(a)) {
608-
atomState.m.d.delete(a)
609-
const aMounted = unmountAtom(batch, a, ensureAtomState(a))
610-
aMounted?.t.delete(atom)
611-
}
613+
for (const a of atomState.m.d || []) {
614+
if (!atomState.d.has(a) && !atomState.m.q.has(a)) {
615+
atomState.m.d.delete(a)
616+
const aMounted = unmountAtom(batch, a, ensureAtomState(a))
617+
aMounted?.t.delete(atom)
612618
}
613619
}
614620
}
@@ -631,6 +637,7 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
631637
atomState.m = {
632638
l: new Set(),
633639
d: new Set(atomState.d.keys()),
640+
q: new Set(),
634641
t: new Set(),
635642
}
636643
atomState.h?.(batch)

tests/vanilla/dependency.test.tsx

+86
Original file line numberDiff line numberDiff line change
@@ -425,3 +425,89 @@ it('batches sync writes', () => {
425425
expect(fetch).toBeCalledWith(1)
426426
expect(store.get(a)).toBe(1)
427427
})
428+
429+
it('mounts and unmounts sync and async dependencies correctly', async () => {
430+
const mounted = [0, 0, 0, 0, 0] as [number, number, number, number, number]
431+
const a = atom(0)
432+
a.onMount = () => {
433+
++mounted[0]
434+
return () => {
435+
--mounted[0]
436+
}
437+
}
438+
439+
const b = atom(0)
440+
b.onMount = () => {
441+
++mounted[1]
442+
return () => {
443+
--mounted[1]
444+
}
445+
}
446+
447+
const c = atom(0)
448+
c.onMount = () => {
449+
++mounted[2]
450+
return () => {
451+
--mounted[2]
452+
}
453+
}
454+
455+
const d = atom(0)
456+
d.onMount = () => {
457+
++mounted[3]
458+
return () => {
459+
--mounted[3]
460+
}
461+
}
462+
463+
const e = atom(0)
464+
e.onMount = () => {
465+
++mounted[4]
466+
return () => {
467+
--mounted[4]
468+
}
469+
}
470+
471+
let resolve: (() => Promise<void>) | undefined
472+
const f = atom((get) => {
473+
if (!get(a)) {
474+
get(b)
475+
} else {
476+
get(c)
477+
}
478+
const promise = new Promise<void>((r) => {
479+
resolve = () => {
480+
r()
481+
return promise
482+
}
483+
}).then(() => {
484+
if (!get(a)) {
485+
get(d)
486+
} else {
487+
get(e)
488+
}
489+
})
490+
return promise
491+
})
492+
493+
const store = createStore()
494+
// mount a, b synchronously
495+
const unsub = store.sub(f, () => {})
496+
expect(mounted).toEqual([1, 1, 0, 0, 0])
497+
498+
// mount d asynchronously
499+
await resolve!()
500+
expect(mounted).toEqual([1, 1, 0, 1, 0])
501+
502+
// unmount b, mount c synchronously
503+
store.set(a, 1)
504+
expect(mounted).toEqual([1, 0, 1, 1, 0])
505+
506+
// unmount d, mount e asynchronously
507+
await resolve!()
508+
expect(mounted).toEqual([1, 0, 1, 0, 1])
509+
510+
// unmount a, b, d, e synchronously
511+
unsub()
512+
expect(mounted).toEqual([0, 0, 0, 0, 0])
513+
})

0 commit comments

Comments
 (0)