-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathatom.ts
More file actions
49 lines (42 loc) · 1.55 KB
/
Copy pathatom.ts
File metadata and controls
49 lines (42 loc) · 1.55 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
48
49
import { Effect, Schema } from "effect";
import * as AtomRegistry from "effect/unstable/reactivity/AtomRegistry";
import { Event, Machine, State } from "effect-machine";
import * as ActorAtom from "effect-machine/atom";
const ProfileState = State({
Editing: { name: Schema.String, visits: Schema.Finite },
});
const ProfileEvent = Event({
Rename: { name: Schema.String },
Visit: {},
});
const profileMachine = Machine.make({
state: ProfileState,
event: ProfileEvent,
initial: ProfileState.Editing({ name: "Ada", visits: 0 }),
})
.on(ProfileState.Editing, ProfileEvent.Rename, ({ state, event }) =>
ProfileState.Editing.with(state, { name: event.name }),
)
.on(ProfileState.Editing, ProfileEvent.Visit, ({ state }) =>
ProfileState.Editing.with(state, { visits: state.visits + 1 }),
);
export const atomProgram = Effect.gen(function* () {
const actor = yield* Machine.spawn(profileMachine);
yield* actor.start;
const registry = AtomRegistry.make();
const stateAtom = ActorAtom.make(actor);
const visitsAtom = ActorAtom.select(stateAtom, (state) => state.visits);
const visits: Array<number> = [];
const unsubscribe = registry.subscribe(visitsAtom, (value) => visits.push(value), {
immediate: true,
});
registry.set(visitsAtom, ProfileEvent.Rename({ name: "Grace" }));
yield* actor.waitFor((state) => state.name === "Grace");
registry.set(visitsAtom, ProfileEvent.Visit);
yield* actor.waitFor((state) => state.visits === 1);
yield* Effect.yieldNow;
unsubscribe();
registry.dispose();
yield* actor.stop;
return visits;
});