Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Nov 18, 2024
1 parent 6a6b747 commit e9c3d5c
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ type Pending = readonly [
dependents: Map<AnyAtom, Set<AnyAtom>>,
atomStates: Map<AnyAtom, AtomState>,
functions: Set<() => void>,
// atoms read by readAtomState
// atoms written by writeAtomState
// 1. an atom is written to by writeAtomState
// 2. mark the atom as "dirty"
// 3. store the dependents of the dirty atom
// 4.
]

const createPending = (): Pending => [new Map(), new Map(), new Set()]
Expand Down Expand Up @@ -439,6 +445,28 @@ const buildStore = (
return dependents
}

function getAllDependents(

Check warning on line 448 in src/vanilla/store.ts

View workflow job for this annotation

GitHub Actions / lint

'getAllDependents' is defined but never used. Allowed unused vars must match /^_/u
pending: Pending,
atom: AnyAtom,
atomState: AtomState,
): Set<AnyAtom> {
const visited = new Set<AnyAtom>()
const stack: [AnyAtom, AtomState][] = [[atom, atomState]]
while (stack.length > 0) {
const [a, aState] = stack.pop()!
if (visited.has(a)) {
continue
}
visited.add(a)
for (const [d, dState] of getDependents(pending, a, aState)) {
if (!visited.has(d)) {
stack.push([d, dState])
}
}
}
return visited
}

// This is a topological sort via depth-first search, slightly modified from
// what's described here for simplicity and performance reasons:
// https://en.wikipedia.org/wiki/Topological_sorting#Depth-first_search
Expand Down Expand Up @@ -525,8 +553,9 @@ const buildStore = (
...args: Args
): Result => {
let isSync = true
const getter: Getter = <V>(a: Atom<V>) =>
returnAtomValue(readAtomState(pending, a))
const getter: Getter = <V>(a: Atom<V>) => {
return returnAtomValue(readAtomState(pending, a))
}
const setter: Setter = <V, As extends unknown[], R>(
a: WritableAtom<V, As, R>,
...args: As
Expand Down

0 comments on commit e9c3d5c

Please sign in to comment.