machine

package
v0.9.9-rc8 Latest Latest
Warning

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

Go to latest
Published: May 28, 2024 License: MIT Imports: 6 Imported by: 0

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

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.

func (Action) String

func (a Action) String() string

String returns the string representation of the Action.

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.

func (State) String

func (s State) String() string

String returns the string representation of the State.

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.

Jump to

Keyboard shortcuts

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