Skip to content
@deckyard/fsm

fsm cheatsheet

Every export of @deckyard/fsm. Guides are listed in the introduction.

jennifer
import "@deckyard/fsm/" as fsm;

Every failure is Error{kind: "fsm"}; errors a guard or hook throws pass through unchanged. An m argument is a Machine. A ctx argument is a map of string to string, {} when there is none.

Constants

ConstantValueMeaning
ERROR_KIND"fsm"the kind of every error the deck throws
MAX_NAME100the longest machine, state or event name
MAX_EDGES10000the most transition sources load accepts, summed over all transitions
MAX_STATES10000the most entries load accepts in states and in final

Types

Machine

FieldMeaning
name as stringthe machine's name, in messages and diagrams
initial as stringthe state a new instance starts in
states as list of stringevery state, in the order added
finals as list of stringthe final states
transitions as list of Transitionin the order added
enter as map of string to funcenter hooks by state
leave as map of string to funcleave hooks by state
exits as map of string to list of stringeach state's outgoing events; kept by the deck
routes as map of string to list of int"<state> <event>" to its transitions; kept by the deck

Transition

FieldMeaning
event as stringthe event that takes it
sources as list of stringthe states it leaves from
target as stringthe state it enters
guard as functhe guard; the null function when there is none

Step

FieldMeaning
event as stringthe event fired
source as stringthe state left
target as stringthe state entered
ctx as map of string to stringthe context; after fire, as the hooks returned it

Problem

FieldMeaning
kind as string"unreachable", "dead-end" or "final-exit"
state as stringthe state it is about
message as stringwhat is wrong

Defining

FunctionReturnsDoes
fsm.machine(name, initial)Machinea machine with one state
fsm.addStates(m, names)Machineadd states
fsm.addFinals(m, names)Machinemark states final
fsm.addTransition(m, event, sources, target)Machineadd a transition
fsm.addGuarded(m, event, sources, target, guard)Machineadd a transition taken when guard(step) is true
fsm.onEnter(m, state, hook)Machinerun hook(step) on entering a state
fsm.onLeave(m, state, hook)Machinerun hook(step) on leaving a state
fsm.load(text, format, funcs)Machineread a machine from "toml", "yaml" or "json"; funcs maps the names it uses to functions
fsm.check(m)list of Problemunreachable states, dead ends, final states with exits

Running

FunctionReturnsDoes
fsm.fire(m, state, event, ctx)Steptake the transition, run the hooks
fsm.can(m, state, event, ctx)boolwhether fire would succeed; runs guards, not hooks
fsm.events(m, state, ctx)list of stringthe events that can be fired now
fsm.isState(m, state)boolwhether a string is a state
fsm.isFinal(m, state)boolwhether a state is final

Drawing

FunctionReturnsDoes
fsm.mermaid(m)stringa Mermaid stateDiagram-v2
fsm.dot(m)stringa Graphviz digraph