Skip to content

Commit

Permalink
refactor readAtomState
Browse files Browse the repository at this point in the history
  • Loading branch information
dai-shi committed Jan 11, 2025
1 parent 417532e commit 9827d96
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,12 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
// Compute a new state for this atom.
atomState.d.clear()
let isSync = true
const mountDependenciesIfAsync = () => {
if (atomState.m) {
mountDependencies(atom, atomState)
flushCallbacks()
}
}
const getter: Getter = <V>(a: Atom<V>) => {
if (isSelfAtom(atom, a)) {
const aState = ensureAtomState(a)
Expand All @@ -350,8 +356,7 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
} finally {
addDependency(atom, atomState, a, aState)
if (!isSync) {
mountDependencies(atom, atomState)
flushCallbacks()
mountDependenciesIfAsync()
}
}
}
Expand Down Expand Up @@ -389,13 +394,7 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
setAtomStateValueOrPromise(atom, atomState, valueOrPromise)
if (isPromiseLike(valueOrPromise)) {
valueOrPromise.onCancel?.(() => controller?.abort())
const complete = () => {
if (atomState.m) {
mountDependencies(atom, atomState)
flushCallbacks()
}
}
valueOrPromise.then(complete, complete)
valueOrPromise.then(mountDependenciesIfAsync, mountDependenciesIfAsync)
}
return atomState
} catch (error) {
Expand Down

0 comments on commit 9827d96

Please sign in to comment.