-
Notifications
You must be signed in to change notification settings - Fork 1
/
13-race-monoid.ts
61 lines (54 loc) · 1.56 KB
/
13-race-monoid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { apply, identity, pipe } from "fp-ts/function";
import * as A from "fp-ts/ReadonlyArray";
import * as T from "fp-ts/Task";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
const logs: Array<string> = [];
const task = (name: string, millis: number): T.Task<string> =>
pipe(
T.of(name),
T.delay(millis),
T.chainFirst(() => T.fromIO(() => logs.push(name))),
);
beforeEach(() => {
logs.length = 0;
});
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
describe("getRaceMonoid", () => {
// NOTE: Run only one test because of race conditions with the other tests.
it.only("Option 1", async () => {
expect(
await pipe(
[
task("slow", 20),
task("fast", 10),
task("slug", 50), //
],
A.foldMap(T.getRaceMonoid<string>())(identity), //
)(),
).toEqual("fast");
expect(logs).toEqual(["fast"]);
});
it("Option 2", async () => {
// Task<string> -> Task<string> -> Task<string>
const concat = T.getRaceMonoid<string>().concat;
expect(
await pipe(
concat,
apply(task("slow", 20)),
apply(task("fast", 10)), //
)(),
).toEqual("fast");
expect(logs).toEqual(["fast"]);
});
it("Option 3", async () => {
// Task<string> -> Task<string> -> Task<string>
const concat = T.getRaceMonoid<string>().concat;
expect(
await pipe(
task("slow", 20),
concat(task("fast", 10)), //
)(),
).toEqual("fast");
expect(logs).toEqual(["fast"]);
});
});