-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbasic.ts
More file actions
47 lines (43 loc) · 1.48 KB
/
Copy pathbasic.ts
File metadata and controls
47 lines (43 loc) · 1.48 KB
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
import { Effect, Schema } from "effect";
import { Event, Machine, State } from "effect-machine";
export const CounterState = State({
Counting: { count: Schema.Finite, limit: Schema.Finite },
AtLimit: { count: Schema.Finite, limit: Schema.Finite },
Done: { count: Schema.Finite },
});
export const CounterEvent = Event({
Increment: {},
Finish: {},
});
export const counterMachine = Machine.make({
state: CounterState,
event: CounterEvent,
initial: (input: { readonly count: number; readonly limit: number }) =>
CounterState.Counting(input),
})
.when(
CounterState.Counting,
CounterEvent.Increment,
({ state }) => state.count < state.limit,
({ state }) => CounterState.Counting.with(state, { count: state.count + 1 }),
)
.on(CounterState.Counting, CounterEvent.Increment, ({ state }) =>
CounterState.AtLimit.with(state),
)
.on([CounterState.Counting, CounterState.AtLimit], CounterEvent.Finish, ({ state }) =>
CounterState.Done.with(state),
)
.final(CounterState.Done, ({ state }) => state.count);
export const basicProgram = Effect.scoped(
Machine.scoped(
Effect.gen(function* () {
const actor = yield* Machine.spawn(counterMachine, { input: { count: 0, limit: 2 } });
yield* actor.start;
yield* actor.send(CounterEvent.Increment);
yield* actor.send(CounterEvent.Increment);
yield* actor.send(CounterEvent.Increment);
yield* actor.send(CounterEvent.Finish);
return yield* actor.awaitOutput;
}),
),
);