|
1 | | -import { Effect, Fiber, Stream } from "npm:effect"; |
| 1 | +import * as Effect from "https://raw.githubusercontent.com/Effect-TS/effect-smol/refs/heads/deno/src/Effect.ts"; |
| 2 | +import * as Fiber from "https://raw.githubusercontent.com/Effect-TS/effect-smol/refs/heads/deno/src/Fiber.ts"; |
| 3 | +import * as Stream from "https://raw.githubusercontent.com/Effect-TS/effect-smol/refs/heads/deno/src/Stream.ts"; |
2 | 4 | import { call } from "../../../mod.ts"; |
3 | 5 | import { scenario } from "./scenario.ts"; |
4 | 6 |
|
5 | | -await scenario( |
6 | | - "effect.events", |
7 | | - (depth) => call(() => Effect.runPromise(start(depth))), |
8 | | -); |
9 | | - |
10 | | -export function start(depth: number) { |
11 | | - return Effect.gen(function* () { |
12 | | - const target = new EventTarget(); |
13 | | - const task = yield* Effect.fork(recurse(target, depth)); |
14 | | - for (let i = 0; i < 100; i++) { |
15 | | - yield* Effect.sleep(0); |
16 | | - target.dispatchEvent(new Event("foo")); |
17 | | - } |
| 7 | +export const start = Effect.fnUntraced(function* (depth: number) { |
| 8 | + const target = new EventTarget(); |
| 9 | + const task = yield* Effect.fork(recurse(target, depth)); |
| 10 | + for (let i = 0; i < 100; i++) { |
18 | 11 | yield* Effect.sleep(0); |
19 | | - yield* Fiber.interrupt(task); |
20 | | - }); |
21 | | -} |
| 12 | + target.dispatchEvent(new Event("foo")); |
| 13 | + } |
| 14 | + yield* Effect.sleep(0); |
| 15 | + yield* Fiber.interrupt(task); |
| 16 | +}); |
22 | 17 |
|
23 | | -function recurse( |
| 18 | +const recurse: ( |
24 | 19 | target: EventTarget, |
25 | 20 | depth: number, |
26 | | -): Effect.Effect<void, never, never> { |
27 | | - return Effect.gen(function* () { |
28 | | - const eventStream = Stream.fromEventListener(target, "foo"); |
| 21 | +) => Effect.Effect<void> = Effect.fnUntraced(function* (target, depth) { |
| 22 | + const eventStream = Stream.fromEventListener(target, "foo"); |
29 | 23 |
|
30 | | - if (depth > 1) { |
31 | | - const subTarget = new EventTarget(); |
32 | | - yield* Effect.fork(recurse(subTarget, depth - 1)); |
| 24 | + if (depth > 1) { |
| 25 | + const subTarget = new EventTarget(); |
| 26 | + yield* Effect.fork(recurse(subTarget, depth - 1)); |
33 | 27 |
|
34 | | - yield* eventStream.pipe( |
35 | | - Stream.runForEach(() => { |
36 | | - return Effect.sync(() => { |
37 | | - subTarget.dispatchEvent(new Event("foo")); |
38 | | - }); |
39 | | - }), |
40 | | - ); |
41 | | - } else { |
42 | | - yield* eventStream.pipe(Stream.runDrain); |
43 | | - } |
44 | | - }); |
45 | | -} |
| 28 | + yield* Stream.runForEach(eventStream, () => |
| 29 | + Effect.sync(() => { |
| 30 | + subTarget.dispatchEvent(new Event("foo")); |
| 31 | + })); |
| 32 | + } else { |
| 33 | + yield* Stream.runDrain(eventStream); |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +await scenario( |
| 38 | + "effect.events", |
| 39 | + (depth) => call(() => Effect.runPromise(start(depth))), |
| 40 | +); |
0 commit comments