Skip to content

Commit

Permalink
encapsulate isSync inside setAtom
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Nov 15, 2024
1 parent 1d44255 commit 116bb80
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,8 +601,7 @@ const buildStore = (
if (isActuallyWritableAtom(atom)) {
const mounted = atomState.m
addPendingFunction(pending, () => {
let isSync = true
const createSetAtom = (pending: Pending) => {
const createSetAtom = (pending: Pending, isSync = true) => {
return (...args: unknown[]) => {
try {
return writeAtomState(pending, atom, ...args)
Expand All @@ -613,22 +612,22 @@ const buildStore = (
}
}
}
let onUnmount: OnUnmount | void
let setAtom = createSetAtom(pending)
try {
const onUnmount = atomOnMount(atom, (...args) => setAtom(...args))
if (onUnmount) {
mounted.u = (pending) => {
setAtom = createSetAtom(pending)
isSync = true
try {
onUnmount()
} finally {
isSync = false
}
onUnmount = atomOnMount(atom, (...args) => setAtom(...args))
} finally {
setAtom = createSetAtom(pending, false)
}
if (typeof onUnmount === 'function') {
mounted.u = (pending) => {
setAtom = createSetAtom(pending)
try {
onUnmount()
} finally {
setAtom = createSetAtom(pending, false)
}
}
} finally {
isSync = false
}
})
}
Expand Down

0 comments on commit 116bb80

Please sign in to comment.