Skip to content

Commit bd71ba5

Browse files
committed
try Effect 4 for thefrontside#981
1 parent dafca73 commit bd71ba5

6 files changed

Lines changed: 58 additions & 64 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33

44
# Local Netlify folder
55
.netlify
6-
/build/
6+
/build/
7+
.direnv/

tasks/bench.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ await main(function* (args) {
5656
for (let scenario of filter(scenarios, { include, exclude })) {
5757
tasks.push(
5858
yield* spawn(() =>
59-
runBenchmark(scenario, { ...options, type: "benchmark" }),
59+
runBenchmark(scenario, { ...options, type: "benchmark" })
6060
),
6161
);
6262
}
Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
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";
24
import { call } from "../../../mod.ts";
35
import { scenario } from "./scenario.ts";
46

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++) {
1811
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+
});
2217

23-
function recurse(
18+
const recurse: (
2419
target: EventTarget,
2520
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");
2923

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));
3327

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+
);
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
import { Effect } from "npm:effect";
1+
import * as Effect from "https://raw.githubusercontent.com/Effect-TS/effect-smol/refs/heads/deno/src/Effect.ts";
22
import { call } from "../../../mod.ts";
33
import { scenario } from "./scenario.ts";
44

5-
await scenario("effect.recursion", (depth) =>
6-
call(() => Effect.runPromise(recurse(depth)))
7-
);
5+
const recurse: (
6+
depth: number,
7+
) => Effect.Effect<void> = Effect.fnUntraced(function* (depth) {
8+
if (depth > 1) {
9+
return yield* recurse(depth - 1);
10+
}
11+
for (let i = 0; i < 100; i++) {
12+
yield* Effect.void;
13+
}
14+
});
815

9-
function recurse(depth: number): Effect.Effect<void, never, never> {
10-
return Effect.gen(function* () {
11-
if (depth > 1) {
12-
yield* recurse(depth - 1);
13-
} else {
14-
for (let i = 0; i < 100; i++) {
15-
yield* Effect.promise(() => Promise.resolve());
16-
}
17-
}
18-
});
19-
}
16+
await scenario(
17+
"effect.recursion",
18+
(depth) => call(() => Effect.runPromise(recurse(depth))),
19+
);
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
import { Effect, Fiber } from "npm:effect";
2-
import { call, ensure } from "../../../mod.ts";
1+
import * as Effect from "https://raw.githubusercontent.com/Effect-TS/effect-smol/refs/heads/deno/src/Effect.ts";
2+
import { action } from "../../../mod.ts";
33
import { scenario } from "./scenario.ts";
44

55
await scenario("effect.startup", function* (_, exit) {
66
let start = performance.now();
77

8-
const startup = Effect.gen(function* () {
8+
const startup = Effect.suspend(() => {
99
exit(performance.now() - start);
10-
yield* Effect.promise(() => Promise.resolve());
10+
return Effect.promise(() => Promise.resolve());
1111
});
1212

1313
const fiber = Effect.runFork(startup);
1414

15-
yield* ensure(function* () {
16-
yield* call(() => Effect.runPromise(Fiber.interrupt(fiber)));
17-
});
18-
19-
return yield* call(() => Effect.runPromise(Fiber.join(fiber)));
15+
return yield* action((resolve) => fiber.addObserver(() => resolve()));
2016
});

tasks/bench/scenarios/effection.startup.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { call, type Operation } from "../../../mod.ts";
1+
import { call, type Operation, spawn } from "../../../mod.ts";
22
import { scenario } from "./scenario.ts";
33

44
await scenario("effection.startup", function* (_, exit) {
@@ -9,5 +9,7 @@ await scenario("effection.startup", function* (_, exit) {
99
yield* call(() => Promise.resolve());
1010
}
1111

12-
return yield* startup();
12+
const task = yield* spawn(() => startup());
13+
14+
return yield* task;
1315
});

0 commit comments

Comments
 (0)