f ( model )
Accelerate development of compositional, safe and ergonomic applications
Functional and Algebraic Domain Modeling
TypeScript
Decide
A pure function that takes command
and current state
as parameters, and returns the flow of new events
.
Evolve
A pure function that takes event
and current state
as parameters, and returns the new state
of the system.
React
A pure function that takes event
as parameter, and returns the flow of commands
, deciding what to execute next.
EventSourced systems
Event-Sourced systems are storing the events
in immutable storage by only appending.
StateStored systems
State-stored systems are traditional systems that are only storing the current state
by overwriting the previous state
in the storage.
export const orderDecider: Decider<OrderCommand, Order | null, OrderEvent> =
new Decider<OrderCommand, Order | null, OrderEvent>(
(command, currentState) => {
switch (command.kind) {
case "CreateOrderCommand":
return (currentState === null)
? [
{
version: 1,
decider: "Order",
kind: "OrderCreatedEvent",
id: command.id,
restaurantId: command.restaurantId,
menuItems: command.menuItems,
final: false,
},
]
: [
{
version: 1,
decider: "Order",
kind: "OrderNotCreatedEvent",
id: command.id,
restaurantId: command.restaurantId,
menuItems: command.menuItems,
final: false,
reason: "Order already exist!",
},
];
case "MarkOrderAsPreparedCommand":
return (currentState !== null && currentState.orderId === command.id)
? [
{
version: 1,
decider: "Order",
kind: "OrderPreparedEvent",
id: currentState.orderId,
final: false,
},
]
: [
{
version: 1,
decider: "Order",
kind: "OrderNotPreparedEvent",
id: command.id,
reason: "Order does not exist!",
final: false,
},
];
default: {
// Exhaustive matching of the command type
const _: never = command;
return [];
}
}
},
(currentState, event) => {
switch (event.kind) {
case "OrderCreatedEvent":
return {
orderId: event.id,
restaurantId: event.restaurantId,
menuItems: event.menuItems,
status: "CREATED",
};
case "OrderNotCreatedEvent":
return currentState;
case "OrderPreparedEvent":
return currentState !== null
? {
orderId: currentState.orderId,
restaurantId: currentState.restaurantId,
menuItems: currentState.menuItems,
status: "PREPARED",
}
: currentState;
case "OrderNotPreparedEvent":
return currentState;
default: {
// Exhaustive matching of the event type
const _: never = event;
return currentState;
}
}
},
null,
);