class FSM::StateDefinition(T)

Overview

A state in the machine definition. Renamed from the old State now that State names the runtime snapshot.

StateDefinition(T) is generic over the caller's context type T, which guards and callbacks receive directly. It is a class with reference semantics: the builder creates it, mutates it during build, and the machine shares one sealed object across every interpreter and fiber. Once sealed, its builder methods raise SealedStateError, which is what keeps a shared definition immutable after build.

Defined in:

fsm/state_definition.cr

Instance Method Summary

Instance Method Detail

def all_target_states : Array(String) #

The distinct target states reachable from this state, used by the builder to validate that every target names an existing state.


[View source]
def id : String #

[View source]
def on_blocked(event : String, &block : String, T, Array(String) -> ) : self #

Register a handler fired when the event has candidate transitions but every guard rejected. It is keyed per event and does not fire for an unknown event or when any transition succeeds. The handler receives the blocked target ids so it can give a specific reason without re-evaluating any guard. Registering a second handler for the same event replaces the first (last write wins, the same semantics as on_entry and on_exit).


[View source]
def on_entry(&block : String, T -> ) : self #

Register a callback fired when this state is entered.


[View source]
def on_event(event : String, target : String) : self #

Register a transition from this state to a target on an event. Calling on_event repeatedly for one event appends, building the ordered candidate list resolution walks.


[View source]
def on_event(event : String, target : String, &) : self #

Register a transition and yield it so a guard, transition callback, or the internal flag can be attached. Because Transition(T) is a class, the yielded object is the same one the machine keeps and the interpreter reads at runtime.


[View source]
def on_exit(&block : String, T -> ) : self #

Register a callback fired when this state is exited.


[View source]
def on_unknown_event(&block : String, T -> ) : self #

Register the state-wide handler fired when an event has no transition registered on this state at all. It is not keyed by event because a single handler catches every unregistered event. It does not fire for a blocked event or when any transition succeeds. The handler receives the event and the context, matching on_entry and on_exit; there are no blocked target ids because no transition was registered. Registering a second handler replaces the first (last write wins).


[View source]