@@ -1005,3 +1005,64 @@ 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+ console . log ( 'initAtom write val2Atoms' )
1039+ set ( val2Atoms . resetAtom , null )
1040+ console . log ( 'initAtom write val1Atoms' )
1041+ set ( val1Atoms . resetAtom , 'bar' )
1042+ } )
1043+ initAtom . debugLabel = 'initAtom'
1044+
1045+ const computedValAtom = atom ( ( get ) => {
1046+ const v2Value = get ( val2Atoms . valueAtom )
1047+ if ( v2Value !== null ) {
1048+ console . log ( 'computedValAtom read val2Atoms' , v2Value )
1049+ return v2Value
1050+ }
1051+ const v1Value = get ( val1Atoms . valueAtom )
1052+ console . log ( 'computedValAtom read val2Atoms' , v1Value )
1053+ return v1Value
1054+ } )
1055+ computedValAtom . debugLabel = 'computedValAtom'
1056+
1057+ const asyncInitAtom = atom ( null , async ( _get , set ) => {
1058+ // if comment out this line, the test will pass [DOES NOT WORK]
1059+ await new Promise ( ( resolve ) => setTimeout ( resolve , 0 ) )
1060+
1061+ set ( initAtom )
1062+ } )
1063+ store . sub ( computedValAtom , ( ) => { } )
1064+ console . log ( 'set asyncInitAtom' )
1065+ await store . set ( asyncInitAtom )
1066+ const result = store . get ( computedValAtom )
1067+ expect ( result ) . toBe ( 'bar' )
1068+ } )
0 commit comments