@@ -560,6 +560,78 @@ describe("Atom", { concurrent: false }, () => {
560560 expect ( result . value ) . toEqual ( 1 )
561561 } )
562562
563+ for ( const form of [ "effect" , "function" , "runtime" ] as const ) {
564+ it . effect ( `effect initial value survives asynchronous failure (${ form } )` , ( ) =>
565+ Effect . gen ( function * ( ) {
566+ const release = yield * Latch . make ( )
567+ const fail = release . await . pipe ( Effect . andThen ( Effect . fail ( "boom" ) ) )
568+ const count = form === "effect"
569+ ? Atom . make ( fail , { initialValue : 0 } )
570+ : form === "function"
571+ ? Atom . make ( ( ) => fail , { initialValue : 0 } )
572+ : Atom . runtime ( Layer . empty ) . atom ( fail , { initialValue : 0 } )
573+ const r = AtomRegistry . make ( )
574+ try {
575+ r . mount ( count )
576+ const waiting = r . get ( count )
577+ assert ( AsyncResult . isSuccess ( waiting ) )
578+ assert . strictEqual ( waiting . value , 0 )
579+ assert . strictEqual ( waiting . waiting , true )
580+
581+ yield * release . open
582+ yield * Effect . yieldNow
583+ const failure = r . get ( count )
584+ assert ( AsyncResult . isFailure ( failure ) )
585+ assert . strictEqual ( failure . waiting , false )
586+ assert . deepStrictEqual ( Cause . findErrorOption ( failure . cause ) , Option . some ( "boom" ) )
587+ assert . deepStrictEqual ( AsyncResult . value ( failure ) , Option . some ( 0 ) )
588+ } finally {
589+ r . dispose ( )
590+ }
591+ } ) )
592+ }
593+
594+ it . effect ( "effect failure retains the latest success published with setSelf" , ( ) =>
595+ Effect . gen ( function * ( ) {
596+ const publish = yield * Latch . make ( )
597+ const release = yield * Latch . make ( )
598+ const count = Atom . make ( ( get ) =>
599+ Effect . gen ( function * ( ) {
600+ yield * publish . await
601+ get . setSelf ( AsyncResult . success ( 1 ) )
602+ yield * release . await
603+ return yield * Effect . fail ( "boom" )
604+ } ) , { initialValue : 0 } )
605+ const r = AtomRegistry . make ( )
606+ try {
607+ r . mount ( count )
608+ assert . deepStrictEqual ( AsyncResult . value ( r . get ( count ) ) , Option . some ( 0 ) )
609+ yield * publish . open
610+ yield * Effect . yieldNow
611+ assert . deepStrictEqual ( AsyncResult . value ( r . get ( count ) ) , Option . some ( 1 ) )
612+
613+ yield * release . open
614+ yield * Effect . yieldNow
615+ const failure = r . get ( count )
616+ assert ( AsyncResult . isFailure ( failure ) )
617+ assert . deepStrictEqual ( AsyncResult . value ( failure ) , Option . some ( 1 ) )
618+ } finally {
619+ r . dispose ( )
620+ }
621+ } ) )
622+
623+ it ( "effect synchronous failure does not retain an unexposed initial value" , ( ) => {
624+ const count = Atom . make ( Effect . fail ( "boom" ) , { initialValue : 0 } )
625+ const r = AtomRegistry . make ( )
626+ try {
627+ const failure = r . get ( count )
628+ assert ( AsyncResult . isFailure ( failure ) )
629+ assert . deepStrictEqual ( AsyncResult . value ( failure ) , Option . none ( ) )
630+ } finally {
631+ r . dispose ( )
632+ }
633+ } )
634+
563635 it ( "effectFn" , async ( ) => {
564636 const count = Atom . fn ( ( n : number ) => Effect . succeed ( n + 1 ) )
565637 const r = AtomRegistry . make ( )
0 commit comments