-
-
Notifications
You must be signed in to change notification settings - Fork 638
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
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
} | ||
|
||
/** | ||
|
@@ -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 => ({ | ||
|
@@ -185,8 +187,8 @@ const createBatch = (): Batch => ({ | |
|
||
const addBatchFunc = ( | ||
batch: Batch, | ||
fn: BatchListener, | ||
priority: BatchPriority, | ||
fn: () => void, | ||
) => { | ||
batch[priority].add(fn) | ||
} | ||
|
@@ -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)) | ||
}) | ||
const scheduleListeners = () => { | ||
for (const listener of atomState.m?.l ?? []) { | ||
addBatchFunc(batch, () => listener(), 'M') | ||
} | ||
} | ||
addBatchFunc(batch, scheduleListeners, 'M') | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
|
@@ -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 = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]! | ||
|
@@ -524,7 +529,8 @@ const buildStore = ( | |
} | ||
delete aState.x | ||
} | ||
}) | ||
} | ||
addBatchFunc(batch, finishRecompute, 'H') | ||
} | ||
|
||
const writeAtomState = <Value, Args extends unknown[], Result>( | ||
|
@@ -645,14 +651,15 @@ const buildStore = ( | |
isSync = false | ||
} | ||
} | ||
addBatchFunc(batch, 'L', () => { | ||
const processOnMount = () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.