@@ -74,6 +74,8 @@ type Mounted = {
74
74
readonly l : Set < ( ) => void >
75
75
/** Set of mounted atoms that the atom depends on. */
76
76
readonly d : Set < AnyAtom >
77
+ /** Set of dependencies added asynchronously */
78
+ readonly q : Set < AnyAtom >
77
79
/** Set of mounted atoms that depends on the atom. */
78
80
readonly t : Set < AnyAtom >
79
81
/** Function to run when the atom is unmounted. */
@@ -310,10 +312,8 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
310
312
for ( const a of atomState . d . keys ( ) ) {
311
313
addPendingPromiseToDependency ( atom , valueOrPromise , ensureAtomState ( a ) )
312
314
}
313
- atomState . v = valueOrPromise
314
- } else {
315
- atomState . v = valueOrPromise
316
315
}
316
+ atomState . v = valueOrPromise
317
317
delete atomState . e
318
318
delete atomState . x
319
319
if ( ! hasPrevValue || ! Object . is ( prevValue , atomState . v ) ) {
@@ -375,6 +375,9 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
375
375
addDependency ( atom , atomState , a , aState )
376
376
} else {
377
377
const batch = createBatch ( )
378
+ if ( isPendingPromise ( atomState . v ) && ! atomState . m ?. d . has ( a ) ) {
379
+ atomState . m ?. q . add ( a )
380
+ }
378
381
addDependency ( atom , atomState , a , aState )
379
382
mountDependencies ( batch , atom , atomState )
380
383
flushBatch ( batch )
@@ -414,6 +417,11 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
414
417
const valueOrPromise = atomRead ( atom , getter , options as never )
415
418
setAtomStateValueOrPromise ( atom , atomState , valueOrPromise )
416
419
if ( isPromiseLike ( valueOrPromise ) ) {
420
+ if ( batch ) {
421
+ addBatchFunc ( batch , 0 , ( ) => atomState . m ?. q . clear ( ) )
422
+ } else {
423
+ atomState . m ?. q . clear ( )
424
+ }
417
425
valueOrPromise . onCancel ?.( ( ) => controller ?. abort ( ) )
418
426
const complete = ( ) => {
419
427
if ( atomState . m ) {
@@ -602,13 +610,11 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
602
610
atomState . m . d . add ( a )
603
611
}
604
612
}
605
- if ( ! isPendingPromise ( atomState . v ) ) {
606
- for ( const a of atomState . m . d || [ ] ) {
607
- if ( ! atomState . d . has ( a ) ) {
608
- atomState . m . d . delete ( a )
609
- const aMounted = unmountAtom ( batch , a , ensureAtomState ( a ) )
610
- aMounted ?. t . delete ( atom )
611
- }
613
+ for ( const a of atomState . m . d || [ ] ) {
614
+ if ( ! atomState . d . has ( a ) && ! atomState . m . q . has ( a ) ) {
615
+ atomState . m . d . delete ( a )
616
+ const aMounted = unmountAtom ( batch , a , ensureAtomState ( a ) )
617
+ aMounted ?. t . delete ( atom )
612
618
}
613
619
}
614
620
}
@@ -631,6 +637,7 @@ const buildStore = (...storeArgs: StoreArgs): Store => {
631
637
atomState . m = {
632
638
l : new Set ( ) ,
633
639
d : new Set ( atomState . d . keys ( ) ) ,
640
+ q : new Set ( ) ,
634
641
t : new Set ( ) ,
635
642
}
636
643
atomState . h ?.( batch )
0 commit comments