Skip to content

Commit 30319d5

Browse files
authored
Retain an Effect atom's exposed success after asynchronous failure (#9607)
1 parent 683ed2c commit 30319d5

3 files changed

Lines changed: 83 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"effect": patch
3+
---
4+
5+
Fix Effect atoms dropping the latest exposed success when an asynchronous run fails, so an `initialValue` or a success published with `get.setSelf` remains available as the failure's previous success.

‎packages/effect/src/reactivity/Atom.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,13 @@ function makeEffect<A, E>(
580580
),
581581
effect,
582582
function(exit) {
583-
syncResult = AsyncResult.fromExitWithPrevious(exit, previous)
584583
if (isAsync) {
585-
ctx.setSelf(syncResult)
584+
// Read the live value rather than `previous`: while the effect was
585+
// running, the atom may have exposed `initialValue` or a `setSelf`
586+
// success, and a failure should retain the latest success shown.
587+
ctx.setSelf(AsyncResult.fromExitWithPrevious(exit, ctx.self()))
588+
} else {
589+
syncResult = AsyncResult.fromExitWithPrevious(exit, previous)
586590
}
587591
},
588592
uninterruptible

‎packages/effect/test/reactivity/Atom.test.ts‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)