Documentation ¶
Index ¶
- Variables
- type Builder
- func (b *Builder) AddRule(rule *Rule) *Builder
- func (b *Builder) AddTimeoutRule(timeoutRule *TimeoutRule) *Builder
- func (b *Builder) Build() (StateMachine, error)
- func (b *Builder) WithCurrentState(current State) *Builder
- func (b *Builder) WithName(name string) *Builder
- func (b *Builder) WithTransitionCallback(callback Callback) *Builder
- type Callback
- type Option
- type Rule
- type State
- type StateMachine
- type StateTimer
- type TimeoutRule
- type Transition
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoOpTransition is returned when the transition is a no-op ErrNoOpTransition = errors.Errorf("no-op transition") )
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is the state machine builder
func (*Builder) AddTimeoutRule ¶
func (b *Builder) AddTimeoutRule(timeoutRule *TimeoutRule) *Builder
AddTimeoutRule adds the rule for state machine
func (*Builder) Build ¶
func (b *Builder) Build() (StateMachine, error)
Build builds the state machine
func (*Builder) WithCurrentState ¶
WithCurrentState adds the current state
func (*Builder) WithTransitionCallback ¶
WithTransitionCallback adds the transition call back
type Option ¶
type Option func(*statemachine)
Option is the option passed to TransitTo to trigger state machine transition
func WithInfo ¶
WithInfo is an option passed in TransitTo to have store any meta infor about state machine
func WithReason ¶
WithReason is an Option and is used to provide the state transition reason
type Rule ¶
type Rule struct { // from is the source state From State // to is the destination state To []State // callback is transition function which defines 1:1 mapping // of callbacks Callback func(*Transition) error }
Rule is struct to define the transition rules Rule is from one source state to multiple destination states This can define callback function from 1:1 basis from src->dest state
type StateMachine ¶
type StateMachine interface { // TransitTo function transits to desired state TransitTo(to State, options ...Option) error // GetCurrentState returns the current state of State Machine GetCurrentState() State // GetReason returns the reason for the last state transition GetReason() string // GetName returns the Name of the StateMachine object GetName() string // GetStateTimer returns the statetimer object GetStateTimer() StateTimer // GetLastUpdatedTime returns the last update time of the state machine GetLastUpdateTime() time.Time // Terminates the state machine Terminate() // GetMetaInfo returns the map of meta info about state machine GetMetaInfo() map[string]string // GetTimeOutRules returns the timeout rules defined for state machine // Its a map of [from state] to timeout rule GetTimeOutRules() map[State]*TimeoutRule }
StateMachine is the interface wrapping around the statemachine Object Using to not expose full object
func NewStateMachine ¶
func NewStateMachine( name string, current State, rules map[State]*Rule, timeoutRules map[State]*TimeoutRule, trasitionCallback Callback, ) (StateMachine, error)
NewStateMachine it will create the new state machine which clients can use to do tansitions on the object
type StateTimer ¶
type StateTimer interface { // Start starts the state recovery Start(timeout time.Duration) error // Stop stops the state recovery Stop() }
StateTimer is the interface for recovering the states
func NewTimer ¶
func NewTimer(sm *statemachine) StateTimer
NewTimer returns the object for the state timer
type TimeoutRule ¶
type TimeoutRule struct { // from is the source state From State // to is the list of destination states // actual destination state has to be determine // in precall back To []State // timeout for transition to "to" state Timeout time.Duration // callback is transition function which defines 1:1 mapping // of callbacks Callback func(*Transition) error // PreCallback is needed to determine about which state timeout // going to transition. If precallback is not present // Timeout should transition to first TO state. PreCallback func(*Transition) error }
TimeoutRule is a struct to define the state transition which is triggered by the time duration. This is kind of timeout where state will automatically move to "to" state after the timeout
type Transition ¶
type Transition struct { // StateMachine object StateMachine StateMachine // From which state transition is happening From State // TO which state transition is happening To State // Arguments passed during the transition // which will be passed to callback function Params []interface{} }
Transition defines the transition for the callback which will be passed to the call back function during the transition