Flows#
A flow is a state machine: a process with named stages, where only some moves between them are allowed. Use one when a record goes through stages (draft, waiting, approved) and who may move it depends on the stage. For a value that just changes, a stored entry and an action are enough.
A flow over a list of records#
JSON
{
"description": "A request waits for an approver, who approves or declines it; the person who asked can take it back while it waits.",
"over": { "collection": "state.requests", "key": "id", "field": "status" },
"initial": "pending",
"states": {
"pending": {
"on": {
"approve": { "to": "approved", "allow": "'approver' in user.roles && user.person.id != item.personId", "do": [ { "patch": "item", "with": { "decidedBy": "=user.person.name" } } ] },
"decline": { "to": "declined", "allow": "'approver' in user.roles && user.person.id != item.personId", "do": [ { "patch": "item", "with": { "decidedBy": "=user.person.name" } } ] },
"cancel": { "to": "cancelled", "allow": "user.person.id == item.personId" }
}
},
"approved": { "final": true },
"declined": { "final": true },
"cancelled": { "final": true }
}
}
overmakes one flow instance per element of a stored list:collectionis the list's path,keythe field that identifies an element,fieldthe field that holds the element's stage. All three are required.initialis the stage of a new element. Append an element without thefield: the host writesinitialinto it, so the field may still berequiredin the record type, asstatusis in the time off example:
JSON
["id", "personId", "personName", "from", "to", "note", "status", "decidedBy"]
statesmaps each stage to what it accepts.onmaps an event name to a transition, or to a list of transitions of which the first whoseifholds is taken.- A transition has
to(required),if(which transition),allow(who may take it) anddo(steps). In them,itemis the element andeventthewithof thesend. Write to the element with{ "patch": "item", "with": ... }. "final": truemarks a stage with no moves out.
A move may go back to an earlier stage. With this declined stage, the person who asked can hand a request in again, corrected by the with of the send:
JSON
{ "on": { "handIn": { "to": "pending", "allow": "user.person.id == item.personId", "do": [ { "patch": "item", "with": { "note": "=event.note", "decidedBy": "" } } ] } } }
Moving a flow#
An action sends an event with a send step; for a flow over a list, key says which element:
JSON
[ { "send": "approve", "to": "approval", "key": "=input.id" } ]
A send carries data in with, which the transition reads as event. This patch lets an approver give a reason when declining:
JSON
[
{ "op": "add", "path": "/actions/decline/input/reason", "value": { "type": "string", "description": "Why, in words the person reads.", "default": "" } },
{ "op": "replace", "path": "/actions/decline/steps", "value": [ { "send": "decline", "to": "approval", "key": "=input.id", "with": { "reason": "=input.reason" } } ] },
{ "op": "replace", "path": "/flows/approval/states/pending/on/decline/do", "value": [ { "patch": "item", "with": { "decidedBy": "=`${user.person.name}: ${event.reason}`" } } ] }
]
A handler cannot send (view.handlerStep): the screen runs the action, so an agent moves the flow on the same terms. A flow over a list needs key, a flow without over must not have one (flow.keyMismatch):
JSON
[ { "send": "approve", "to": "approval" } ]
When the transition's allow is not true, the event fails with flow.forbidden and nothing moves; the reason is not told. When the element's stage has no transition for the event, it fails with flow.noTransition. Give the person words by checking the same conditions in the action's require (see the time off example's decline), and show a button only where canSend is true (see views).
The stage field belongs to the flow#
Only the flow writes the element's stage. A step or binding that writes it is flow.fieldWrite, whether through the list or through item. Read it freely: state.requests.filter(r, r.status == 'pending'). To show it in words, read a map by it; the time off example's table gives each row its words in a field of its own:
JSON
"=state.requests.filter(r, r.personId == user.person.id).map(r, merge(r, {stage: {pending: 'Waiting', approved: 'Approved', declined: 'Declined', cancelled: 'Taken back'}[r.status]}))"
Stages with steps and timers#
A stage may have enter and exit steps, run when an element arrives in it and leaves it. A stage may have after: a duration mapped to a transition taken when the element stays that long, such as the duration P3D mapped to a transition to a later stage. Timers run only while someone has the app open, unless the app holds a core:scheduler grant (trigger.noBackground warns otherwise).
A flow without over#
A flow without over has one instance for the whole app, such as the phases of a game. Its stage is read as flows.<name>.state, and send takes no key.
Events#
Every transition raises flow:<name> with from, to, event and key. A rule can follow it, and an agent surface can list it. To notify people of a move, do it in the transition's do, or in a rule that follows flow:<name>.