Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrBuild represents the error of building an FSM. ErrBuild = errors.New("error when building an FSM") // ErrTransitionNotFound indicates that there doesn't exist a transition defined on the source state and the event. ErrTransitionNotFound = errors.New("unable to find a valid transition") // ErrInvalidTransition indicates that the actual destination state after transition is not defined. ErrInvalidTransition = errors.New("invalid transition") )
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is an FSM builder to help construct an FSM.
func NewBuilder ¶
func NewBuilder() *Builder
NewBuilder creates an FSM builder instance with empty setup.
func (*Builder) AddInitialState ¶
AddInitialState adds an initial state
func (*Builder) AddTransition ¶
AddTransition adds a transition setup, including the source state, the event to trigger the transition, the transition callback, and the legal destination transitions.
type Event ¶
type Event interface { // Type returns the event type Type() EventType }
Event is the interface of the events that could be handled by an FSM
type FSM ¶
type FSM interface { // CurrentState returns the current state. CurrentState() State // Handle handles an event and return error if there is any. Handle(Event) error }
FSM is the interface of an FSM (finite state machine). It allows to define the transition logic and destinations after the transition, and intake the event to trigger the transition. The event handling is synchronized, so that it guarantee always processing one event at any time. An FSM must have exactly one initial state.
type Transition ¶
Transition is the interface of the transition that would happen on a certain state when receiving a certain event. The transition returns a destination state where an FSM should transit into or an error.