Skip to content

Commit

Permalink
pair processOnChangeHandlers with flushPending
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Nov 10, 2024
1 parent 3ca52e3 commit 4d787ed
Showing 1 changed file with 15 additions and 25 deletions.
40 changes: 15 additions & 25 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,18 +217,6 @@ const addUnmountedAtom = (pending: Pending, atom: AnyAtom) => {
pending[3][2].add(atom)
}

const flushPending = (pending: Pending) => {
while (pending[1].size || pending[2].size) {
pending[0].clear()
const atomStates = new Set(pending[1].values())
pending[1].clear()
const functions = new Set(pending[2])
pending[2].clear()
atomStates.forEach((atomState) => atomState.m?.l.forEach((l) => l()))
functions.forEach((fn) => fn())
}
}

type GetAtomState = <Value>(
atom: Atom<Value>,
originAtomState?: AtomState,
Expand Down Expand Up @@ -368,7 +356,6 @@ const buildStore = (getAtomState: StoreArgs[0]): Store => {
addDependency(pending, atom, atomState, a, aState)
mountDependencies(pending, atom, atomState)
flushPending(pending)
processOnChangeHandlers(pending)
}
return returnAtomValue(aState)
}
Expand Down Expand Up @@ -411,7 +398,6 @@ const buildStore = (getAtomState: StoreArgs[0]): Store => {
const pending = createPending()
mountDependencies(pending, atom, atomState)
flushPending(pending)
processOnChangeHandlers(pending)
}
}
valueOrPromise.then(complete, complete)
Expand Down Expand Up @@ -542,10 +528,7 @@ const buildStore = (getAtomState: StoreArgs[0]): Store => {
} else {
r = writeAtomState(pending, a, aState, ...args) as R
}
flushPending(pending)
if (!isSync) {
processOnChangeHandlers(pending)
}
flushPending(pending, !isSync)
return r as R
}
try {
Expand All @@ -562,7 +545,6 @@ const buildStore = (getAtomState: StoreArgs[0]): Store => {
const pending = createPending()
const result = writeAtomState(pending, atom, getAtomState(atom), ...args)
flushPending(pending)
processOnChangeHandlers(pending)
return result
}

Expand Down Expand Up @@ -665,15 +647,13 @@ const buildStore = (getAtomState: StoreArgs[0]): Store => {
const atomState = getAtomState(atom)
const mounted = mountAtom(pending, atom, atomState)
flushPending(pending)
processOnChangeHandlers(pending)
const listeners = mounted.l
listeners.add(listener)
return () => {
listeners.delete(listener)
const pending = createPending()
unmountAtom(pending, atom, atomState)
flushPending(pending)
processOnChangeHandlers(pending)
}
}

Expand All @@ -689,11 +669,22 @@ const buildStore = (getAtomState: StoreArgs[0]): Store => {
}
}

const processOnChangeHandlers = (pending: Pending) => {
const flushPending = (
pending: Pending,
shouldProcessOnChange: boolean = true,
) => {
do {
flushPending(pending)
while (pending[1].size || pending[2].size) {
pending[0].clear()
const atomStates = new Set(pending[1].values())
pending[1].clear()
const functions = new Set(pending[2])
pending[2].clear()
atomStates.forEach((atomState) => atomState.m?.l.forEach((l) => l()))
functions.forEach((fn) => fn())
}
// Process finalizers after all atoms are updated
if (pending[3].some((s) => s.size)) {
if (shouldProcessOnChange && pending[3].some((s) => s.size)) {
for (const handler of onChangeHandlers) {
handler(...pending[3])
}
Expand Down Expand Up @@ -739,7 +730,6 @@ const buildStore = (getAtomState: StoreArgs[0]): Store => {
}
}
flushPending(pending)
processOnChangeHandlers(pending)
},
}
Object.assign(store, devStore)
Expand Down

0 comments on commit 4d787ed

Please sign in to comment.