Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 22 additions & 10 deletions src/vanilla/internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ type ReadAtomState = <Value>(
store: Store,
atom: Atom<Value>,
) => AtomState<Value>
type InvalidateDependents = (store: Store, atom: AnyAtom) => void
type InvalidateDependents = (store: Store, atoms: Iterable<AnyAtom>) => void
type WriteAtomState = <Value, Args extends unknown[], Result>(
store: Store,
atom: WritableAtom<Value, Args, Result>,
Expand Down Expand Up @@ -690,21 +690,27 @@ const BUILDING_BLOCK_readAtomState: ReadAtomState = (store, atom) => {

const BUILDING_BLOCK_invalidateDependents: InvalidateDependents = (
store,
atom,
atoms,
) => {
const buildingBlocks = getInternalBuildingBlocks(store)
const mountedMap = buildingBlocks[1]
const invalidatedAtoms = buildingBlocks[2]
const ensureAtomState = buildingBlocks[11]
const stack: AnyAtom[] = [atom]
while (stack.length) {
const a = stack.pop()!
const aState = ensureAtomState(store, a)
const atomStack: AnyAtom[] = []
const stateStack: AtomState[] = []
for (const atom of atoms) {
atomStack.push(atom)
stateStack.push(ensureAtomState(store, atom))
}
while (atomStack.length) {
const a = atomStack.pop()!
const aState = stateStack.pop()!
for (const d of getMountedOrPendingDependents(a, aState, mountedMap)) {
const dState = ensureAtomState(store, d)
if (invalidatedAtoms.get(d) !== dState.n) {
invalidatedAtoms.set(d, dState.n)
stack.push(d)
atomStack.push(d)
stateStack.push(dState)
}
}
}
Expand Down Expand Up @@ -752,7 +758,7 @@ const BUILDING_BLOCK_writeAtomState: WriteAtomState = (
if (prevEpochNumber !== aState.n) {
++storeEpochHolder[0]
changedAtoms.add(a)
invalidateDependents(store, a)
invalidateDependents(store, [a])
storeHooks.c?.(a)
}
return undefined as R
Expand Down Expand Up @@ -785,6 +791,7 @@ const BUILDING_BLOCK_mountDependencies: MountDependencies = (store, atom) => {
const atomState = ensureAtomState(store, atom)
const mounted = mountedMap.get(atom)
if (mounted) {
const staleDeps = new Set<AnyAtom>()
for (const [a, n] of atomState.d) {
if (!mounted.d.has(a)) {
const aState = ensureAtomState(store, a)
Expand All @@ -793,11 +800,16 @@ const BUILDING_BLOCK_mountDependencies: MountDependencies = (store, atom) => {
mounted.d.add(a)
if (n !== aState.n) {
changedAtoms.add(a)
Comment thread
dmaskasky marked this conversation as resolved.
invalidateDependents(store, a)
storeHooks.c?.(a)
staleDeps.add(a)
}
}
}
if (staleDeps.size) {
invalidateDependents(store, staleDeps)
for (const a of staleDeps) {
storeHooks.c?.(a)
}
}
for (const a of mounted.d) {
if (!atomState.d.has(a)) {
mounted.d.delete(a)
Expand Down
2 changes: 1 addition & 1 deletion tests/vanilla/internals.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ describe('internals', () => {

const unsub = store.sub(leafAtom, () => {})
const invalidateDependents = INTERNAL_getBuildingBlocks(store)[15]
expect(() => invalidateDependents(store, baseAtom)).not.toThrow()
expect(() => invalidateDependents(store, [baseAtom])).not.toThrow()
unsub()
})
})
Expand Down
Loading