-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathinspection.ts
More file actions
46 lines (42 loc) · 1.33 KB
/
Copy pathinspection.ts
File metadata and controls
46 lines (42 loc) · 1.33 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
import { Effect, Schema } from "effect";
import { collectingInspector, Event, Machine, State, type InspectionEvent } from "effect-machine";
const AccessState = State({
Locked: { attempts: Schema.Finite },
Open: {},
});
type AccessState = typeof AccessState.Type;
const AccessEvent = Event({
EnterCode: { code: Schema.String },
});
type AccessEvent = typeof AccessEvent.Type;
const accessMachine = Machine.make({
state: AccessState,
event: AccessEvent,
initial: AccessState.Locked({ attempts: 0 }),
})
.when(
AccessState.Locked,
AccessEvent.EnterCode,
function correctCode({ event }) {
return event.code === "1234";
},
function unlock() {
return AccessState.Open;
},
)
.on(AccessState.Locked, AccessEvent.EnterCode, function recordFailedAttempt({ state }) {
return AccessState.Locked({ attempts: state.attempts + 1 });
})
.final(AccessState.Open);
export const inspectionProgram = Effect.gen(function* () {
const events: Array<InspectionEvent<AccessState, AccessEvent>> = [];
const actor = yield* Machine.spawn(accessMachine, {
id: "access",
inspect: collectingInspector(events),
});
yield* actor.start;
yield* actor.send(AccessEvent.EnterCode({ code: "0000" }));
yield* actor.send(AccessEvent.EnterCode({ code: "1234" }));
yield* actor.awaitFinal;
return events;
});