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

[INTERNAL] Add batch to listeners #2887

Closed
wants to merge 2 commits into from
Closed
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
37 changes: 22 additions & 15 deletions src/vanilla/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ type Mounted = {
/** Set of mounted atoms that depends on the atom. */
readonly t: Set<AnyAtom>
/** Function to run when the atom is unmounted. */
u?: (batch: Batch) => void
u?: BatchListener
}

/**
Expand Down Expand Up @@ -163,17 +163,19 @@ const addDependency = <Value>(
// Batch
//

type BatchListener = (batch: Batch) => void

type BatchPriority = 'H' | 'M' | 'L'

type Batch = Readonly<{
/** Atom dependents map */
D: Map<AnyAtom, Set<AnyAtom>>
/** High priority functions */
H: Set<() => void>
H: Set<BatchListener>
/** Medium priority functions */
M: Set<() => void>
M: Set<BatchListener>
/** Low priority functions */
L: Set<() => void>
L: Set<BatchListener>
}>

const createBatch = (): Batch => ({
Expand All @@ -185,8 +187,8 @@ const createBatch = (): Batch => ({

const addBatchFunc = (
batch: Batch,
fn: BatchListener,
priority: BatchPriority,
fn: () => void,
) => {
batch[priority].add(fn)
}
Expand All @@ -198,9 +200,12 @@ const registerBatchAtom = (
) => {
if (!batch.D.has(atom)) {
batch.D.set(atom, new Set())
addBatchFunc(batch, 'M', () => {
atomState.m?.l.forEach((listener) => addBatchFunc(batch, 'M', listener))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to avoid this hack too.

})
const scheduleListeners = () => {
for (const listener of atomState.m?.l ?? []) {
addBatchFunc(batch, () => listener(), 'M')
}
}
addBatchFunc(batch, scheduleListeners, 'M')
}
}

Expand All @@ -221,9 +226,9 @@ const getBatchAtomDependents = (batch: Batch, atom: AnyAtom) =>
const flushBatch = (batch: Batch) => {
let error: AnyError
let hasError = false
const call = (fn: () => void) => {
const call = (fn: BatchListener) => {
try {
fn()
fn(batch)
} catch (e) {
if (!hasError) {
error = e
Expand Down Expand Up @@ -503,7 +508,7 @@ const buildStore = (

// Step 2: use the topSortedReversed atom list to recompute all affected atoms
// Track what's changed, so that we can short circuit when possible
addBatchFunc(batch, 'H', () => {
const finishRecompute = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though increasing bundle size, this is more readable.

const changedAtoms = new Set<AnyAtom>([atom])
for (let i = topSortedReversed.length - 1; i >= 0; --i) {
const [a, aState, prevEpochNumber] = topSortedReversed[i]!
Expand All @@ -524,7 +529,8 @@ const buildStore = (
}
delete aState.x
}
})
}
addBatchFunc(batch, finishRecompute, 'H')
}

const writeAtomState = <Value, Args extends unknown[], Result>(
Expand Down Expand Up @@ -645,14 +651,15 @@ const buildStore = (
isSync = false
}
}
addBatchFunc(batch, 'L', () => {
const processOnMount = () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too.

const onUnmount = createInvocationContext(batch, () =>
atomOnMount(atom, (...args) => setAtom(...args)),
)
if (onUnmount) {
mounted.u = (batch) => createInvocationContext(batch, onUnmount)
}
})
}
addBatchFunc(batch, processOnMount, 'L')
}
}
return atomState.m
Expand All @@ -671,7 +678,7 @@ const buildStore = (
// unmount self
const onUnmount = atomState.m.u
if (onUnmount) {
addBatchFunc(batch, 'L', () => onUnmount(batch))
addBatchFunc(batch, onUnmount, 'L')
}
delete atomState.m
// unmount dependencies
Expand Down
Loading