Documentation ¶
Overview ¶
Package machine provides a flexible and robust state machine implementation for managing states and transitions in applications, particularly useful in scenarios involving Ethereum smart contract interactions. It leverages Go's concurrency features and is designed with thread-safety and scalability in mind.
Index ¶
- type Action
- type Data
- type Handler
- type State
- type StateMachine
- func (sm *StateMachine) Destroy()
- func (sm *StateMachine) GetCurrentState() State
- func (sm *StateMachine) GetHistory() []State
- func (sm *StateMachine) GetProcessedStates() []State
- func (sm *StateMachine) Pause()
- func (sm *StateMachine) Process() error
- func (sm *StateMachine) RegisterErrorState(state State) error
- func (sm *StateMachine) RegisterFinalState(state State) error
- func (sm *StateMachine) RegisterState(state State, handler Handler)
- func (sm *StateMachine) RegisterTransition(from State, action Action, to State) error
- func (sm *StateMachine) Resume()
- func (sm *StateMachine) Timeout(duration time.Duration)
- func (sm *StateMachine) Trigger(action Action) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action string
Action represents an action that triggers state transitions in the state machine.
type Data ¶
type Data any
Data represents the data carried through the state machine. It can be of any type, allowing for flexibility in what information is passed between states.
type Handler ¶
type Handler struct { Enter func(Data) (Data, error) // Function executed when entering a state. Process func(Data) (State, Data, error) // Function executed when processing a state. Exit func(Data) (Data, error) // Function executed when exiting a state. }
Handler defines the functions to execute when entering, processing, and exiting a state. Each function can modify the machine's data and optionally change the state.
type State ¶
type State string
State represents a state in the state machine. It is a simple string type to facilitate easy identification and comparison of different states.
type StateMachine ¶
type StateMachine struct {
// contains filtered or unexported fields
}
StateMachine represents the state machine itself. It manages the current state, the available states and their handlers, and the state transitions.
func NewStateMachine ¶
func NewStateMachine(ctx context.Context, initialState State, data Data) *StateMachine
NewStateMachine creates and returns a new StateMachine with the specified initial state and data.
func (*StateMachine) Destroy ¶
func (sm *StateMachine) Destroy()
Destroy cleans up the state machine, removing all states, transitions, and data. It essentially resets the machine to its initial state.
func (*StateMachine) GetCurrentState ¶
func (sm *StateMachine) GetCurrentState() State
GetCurrentState returns the current state of the state machine.
func (*StateMachine) GetHistory ¶
func (sm *StateMachine) GetHistory() []State
GetHistory returns the history of all the states the machine has been in.
func (*StateMachine) GetProcessedStates ¶
func (sm *StateMachine) GetProcessedStates() []State
GetProcessedStates returns a slice of all states that have been processed so far.
func (*StateMachine) Pause ¶
func (sm *StateMachine) Pause()
Pause pauses the state machine process. No new transitions or state processing will occur while paused.
func (*StateMachine) Process ¶
func (sm *StateMachine) Process() error
Process runs the state machine process, handling transitions and state processing based on the defined rules and transitions.
func (*StateMachine) RegisterErrorState ¶
func (sm *StateMachine) RegisterErrorState(state State) error
RegisterErrorState sets the error state of the state machine. This state is used when an error occurs in any state transition or processing.
func (*StateMachine) RegisterFinalState ¶
func (sm *StateMachine) RegisterFinalState(state State) error
RegisterFinalState sets the final state of the state machine. Reaching this state means the machine's process is complete.
func (*StateMachine) RegisterState ¶
func (sm *StateMachine) RegisterState(state State, handler Handler)
RegisterState adds a new state and its associated handler to the state machine.
func (*StateMachine) RegisterTransition ¶
func (sm *StateMachine) RegisterTransition(from State, action Action, to State) error
RegisterTransition defines a transition from one state to another based on a given action.
func (*StateMachine) Resume ¶
func (sm *StateMachine) Resume()
Resume resumes the state machine process after being paused.
func (*StateMachine) Timeout ¶
func (sm *StateMachine) Timeout(duration time.Duration)
Timeout introduces a delay in the state machine processing, useful for testing and simulating time-based scenarios.
func (*StateMachine) Trigger ¶
func (sm *StateMachine) Trigger(action Action) error
Trigger initiates the transition from the current state to the next state based on the provided action.