@@ -163,6 +163,8 @@ const addDependency = <Value>(
163163// Batch
164164//
165165
166+ type BatchPriority = 'H' | 'M' | 'L'
167+
166168type Batch = Readonly < {
167169 /** Atom dependents map */
168170 D : Map < AnyAtom , Set < AnyAtom > >
@@ -181,16 +183,12 @@ const createBatch = (): Batch => ({
181183 L : new Set ( ) ,
182184} )
183185
184- const addBatchFuncHigh = ( batch : Batch , fn : ( ) => void ) => {
185- batch . H . add ( fn )
186- }
187-
188- const addBatchFuncMedium = ( batch : Batch , fn : ( ) => void ) => {
189- batch . M . add ( fn )
190- }
191-
192- const addBatchFuncLow = ( batch : Batch , fn : ( ) => void ) => {
193- batch . L . add ( fn )
186+ const addBatchFunc = (
187+ batch : Batch ,
188+ priority : BatchPriority ,
189+ fn : ( ) => void ,
190+ ) => {
191+ batch [ priority ] . add ( fn )
194192}
195193
196194const registerBatchAtom = (
@@ -200,8 +198,8 @@ const registerBatchAtom = (
200198) => {
201199 if ( ! batch . D . has ( atom ) ) {
202200 batch . D . set ( atom , new Set ( ) )
203- addBatchFuncMedium ( batch , ( ) => {
204- atomState . m ?. l . forEach ( ( listener ) => addBatchFuncMedium ( batch , listener ) )
201+ addBatchFunc ( batch , 'M' , ( ) => {
202+ atomState . m ?. l . forEach ( ( listener ) => addBatchFunc ( batch , 'M' , listener ) )
205203 } )
206204 }
207205}
@@ -514,7 +512,7 @@ const buildStore = (
514512
515513 // Step 2: use the topSortedReversed atom list to recompute all affected atoms
516514 // Track what's changed, so that we can short circuit when possible
517- addBatchFuncHigh ( batch , ( ) => {
515+ addBatchFunc ( batch , 'H' , ( ) => {
518516 const changedAtoms = new Set < AnyAtom > ( [ atom ] )
519517 for ( let i = topSortedReversed . length - 1 ; i >= 0 ; -- i ) {
520518 const [ a , aState , prevEpochNumber ] = topSortedReversed [ i ] !
@@ -659,7 +657,7 @@ const buildStore = (
659657 isSync = false
660658 }
661659 }
662- addBatchFuncLow ( batch , ( ) => {
660+ addBatchFunc ( batch , 'L' , ( ) => {
663661 const onUnmount = createInvocationContext ( batch , ( ) =>
664662 atomOnMount ( atom , ( ...args ) => setAtom ( ...args ) ) ,
665663 )
@@ -685,7 +683,7 @@ const buildStore = (
685683 // unmount self
686684 const onUnmount = atomState . m . u
687685 if ( onUnmount ) {
688- addBatchFuncLow ( batch , ( ) => onUnmount ( batch ) )
686+ addBatchFunc ( batch , 'L' , ( ) => onUnmount ( batch ) )
689687 }
690688 delete atomState . m
691689 if ( import . meta. env ?. MODE !== 'production' ) {
0 commit comments