@@ -697,6 +697,63 @@ describe("Atom", () => {
697697 expect ( r . get ( derived ) ) . toEqual ( "2b" )
698698 } )
699699
700+ it ( "retains method-form dependencies added during a batch rebuild" , async ( ) => {
701+ const registry = Registry . make ( )
702+ const source = Atom . make ( Option . none < string > ( ) )
703+ const gate = Effect . unsafeMakeLatch ( )
704+ const asyncAtom = Atom . make ( ( get ) =>
705+ Effect . gen ( function * ( ) {
706+ const value = get ( source )
707+ if ( Option . isNone ( value ) ) {
708+ return yield * Effect . fail ( "SourceIsNone" as const )
709+ }
710+ yield * gate . await
711+ return `computed-${ value . value } `
712+ } )
713+ )
714+ const derived = Atom . make ( ( get ) : unknown => {
715+ const value = get . get ( source )
716+ if ( Option . isNone ( value ) ) {
717+ return "empty"
718+ }
719+ return get . get ( asyncAtom )
720+ } )
721+
722+ registry . subscribe ( derived , ( ) => { } , { immediate : true } )
723+ registry . subscribe ( asyncAtom , ( ) => { } , { immediate : true } )
724+
725+ Atom . batch ( ( ) => registry . set ( source , Option . some ( "a" ) ) )
726+
727+ gate . unsafeOpen ( )
728+ await Effect . runPromise ( Effect . yieldNow ( ) )
729+
730+ const result = registry . get ( derived ) as Result . Result < string , "SourceIsNone" >
731+ assert ( Result . isSuccess ( result ) )
732+ assert . strictEqual ( result . value , "computed-a" )
733+ } )
734+
735+ it ( "rebuilds an atom invalidated during its own batch rebuild" , ( ) => {
736+ const registry = Registry . make ( )
737+ const source = Atom . make ( 0 )
738+ const enabled = Atom . make ( false )
739+ const updateSource = Atom . make ( ( get ) => {
740+ get . set ( source , 1 )
741+ } )
742+ const derived = Atom . make ( ( get ) => {
743+ const value = get ( source )
744+ if ( get ( enabled ) ) {
745+ get ( updateSource )
746+ }
747+ return value
748+ } )
749+
750+ registry . subscribe ( derived , ( ) => { } , { immediate : true } )
751+
752+ Atom . batch ( ( ) => registry . set ( enabled , true ) )
753+
754+ assert . strictEqual ( registry . get ( derived ) , 1 )
755+ } )
756+
700757 it ( "nested batch" , async ( ) => {
701758 const r = Registry . make ( )
702759 const state = Atom . make ( 1 ) . pipe ( Atom . keepAlive )
0 commit comments