Version: v0.0.0-...-a69e935 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2019 License: Apache-2.0 Imports: 6 Imported by: 0




This section is empty.


View Source
var (
	// ErrNoOpTransition is returned when the transition is a no-op
	ErrNoOpTransition = errors.Errorf("no-op transition")


This section is empty.


type Builder

type Builder struct {
	// contains filtered or unexported fields

Builder is the state machine builder

func NewBuilder

func NewBuilder() *Builder

NewBuilder creates new state machine builder

func (*Builder) AddRule

func (b *Builder) AddRule(rule *Rule) *Builder

AddRule adds the rule for state machine

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

func (b *Builder) WithCurrentState(current State) *Builder

WithCurrentState adds the current state

func (*Builder) WithName

func (b *Builder) WithName(name string) *Builder

WithName adds the name to state machine

func (*Builder) WithTransitionCallback

func (b *Builder) WithTransitionCallback(callback Callback) *Builder

WithTransitionCallback adds the transition call back

type Callback

type Callback func(*Transition) error

Callback is the type for callback function

type Option

type Option func(*statemachine)

Option is the option passed to TransitTo to trigger state machine transition

func WithInfo

func WithInfo(key string, value string) Option

WithInfo is an option passed in TransitTo to have store any meta infor about state machine

func WithReason

func WithReason(reason string) Option

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 State

type State string

State defines the 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

	// 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

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL