|
1 | | -import { expect, test } from "bun:test" |
| 1 | +import { expect } from "bun:test" |
2 | 2 | import { Effect, Layer, Context } from "effect" |
3 | 3 | import { makeRuntime } from "../../src/effect/run-service" |
| 4 | +import { it } from "../lib/effect" |
4 | 5 |
|
5 | 6 | class Shared extends Context.Service<Shared, { readonly id: number }>()("@test/Shared") {} |
6 | 7 |
|
7 | | -test("makeRuntime shares dependent layers through the shared memo map", async () => { |
8 | | - let n = 0 |
| 8 | +it.live("makeRuntime shares dependent layers through the shared memo map", () => |
| 9 | + Effect.gen(function* () { |
| 10 | + let n = 0 |
9 | 11 |
|
10 | | - const shared = Layer.effect( |
11 | | - Shared, |
12 | | - Effect.sync(() => { |
13 | | - n += 1 |
14 | | - return Shared.of({ id: n }) |
15 | | - }), |
16 | | - ) |
| 12 | + const shared = Layer.effect( |
| 13 | + Shared, |
| 14 | + Effect.sync(() => { |
| 15 | + n += 1 |
| 16 | + return Shared.of({ id: n }) |
| 17 | + }), |
| 18 | + ) |
17 | 19 |
|
18 | | - class One extends Context.Service<One, { readonly get: () => Effect.Effect<number> }>()("@test/One") {} |
19 | | - const one = Layer.effect( |
20 | | - One, |
21 | | - Effect.gen(function* () { |
22 | | - const svc = yield* Shared |
23 | | - return One.of({ |
24 | | - get: Effect.fn("One.get")(() => Effect.succeed(svc.id)), |
25 | | - }) |
26 | | - }), |
27 | | - ).pipe(Layer.provide(shared)) |
| 20 | + class One extends Context.Service<One, { readonly get: () => Effect.Effect<number> }>()("@test/One") {} |
| 21 | + const one = Layer.effect( |
| 22 | + One, |
| 23 | + Effect.gen(function* () { |
| 24 | + const svc = yield* Shared |
| 25 | + return One.of({ |
| 26 | + get: Effect.fn("One.get")(() => Effect.succeed(svc.id)), |
| 27 | + }) |
| 28 | + }), |
| 29 | + ).pipe(Layer.provide(shared)) |
28 | 30 |
|
29 | | - class Two extends Context.Service<Two, { readonly get: () => Effect.Effect<number> }>()("@test/Two") {} |
30 | | - const two = Layer.effect( |
31 | | - Two, |
32 | | - Effect.gen(function* () { |
33 | | - const svc = yield* Shared |
34 | | - return Two.of({ |
35 | | - get: Effect.fn("Two.get")(() => Effect.succeed(svc.id)), |
36 | | - }) |
37 | | - }), |
38 | | - ).pipe(Layer.provide(shared)) |
| 31 | + class Two extends Context.Service<Two, { readonly get: () => Effect.Effect<number> }>()("@test/Two") {} |
| 32 | + const two = Layer.effect( |
| 33 | + Two, |
| 34 | + Effect.gen(function* () { |
| 35 | + const svc = yield* Shared |
| 36 | + return Two.of({ |
| 37 | + get: Effect.fn("Two.get")(() => Effect.succeed(svc.id)), |
| 38 | + }) |
| 39 | + }), |
| 40 | + ).pipe(Layer.provide(shared)) |
39 | 41 |
|
40 | | - const { runPromise: runOne } = makeRuntime(One, one) |
41 | | - const { runPromise: runTwo } = makeRuntime(Two, two) |
| 42 | + const { runPromise: runOne } = makeRuntime(One, one) |
| 43 | + const { runPromise: runTwo } = makeRuntime(Two, two) |
42 | 44 |
|
43 | | - expect(await runOne((svc) => svc.get())).toBe(1) |
44 | | - expect(await runTwo((svc) => svc.get())).toBe(1) |
45 | | - expect(n).toBe(1) |
46 | | -}) |
| 45 | + expect(yield* Effect.promise(() => runOne((svc) => svc.get()))).toBe(1) |
| 46 | + expect(yield* Effect.promise(() => runTwo((svc) => svc.get()))).toBe(1) |
| 47 | + expect(n).toBe(1) |
| 48 | + }), |
| 49 | +) |