For a person or a model about to write an app; a model gets the same text from oad guide.

Contents

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 }
  }
}
  • over makes one flow instance per element of a stored list: collection is the list's path, key the field that identifies an element, field the field that holds the element's stage. All three are required.
  • initial is the stage of a new element. Append an element without the field: the host writes initial into it, so the field may still be required in the record type, as status is in the time off example:

JSON

["id", "personId", "personName", "from", "to", "note", "status", "decidedBy"]
  • states maps each stage to what it accepts. on maps an event name to a transition, or to a list of transitions of which the first whose if holds is taken.
  • A transition has to (required), if (which transition), allow (who may take it) and do (steps). In them, item is the element and event the with of the send. Write to the element with { "patch": "item", "with": ... }.
  • "final": true marks 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>.

Next#

  • people for roles and who may move a record.
  • actions for the actions that send events.
  • rules for following transitions.