@@ -1005,3 +1005,56 @@ it('should process all atom listeners even if some of them throw errors', () =>
10051005 expect ( listenerB ) . toHaveBeenCalledTimes ( 1 )
10061006 expect ( listenerC ) . toHaveBeenCalledTimes ( 1 )
10071007} )
1008+
1009+ it . only ( 'runs recomputeDependents on atoms in the correct order' , async ( ) => {
1010+ const store = createStore ( ) . unstable_derive ( ( getAtomState , ...storeArgs ) => [
1011+ ( a ) => Object . assign ( getAtomState ( a ) , { label : a . debugLabel } ) ,
1012+ ...storeArgs ,
1013+ ] )
1014+ let i = 0
1015+ function createHistoryAtoms < T > ( initialValue : T ) {
1016+ const historyStackAtom = atom < T [ ] > ( [ initialValue ] )
1017+ historyStackAtom . debugLabel = `${ i } :historyStackAtom`
1018+ const historyIndexAtom = atom ( 0 )
1019+ historyIndexAtom . debugLabel = `${ i } :historyIndexAtom`
1020+ const valueAtom = atom (
1021+ ( get ) => get ( historyStackAtom ) [ get ( historyIndexAtom ) ] ! ,
1022+ )
1023+ valueAtom . debugLabel = `${ i } :valueAtom`
1024+ const resetAtom = atom ( null , ( _ , set , value : T ) => {
1025+ set ( historyStackAtom , [ value ] )
1026+ set ( historyIndexAtom , 0 )
1027+ } )
1028+ resetAtom . debugLabel = `${ i } :resetAtom`
1029+ i ++
1030+ return { valueAtom, resetAtom }
1031+ }
1032+
1033+ const val1Atoms = createHistoryAtoms ( 'foo' )
1034+ const val2Atoms = createHistoryAtoms < string | null > ( null )
1035+
1036+ const initAtom = atom ( null , ( _get , set ) => {
1037+ // if comment out this line, the test will pass
1038+ set ( val2Atoms . resetAtom , null )
1039+
1040+ set ( val1Atoms . resetAtom , 'bar' )
1041+ } )
1042+ initAtom . debugLabel = 'initAtom'
1043+
1044+ const computedValAtom = atom ( ( get ) => {
1045+ const val = get ( val2Atoms . valueAtom ) ?? get ( val1Atoms . valueAtom )
1046+ return val
1047+ } )
1048+ computedValAtom . debugLabel = 'computedValAtom'
1049+
1050+ const asyncInitAtom = atom ( null , async ( _get , set ) => {
1051+ // if comment out this line, the test will pass [DOES NOT WORK]
1052+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
1053+
1054+ set ( initAtom )
1055+ } )
1056+ store . sub ( computedValAtom , ( ) => { } )
1057+ await store . set ( asyncInitAtom )
1058+ const result = store . get ( computedValAtom )
1059+ expect ( result ) . toBe ( 'bar' )
1060+ } )
0 commit comments