Skip to content

Commit

Permalink
ideating with overwritable setAtom pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Nov 14, 2024
1 parent 2e41018 commit aa9cdb2
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,22 +600,25 @@ const buildStore = (
}
if (isActuallyWritableAtom(atom)) {
const mounted = atomState.m
let isSync = true
const createSetAtom = (pending: Pending) => {
return (...args: unknown[]) => {
try {
return writeAtomState(pending, atom, ...args)
} finally {
if (!isSync) {
flushPending(pending)
}
}
}
}
addPendingFunction(pending, () => {
let isSync = true
let currPending = pending
let setAtom = createSetAtom(pending)
try {
const onUnmount = atomOnMount(atom, (...args) => {
try {
return writeAtomState(currPending, atom, ...args)
} finally {
if (!isSync) {
flushPending(currPending)
}
}
})
const onUnmount = atomOnMount(atom, (...args) => setAtom(...args))
if (onUnmount) {
mounted.u = (pending) => {
currPending = pending
setAtom = createSetAtom(pending)
isSync = true
try {
onUnmount()
Expand Down

0 comments on commit aa9cdb2

Please sign in to comment.