Minimal finite state machine with explicit transitions between states.

FSM keeps track of one current state and only allows transitions that are explicitly registered on that state. This keeps state flow easy to reason about: if there is no transition, goto simply does nothing.

Typical usage:

var idle = new State();
var walking = new State();

idle[walking] = () -> trace("start walking");
walking[idle] = () -> trace("stop walking");

var fsm = new FSM(idle);
fsm.goto(walking);

Constructor

new(?state:State)

Creates a finite state machine.

Parameters:

state

Initial state. May be null, in which case goto cannot do anything until a state is assigned.

Variables

current:State

Current active state.

This field can be assigned directly, but in normal usage transitions should go through goto so transition callbacks are respected.

Methods

goto(to:State):Void

Transitions to another state if the current state defines a transition for it.

If the transition exists, the target state becomes current and the transition callback is invoked immediately.

Parameters:

to

Target state.