Skip to content

Commit

Permalink
refactor with onMount queue
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Aug 31, 2023
1 parent bcc0a93 commit 4aa774d
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,22 +570,23 @@ export const createStore = () => {

const mountAtom = <Value>(
atom: Atom<Value>,
initialDependent?: AnyAtom
initialDependent?: AnyAtom,
onMountQueue?: (() => void)[]
): Mounted => {
const prevState = getAtomState(atom)
const queue = onMountQueue || []
// mount dependencies before mounting self
prevState?.d.forEach((_, a) => {
getAtomState(atom)?.d.forEach((_, a) => {
const aMounted = mountedMap.get(a)
if (aMounted) {
aMounted.t.add(atom) // add dependent
} else {
if (a !== atom) {
mountAtom(a, atom)
mountAtom(a, atom, queue)
}
}
})
// recompute atom state
const currentState = readAtomState(atom)
readAtomState(atom)
// mount self
const mounted: Mounted = {
t: new Set(initialDependent && [initialDependent]),
Expand All @@ -595,21 +596,18 @@ export const createStore = () => {
if (import.meta.env?.MODE !== 'production') {
mountedAtoms.add(atom)
}
// update `initialDependent` if the value has changed during mounting
if (
initialDependent &&
prevState &&
currentState !== prevState &&
!isEqualAtomValue(prevState, currentState)
) {
readAtomState(initialDependent, true)
}
// onMount
if (isActuallyWritableAtom(atom) && atom.onMount) {
const onUnmount = atom.onMount((...args) => writeAtom(atom, ...args))
if (onUnmount) {
mounted.u = onUnmount
}
const { onMount } = atom
queue.push(() => {
const onUnmount = onMount((...args) => writeAtom(atom, ...args))
if (onUnmount) {
mounted.u = onUnmount
}
})
}
if (!onMountQueue) {
queue.forEach((f) => f())
}
return mounted
}
Expand Down

0 comments on commit 4aa774d

Please sign in to comment.