fsm

package
v0.0.0-...-1d93c82 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 4 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(*FSM)

Callback is a function type that callbacks should use. Event is the current event info as the callback happenf.

type Callbacks

type Callbacks struct {
	// contains filtered or unexported fields
}

Callbaks is a shorthand for defining the callbacks in FSM

type Export

type Export struct {
	// current is the state that the FSM is currently in.
	State State

	// transitions maps events and source states to destination statef.
	Transitions map[State]TransitionRuleSet

	Events map[State]State

	// callbacks maps events and source states to destination statef.
	Callbacks Callbacks
}

Export this FSM type for export data

type FSM

type FSM struct {
	// contains filtered or unexported fields
}

FSM is the state machine that holds the current state.

func New

func New() (*FSM, error)

Create a new FSM with empty setup

func (*FSM) AddCallback

func (f *FSM) AddCallback(state State, typeCb string, cb func(*FSM)) error

AddCallback is a function for adding callback function by event

func (*FSM) AddEvent

func (f *FSM) AddEvent(name State, dst State) error

AddEvent is a function for adding event

func (*FSM) AddStateTransitionRules

func (f *FSM) AddStateTransitionRules(src State, dst ...State) error

AddStateTransitionRules is a function for adding valid state transitions to the machine. This allows you to define whicj states any given state can be transitioned to.

func (*FSM) Export

func (f *FSM) Export() Export

func (*FSM) ExportFunc

func (f *FSM) ExportFunc(exec func(Export, interface{}) interface{}, args interface{}) interface{}

func (*FSM) GetCurrentState

func (f *FSM) GetCurrentState() State

GetCurrentState returns the current state. If the State returned is "", then the machine has not been given an initial state.

func (*FSM) Import

func (f *FSM) Import(data Export) error

func (*FSM) MarshalJSON

func (f *FSM) MarshalJSON() ([]byte, error)

func (*FSM) SendEvent

func (f *FSM) SendEvent(event State) error

func (*FSM) SetStateTransition

func (f *FSM) SetStateTransition(toState State) error

SetStateTransition triggers a transition to the toState. This function is also used to set the initial stte of machine.

Before you can transition to any state, even for the initial, you stateMust define it with AddStateTransitionRules(). If you are setting the initial state, and that state is not define, this will return an ErrInvalidInitialState error.

When transitiong 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 (*FSM) UnmarshalJSON

func (f *FSM) UnmarshalJSON(data []byte) error

type Init

type Init interface {
	Export() string
	Import(FSM) error
	// TODO: add work with channels (input/output)
	Watch() chan<- interface{}
	Listen(<-chan interface{})
}

Import/Export TODO: use DI instead struct Export

type State

type State string

State is the machine

type TransitionRuleSet

type TransitionRuleSet map[State]struct{}

TransitionRuleSet is a set of allowed transitionf. This uses map of struct{} to implement a set.

Jump to

Keyboard shortcuts

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