-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcomposition.ts
More file actions
32 lines (27 loc) · 1.03 KB
/
Copy pathcomposition.ts
File metadata and controls
32 lines (27 loc) · 1.03 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
import { Effect, Schema } from "effect";
import { Event, Machine, State } from "effect-machine";
const CartState = State({
Ready: { cartId: Schema.String, totalCents: Schema.Finite },
});
const CartEvent = Event({ Refresh: {} });
const cartMachine = Machine.make({
state: CartState,
event: CartEvent,
initial: CartState.Ready({ cartId: "cart-123", totalCents: 4200 }),
}).final(CartState.Ready, ({ state }) => ({
cartId: state.cartId,
totalCents: state.totalCents,
}));
const PaymentState = State({
Charged: { receiptId: Schema.String },
});
const PaymentEvent = Event({ Refresh: {} });
const paymentMachine = Machine.make({
state: PaymentState,
event: PaymentEvent,
initial: (input: { readonly cartId: string; readonly totalCents: number }) =>
PaymentState.Charged({ receiptId: `${input.cartId}-${input.totalCents}` }),
}).final(PaymentState.Charged, ({ state }) => state.receiptId);
export const compositionProgram = Machine.run(cartMachine).pipe(
Effect.flatMap((cart) => Machine.run(paymentMachine, { input: cart })),
);