States and transitions
A Template declares the states its Items can be in, and the transitions that lead from one state to another. A handler reads the state of an Item and executes a transition to move it.
Builder calls
Section titled “Builder calls”addState, addTransition
Section titled “addState, addTransition”template.addState(key: string): StateBuilderstate.addTransition(key: string, to: StateBuilder): TransitionBuilderstate.setDefault(): StateBuilderaddState declares a state that Items of the Template can be in. addTransition declares a named, one-way move from that state to another state of the same Template.
| Argument | Type | Description |
|---|---|---|
key |
string |
A lowercase letter, then letters and digits, 64 characters at most. A Template uses a state key once, and a state uses a transition key once. |
to |
StateBuilder |
The state the move ends in. |
addState returns the state, and addTransition returns the transition. .setDefault() makes a state the one a new Item starts in, and returns that state. Without it, the first state added is the default.
const pending = leaveRequest.addState('pending').setDefault(); const approved = leaveRequest.addState('approved'); pending.addTransition('approve', approved);
const rejected = leaveRequest.addState('rejected'); pending.addTransition('reject', rejected);
const canceled = leaveRequest.addState('canceled'); pending.addTransition('cancel', canceled); approved.addTransition('cancel', canceled);Behavior
- A new Item starts in the default state.
- Two states can each declare a transition under the same key, as
pendingandapproveddo withcancel.
See: State machines, Tutorial: the lifecycle, Sales pipeline with stages, TemplateBuilder.addState, StateBuilder.addTransition.
In a handler
Section titled “In a handler”state.value
Section titled “state.value”item.state: ReadStateInstance | nullitem.state.value: string | nullThe key of the state an Item is in: the key given to addState. The type of item.state allows null, so a handler reads the value as item.state?.value.
A handler compares the value with a state key to decide what to do. Here approve, a Mutation, refuses an Item that is not pending before it executes a transition.
const approve = app.addMutation('approve', async (input: IdInput, current) => { const item = await current.templates.leaveRequest.getItem(input.id); if (item === undefined || item.state?.value !== 'pending') { throw new Error('Leave request is not pending'); } const updated = await item.state.transitions.approve.execute({}); await updated.events.approved.publish({}); return updated; });Behavior
- The value is the key of the state the Item is in, and it is the key of the new state once a transition has been executed.
- When a Mutation throws on a value it does not accept, the App refuses the call and the Item stays in its state.
See: State machines, Tutorial: the lifecycle, ReadStateInstance.value.
state.transitions
Section titled “state.transitions”item.state.transitions: Record<string | number, WriteTransitionInstance>The transitions that the current state of an Item declares, each under its key. The type has it on a WriteItem, which a Mutation gets, and not on a ReadItem.
A handler that knows the key reads the transition by name, as in item.state?.transitions.cancel. A handler that takes the key from its input tests it first, as move does here.
const item = await current.templates.deal.getItem(input.id); if (item === undefined) throw new Error('Deal not found'); const transitions = item.state?.transitions ?? {}; if (!Object.keys(transitions).includes(input.move)) { throw new Error(`A ${item.state?.value} deal cannot ${input.move}`); } const moved = await transitions[input.move].execute({});Behavior
- An Item offers the transitions its current state declares, and no others.
- The key of a transition that only another state declares reads as
undefined. Object.keyslists the keys the current state declares. A handler that tests a key against that list refuses every other key, and the App refuses the call.
See: State machines, Sales pipeline with stages, WriteStateInstance.transitions.
execute
Section titled “execute”transition.execute(payload: Record<string, unknown>): Promise<WriteItem>Moves an Item from its current state to the state the transition leads to.
| Argument | Type | Description |
|---|---|---|
payload |
Record<string, unknown> |
An object. Pass {}. |
Returns the Item in its new state. The samples carry on with the Item it returns: cancel returns it from the Mutation, and approve publishes an Event on it.
const cancel = app.addMutation('cancel', async (input: IdInput, current) => { const item = await current.templates.leaveRequest.getItem(input.id); if (item === undefined) throw new Error('Leave request not found'); const transition = item.state?.transitions.cancel; if (transition === undefined) { throw new Error(`A ${item.state?.value} leave request cannot be canceled`); } return await transition.execute({}); });Behavior
- The Item it returns is in the state the transition leads to.
- After the move the Item offers the transitions of its new state, and none where that state declares none.
See: State machines, Tutorial: the lifecycle, Move an item to a new state at a deadline, WriteTransitionInstance.execute.

