// Composition
r01
One machine per entity
Don't try to fit ten orders into one machine. Spin up a fresh instance per row, and let each one run its own lifecycle independently.
Code
const OrderM = () => sm`
cart 'pay' → paid 'ship' → done;
`;
const byId = new Map();
function machineFor(id) {
if (!byId.has(id)) byId.set(id, OrderM());
return byId.get(id);
}
Notes
Each entity carries its own state — no shared bag of flags, no row accidentally inheriting another's status. When the row's gone, drop its machine.