Skip to content

Commit 84fd4f7

Browse files
authored
pass suspendOnWaiting options to resultOnce in fn context (#384)
1 parent b33e608 commit 84fd4f7

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect-atom/atom": patch
3+
---
4+
5+
pass suspendOnWaiting options to resultOnce in Atom.fn context

packages/atom/src/internal/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ const LifetimeProto: Omit<Lifetime<any>, "node" | "finalizers" | "disposed" | "i
538538
if (this.disposed) {
539539
throw disposedError(this.node.atom)
540540
} else if (this.isFn) {
541-
return this.resultOnce(atom)
541+
return this.resultOnce(atom, options)
542542
}
543543
const result = this.get(atom)
544544
if (options?.suspendOnWaiting && result.waiting) {

packages/atom/test/Atom.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,6 +1422,57 @@ describe("Atom", () => {
14221422

14231423
unmount2()
14241424
})
1425+
1426+
it("get.result suspendOnWaiting", async () => {
1427+
const r = Registry.make()
1428+
1429+
const inner = Atom.make(Effect.succeed(1).pipe(Effect.delay(50)))
1430+
const outer = Atom.make((get) => get.result(inner, { suspendOnWaiting: true }))
1431+
1432+
r.mount(outer)
1433+
1434+
let result = r.get(outer)
1435+
assert(result.waiting)
1436+
1437+
await vitest.advanceTimersByTimeAsync(50)
1438+
1439+
result = r.get(outer)
1440+
assert(Result.isSuccess(result))
1441+
assert.strictEqual(result.value, 1)
1442+
})
1443+
1444+
it("fn get.result suspendOnWaiting", async () => {
1445+
const r = Registry.make()
1446+
let runs = 0
1447+
1448+
const inner = Atom.fn((n: number) => {
1449+
runs++
1450+
return Effect.succeed(n * 2).pipe(Effect.delay(50))
1451+
})
1452+
1453+
const outer = Atom.fn(
1454+
Effect.fn(function*(_: void, get: Atom.FnContext) {
1455+
get.set(inner, 1)
1456+
const a = yield* get.result(inner, { suspendOnWaiting: true })
1457+
1458+
get.set(inner, 2)
1459+
const b = yield* get.result(inner, { suspendOnWaiting: true })
1460+
1461+
return { a, b }
1462+
})
1463+
)
1464+
1465+
r.mount(outer)
1466+
r.set(outer, void 0)
1467+
1468+
await vitest.advanceTimersByTimeAsync(100)
1469+
1470+
const result = r.get(outer)
1471+
assert(Result.isSuccess(result))
1472+
assert.strictEqual(result.value.a, 2)
1473+
assert.strictEqual(result.value.b, 4)
1474+
assert.strictEqual(runs, 2)
1475+
})
14251476
})
14261477

14271478
interface BuildCounter {

0 commit comments

Comments
 (0)