Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: eliminate batch #2925

Merged
merged 27 commits into from
Jan 14, 2025
Merged
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,15 @@ const addDependency = <Value>(
aState.m?.t.add(atom)
}

const clearThenForEach = <T>(
items: { values: () => Iterable<T>; clear: () => void },
callback: (item: T) => void,
) => {
const values = new Set(items.values())
items.clear()
values.forEach(callback)
}

// internal & unstable type
type StoreArgs = readonly [
getAtomState: <Value>(atom: Atom<Value>) => AtomState<Value> | undefined,
Expand Down Expand Up @@ -269,12 +278,11 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
recomputeInvalidatedAtoms()
}
;(store as any)[INTERNAL_flushStoreHook]?.()
changedAtoms.forEach((atomState) => atomState.m?.l.forEach(call))
changedAtoms.clear()
unmountCallbacks.forEach(call)
unmountCallbacks.clear()
mountCallbacks.forEach(call)
mountCallbacks.clear()
clearThenForEach(changedAtoms, (atomState) =>
atomState.m?.l.forEach(call),
)
clearThenForEach(unmountCallbacks, call)
clearThenForEach(mountCallbacks, call)
dai-shi marked this conversation as resolved.
Show resolved Hide resolved
} while (changedAtoms.size)
}
--inTransaction
Expand Down
Loading