Std.Fsm
View source →FSM runtime operations: spawn, stop, send events, get state.
Runtime surface for FSM modules compiled from fsm containers.
Every entry delegates to Cure.FSM.Builtins, which wraps
Cure.FSM.Runtime. Spawning captures the calling process as the
FSM's :caller so lifecycle hooks can notify it via
Std.Fsm.notify/1.
Examples
use Std.Fsm
# Assume a compiled `Cure.FSM.TrafficLight` with Red -> Green -> Yellow -> Red.
let pid = spawn(:"Cure.FSM.TrafficLight")
send(pid, :timer)
state(pid) # => :green
stop(pid)
use Std.Fsm
# Drive the FSM through a batch of events.
let pid = spawn_named(:"Cure.FSM.TrafficLight", "main")
send_batch(pid, [:timer, :timer, :timer])
history(pid) # => [:timer, :timer, :timer]
Functions
-
# fn __group__() -> Atom
Group tag consumed by
Cure.Stdlib.Preload. -
# fn caller(pid: Pid) -> Pid extern
Return the caller pid registered for a running FSM, or
nil. -
# fn full_state(pid: Pid) -> Tuple extern
Get the full
%Cure.FSM.State{}wrapped as%[state_atom, %FsmState{caller, meta, payload}]. -
# fn get_state(pid: Pid) -> Tuple extern
Get the current state of an FSM as
%[state_atom, payload]. -
# fn history(pid: Pid) -> List(Atom) extern
Get the event history of an FSM (most-recent-first list of atoms).
-
# fn info(pid: Pid) -> Map(Any, Any) extern
Get metadata about an FSM from the runtime registry.
-
# fn is_alive(pid: Pid) -> Bool extern
truewhen the FSM process is still alive. -
# fn lookup(name: String) -> Pid extern
Look up an FSM by its registered name.
-
# fn notify(message: Any) -> Atom extern
Send
messageto the caller registered for the currently-executing FSM. Intended to be called from inside a lifecycle-hook body; a no-op returning:no_callerwhen invoked outside an FSM process. -
# fn send(pid: Pid, event: Atom) -> Atom extern
Send an event (without a payload) to an FSM.
-
# fn send_batch(pid: Pid, events: List(Atom)) -> Atom extern
Send a batch of events; each is delivered in order.
-
# fn send_with(pid: Pid, event: Atom, payload: Any) -> Atom extern
Send an event carrying a payload to an FSM.
-
# fn spawn(fsm_module: Atom) -> Pid extern
Spawn an FSM instance from a compiled FSM module atom. The spawning process is recorded as the FSM's caller automatically; lifecycle hooks can reach it via
Std.Fsm.notify/1. -
# fn spawn_named(fsm_module: Atom, name: String) -> Pid extern
Spawn a named FSM instance. The
namestring is registered with the runtime registry for laterlookup/1. -
# fn spawn_with(fsm_module: Atom, init: Any) -> Pid extern
Spawn an FSM instance with a fully-specified initial state.
initmay be a%Cure.FSM.State{}struct, a keyword list with:caller/:meta/:payload, or a plain map in the same shape. -
# fn spawn_with_payload(fsm_module: Atom, payload: Any) -> Pid extern
Spawn an FSM instance with an explicit initial payload.
-
# fn state(pid: Pid) -> Atom extern
Get just the current state atom (no payload).
-
# fn stop(pid: Pid) -> Atom extern
Stop a running FSM instance.