Documentation ¶
Overview ¶
Package fsm defines a finite state machine and has the ability to save this state machine to a graph file This graph file can be visualized using mermaid.js
Index ¶
- type FSM
- func (fsm *FSM) CheckTransition(desired StateID) error
- func (fsm *FSM) GenerateGraph() string
- func (fsm *FSM) GoTransition(newState StateID) (bool, error)
- func (fsm *FSM) GoTransitionRequired(newState StateID, data interface{}) error
- func (fsm *FSM) GoTransitionWithData(newState StateID, data interface{}) (bool, error)
- func (fsm *FSM) InState(check StateID) bool
- func (fsm *FSM) Init(current StateID, states States, ...)
- type State
- type StateID
- type StateIDSlice
- type States
- type Transition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FSM ¶
type FSM struct { // States is the map from state ID to states States States // Current is the current state represented by the identifier Current StateID // Name represents the descriptive name of this state machine Name string // StateCallback is the function ran when a transition occurs // It takes the old state, the new state and the data and returns if this is handled by the client StateCallback func(StateID, StateID, interface{}) bool // Directory represents the path where the state graph is stored Directory string // Generate represents whether we want to generate the graph Generate bool // GetStateName gets the name of a state as a string GetStateName func(StateID) string // contains filtered or unexported fields }
FSM represents the total graph.
func (*FSM) CheckTransition ¶
CheckTransition returns an error whether or not a transition to state `desired` is possible
func (*FSM) GenerateGraph ¶
GenerateGraph generates a mermaid graph if the state machine is initialized If the graph cannot be generated, it returns the empty string.
func (*FSM) GoTransition ¶
GoTransition is an alias to call GoTransitionWithData but have an empty string as data.
func (*FSM) GoTransitionRequired ¶
GoTransitionRequired transitions the state machine to a new state with associated state data 'data' If this transition is not handled by the client, it returns an error.
func (*FSM) GoTransitionWithData ¶
GoTransitionWithData is a helper that transitions the state machine toward the 'newState' with associated state data 'data' It returns whether or not the transition is handled by the client.
type State ¶
type State struct { // Transitions indicates which out arrows this node has Transitions []Transition }
State represents a single node in the graph.
type StateIDSlice ¶
type StateIDSlice []StateID
StateIDSlice represents the list of state identifiers.
func (StateIDSlice) Len ¶
func (v StateIDSlice) Len() int
Len is defined here such that we can sort the slice
func (StateIDSlice) Less ¶
func (v StateIDSlice) Less(i, j int) bool
Less is defined here such that we can sort the slice
func (StateIDSlice) Swap ¶
func (v StateIDSlice) Swap(i, j int)
Swap is defined here such that we can sort the slice
type Transition ¶
type Transition struct { // To represents the to-be-new state To StateID // Description is what type of message the arrow gets in the graph Description string }
Transition indicates an arrow in the state graph.