Skip to content

Commit

Permalink
add back high, medium, low batch channels
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaskasky committed Jan 2, 2025
1 parent adfcddf commit 14eb63f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,29 @@ const addDependency = <Value>(
// Batch
//

type BatchPriority = 0 | 1 | 2
type BatchPriority = 'H' | 'M' | 'L'

type Batch = [
/** Batch channels */
recomputeDependents: Set<() => void>,
atomListeners: Set<() => void>,
atomMountHooks: Set<() => void>,
] & {
/** High priority function */
H: Set<() => void>
/** Medium priority function */
M: Set<() => void>
/** Low priority function */
L: Set<() => void>
/** Atom dependents map */
D: Map<AnyAtom, Set<AnyAtom>>
}

const createBatch = (): Batch =>
Object.assign(Array(3).fill(new Set()), { D: new Map() }) as Batch
const createBatch = (): Batch => {
const batch = Array(3).fill(new Set()) as Batch
const [H, M, L] = batch
return Object.assign(batch, { H, M, L, D: new Map() })
}

const addBatchFunc = (
batch: Batch,
Expand All @@ -193,9 +203,9 @@ const registerBatchAtom = (
if (!batch.D.has(atom)) {
batch.D.set(atom, new Set())
const scheduleListeners = () => {
atomState.m?.l.forEach((listener) => addBatchFunc(batch, 1, listener))
atomState.m?.l.forEach((listener) => addBatchFunc(batch, 'H', listener))
}
addBatchFunc(batch, 1, scheduleListeners)
addBatchFunc(batch, 'M', scheduleListeners)
}
}

Expand Down Expand Up @@ -518,7 +528,7 @@ const buildStore = (
delete aState.x
}
}
addBatchFunc(batch, 0, finishRecompute)
addBatchFunc(batch, 'H', finishRecompute)
}

const writeAtomState = <Value, Args extends unknown[], Result>(
Expand Down Expand Up @@ -647,7 +657,7 @@ const buildStore = (
mounted.u = (batch) => createInvocationContext(batch, onUnmount)
}
}
addBatchFunc(batch, 2, processOnMount)
addBatchFunc(batch, 'L', processOnMount)
}
}
return atomState.m
Expand All @@ -666,7 +676,7 @@ const buildStore = (
// unmount self
const onUnmount = atomState.m.u
if (onUnmount) {
addBatchFunc(batch, 2, () => onUnmount(batch))
addBatchFunc(batch, 'L', () => onUnmount(batch))
}
delete atomState.m
// unmount dependencies
Expand Down

0 comments on commit 14eb63f

Please sign in to comment.