Documentation
¶
Overview ¶
Package fsm implements a simple finite state machine. Events cause Transitions between States, and entry/exit to a state may be hooked by defining one or more functions to be called. That's it!
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event int
Event represents an event that can occur which may cause a state transition.
type Machine ¶
type Machine struct { // State is the current Machine state. State State // Transition is a slice of possible transitions from state to state. Transitions []Transition // OnEntry is a way of hooking transitions between functions. Each // func(Event, State) will be called just before the Machine enters the // associated State. The State passed to the TransitionFunc is the _source_ // state of the transition. OnEntry map[State][]TransitionFunc // OnExit works similarly to OnEntry. Each func(Event, State) will be called // just before the Machine leaves the associated State. The State passed to // the TransitionFunc is the _destination_ state of the transition. OnExit map[State][]TransitionFunc // By default Occur() will return an UnexpectedEventError when an event // occurs with no matching transition given the current state. If // IgnoreUnexpectedEvent is true, unexpected events will be ignored instead. IgnoreUnexpectedEvent bool // contains filtered or unexported fields }
Machine represents a finite state machine (FSM).
func (*Machine) Occur ¶
Occur handles events which may cause a transition in the machine's state. It handles synchronisation via an internal mutex, so is safe to call from multiple goroutines.
The order of operations is: * OnExit functions of the source state. * OnEntry functions of the destination state. * Update Machine state.
type Transition ¶
Transition represents a transition that the FSM can make between states.
type TransitionFunc ¶
A TransitionFunc handles an event during a state transition.
type UnexpectedEventError ¶
UnexpectedEventError is an error type that exposes the event/state that caused the error.
func (UnexpectedEventError) Error ¶
func (e UnexpectedEventError) Error() string