Documentation ¶
Overview ¶
Package fsm is a simple finite state machine in Go. This state machine is safe for concurrent use, so multiple goroutines can work with the machine safely.
Index ¶
- Constants
- type CallbackHandler
- type Error
- type ErrorCode
- type Machine
- func (m *Machine) AddStateTransitionRules(sourceState State, destinationStates ...State) error
- func (m *Machine) CurrentState() State
- func (m *Machine) SetStateTransitionCallback(callback CallbackHandler, synchronous bool) error
- func (m *Machine) StateTransition(toState State) error
- func (m *Machine) StateTransitionRules(state State) (TransitionRuleSet, error)
- type State
- type TransitionRuleSet
Constants ¶
const Version = "0.0.2"
Version is the semantic version (SemVer) string.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CallbackHandler ¶
CallbackHandler is an interface type defining the interface for receiving callbacks.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the struct representing internal errors. This implements the error interface
type ErrorCode ¶
type ErrorCode uint
ErrorCode is the type for package-specific error codes. This is used within the Error struct, which allows you to programatically determine the error cause.
const ( // ErrorUnknown is the default value ErrorUnknown ErrorCode = iota // ErrorMachineNotInitialized is an error returned when actions are taken on // a machine before it has been initialized. A machine is initialized by // adding at least one state and setting it as the initial state. ErrorMachineNotInitialized // ErrorTransitionNotPermitted is the error returned when trying to // transition to an invalid state. In other words, the machine is not // permitted to transition from the current state to the one requested. ErrorTransitionNotPermitted // ErrorStateUndefined is the error returned when the requested state is // not defined within the machine. ErrorStateUndefined )
type Machine ¶
type Machine struct {
// contains filtered or unexported fields
}
Machine is the state machine.
func (*Machine) AddStateTransitionRules ¶
AddStateTransitionRules is a function for adding valid state transitions to the machine. This allows you to define which states any given state can be transitioned to.
func (*Machine) CurrentState ¶
CurrentState returns the machine's current state. If the State returned is "", then the machine has not been given an initial state.
func (*Machine) SetStateTransitionCallback ¶
func (m *Machine) SetStateTransitionCallback(callback CallbackHandler, synchronous bool) error
SetStateTransitionCallback for the state transition. This is meant to send callbacks back to the consumer for state changes. The callback only sends the new state. The synchonous parameter indicates whether the callback is done synchronously with the StateTransition() call.
func (*Machine) StateTransition ¶
StateTransition triggers a transition to the toState. This function is also used to set the initial state of machine.
Before you can transition to any state, even for the initial, you must define it with AddStateTransition(). If you are setting the initial state, and that state is not define, this will return an ErrInvalidInitialState error.
When transitioning from a state, this function will return an error either if the state transition is not allowed, or if the destination state has not been defined. In both cases, it's seen as a non-permitted state transition.
func (*Machine) StateTransitionRules ¶
func (m *Machine) StateTransitionRules(state State) (TransitionRuleSet, error)
StateTransitionRules returns the allowed states for
type TransitionRuleSet ¶
type TransitionRuleSet map[State]struct{}
TransitionRuleSet is a set of allowed transitions. This uses map of struct{} to implement a set.
func (TransitionRuleSet) Copy ¶
func (trs TransitionRuleSet) Copy() TransitionRuleSet
Copy copies the TransitionRuleSet in to a different TransitionRuleSet.