Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/vanilla/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ function getMountedOrPendingDependents(
): Set<AnyAtom> {
const dependents = new Set<AnyAtom>()
for (const a of mountedMap.get(atom)?.t || []) {
if (mountedMap.has(a)) {
dependents.add(a)
}
dependents.add(a)
}
for (const atomWithPendingPromise of atomState.p) {
dependents.add(atomWithPendingPromise)
Expand Down Expand Up @@ -549,14 +547,14 @@ const readAtomState: ReadAtomState = (store, atom) => {
}
// Otherwise, check if the dependencies have changed.
// If all dependencies haven't changed, we can use the cache.
if (
Array.from(atomState.d).every(
([a, n]) =>
// Recursively, read the atom state of the dependency, and
// check if the atom epoch number is unchanged
readAtomState(store, a).n === n,
)
) {
let hasChangedDeps = false
for (const [a, n] of atomState.d) {
if (readAtomState(store, a).n !== n) {
hasChangedDeps = true
break
}
}
if (!hasChangedDeps) {
return atomState
}
}
Expand Down Expand Up @@ -592,7 +590,9 @@ const readAtomState: ReadAtomState = (store, atom) => {
if (isPendingPromise(atomState.v)) {
addPendingPromiseToDependency(atom, atomState.v, aState)
}
mountedMap.get(a)?.t.add(atom)
if (mountedMap.has(atom)) {
mountedMap.get(a)?.t.add(atom)
}
if (!isSync) {
mountDependenciesIfAsync()
}
Expand Down Expand Up @@ -765,7 +765,7 @@ const mountDependencies: MountDependencies = (store, atom) => {
}
}
}
for (const a of mounted.d || []) {
for (const a of mounted.d) {
if (!atomState.d.has(a)) {
mounted.d.delete(a)
const aMounted = unmountAtom(store, a)
Expand Down Expand Up @@ -848,11 +848,17 @@ const unmountAtom: UnmountAtom = (store, atom) => {
const unmountAtom = buildingBlocks[19]
const atomState = ensureAtomState(store, atom)
let mounted = mountedMap.get(atom)
if (
mounted &&
!mounted.l.size &&
!Array.from(mounted.t).some((a) => mountedMap.get(a)?.d.has(atom))
) {
if (!mounted || mounted.l.size) {
return mounted
}
let isDependent = false
for (const a of mounted.t) {
if (mountedMap.get(a)?.d.has(atom)) {
isDependent = true
break
}
}
if (!isDependent) {
// unmount self
if (mounted.u) {
unmountCallbacks.add(mounted.u)
Expand Down
Loading