Documentation
¶
Index ¶
- type State
- type StateConfig
- func (c *StateConfig) OnEnter(f func()) *StateConfig
- func (c *StateConfig) OnEnterFrom(t Trigger, f func(interface{})) *StateConfig
- func (c *StateConfig) OnExit(f func()) *StateConfig
- func (c *StateConfig) Permit(t Trigger, s State) *StateConfig
- func (c *StateConfig) PermitIf(t Trigger, s State, pred func() bool) *StateConfig
- func (c *StateConfig) SubstateOf(s State) *StateConfig
- type StateMachine
- type Trigger
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type State ¶
type State struct {
// Name is the name of the state, give it something meaningful
Name string
}
State represents a single state of the system.
type StateConfig ¶
type StateConfig struct {
// contains filtered or unexported fields
}
StateConfig stores all of the config information for a state.
func NewStateConfig ¶
func NewStateConfig(sm *StateMachine, s State) *StateConfig
NewStateConfig returns an initialized StateConfig instance
func (*StateConfig) OnEnter ¶
func (c *StateConfig) OnEnter(f func()) *StateConfig
OnEnter registers a handler that will be fired when the state is entered. This is also fired for re-entrant transitions where a state transitions to itself. This handler is called for all triggers that enter a state, if you only want to perform an action entering a state for a certain trigger, then use OnEnterFrom instead.
func (*StateConfig) OnEnterFrom ¶
func (c *StateConfig) OnEnterFrom(t Trigger, f func(interface{})) *StateConfig
OnEnterFrom registers a handler that will fire when entering a state only via the specified trigger.
func (*StateConfig) OnExit ¶
func (c *StateConfig) OnExit(f func()) *StateConfig
OnExit registers a handler that will fire when we exit a state. This will also for for re-entrant transitions where we transition from a state to itself.
func (*StateConfig) Permit ¶
func (c *StateConfig) Permit(t Trigger, s State) *StateConfig
Permit adds an entry indicating the state is permitted to transition to the target state via the specified trigger.
func (*StateConfig) PermitIf ¶
func (c *StateConfig) PermitIf(t Trigger, s State, pred func() bool) *StateConfig
PermitIf defines a relationship from one state to another via a trigger, which is valid when the predicate function evaluates to true. You can use this to say that we can transition from one state to another via a trigger only under certain conditions
func (*StateConfig) SubstateOf ¶
func (c *StateConfig) SubstateOf(s State) *StateConfig
SubstateOf specifies that a state is a substate of another. This means you can specify that state B is a substate of state A and if the state machine is currently in state B, asking IsInState(A) will return true ans well as IsInState(B). This is also true for any depth of substate relationship.
type StateMachine ¶
type StateMachine struct {
// contains filtered or unexported fields
}
StateMachine is a simple state machine that allows a caller to specify states, triggers and edges between states via triggers. See the examples and test files for more examples.
func NewStateMachine ¶
func NewStateMachine(initial State) *StateMachine
NewStateMachine returns an initialized StateMachine instance.
func (*StateMachine) CanFire ¶
func (sm *StateMachine) CanFire(triggerKey string) bool
CanFire returns true if the specified trigger is valid for the State Machines current state.
func (*StateMachine) Configure ¶
func (sm *StateMachine) Configure(s State) *StateConfig
Configure is used to configure a state, such as setting OnEnter/OnExit handlers, defining valid triggers etc.
func (*StateMachine) Fire ¶
func (sm *StateMachine) Fire(triggerKey string, ctx interface{}) error
Fire fires the specified trigger. If the trigger is not valid for the current state an error is returned.
func (*StateMachine) IsInState ¶
func (sm *StateMachine) IsInState(s State) bool
IsInState returns true if the current state matches the specified state. It will also return true if the current state is a substate of the specified state.
func (*StateMachine) State ¶
func (sm *StateMachine) State() State
State returns the current state of the state machine.