Documentation
¶
Index ¶
- Variables
- type Action
- type ActionError
- type ActionFunc
- type Event
- type EventError
- type FSM
- func (f *FSM[T]) AddTransition(from, to State, event Event, actions []Action[T])
- func (f *FSM[T]) GetState() State
- func (f *FSM[T]) RegisterState(state State) error
- func (f *FSM[T]) SetErrorHandler(errorState State, handler ActionFunc[T])
- func (f *FSM[T]) SetLogLevel(level zerolog.Level)
- func (f *FSM[T]) SetState(s State)
- func (f *FSM[T]) Trigger(ctx context.Context, event Event, args *T) (*T, error)
- type FSMOptionFunc
- type FSMOptions
- type State
- type StateError
- type States
- type Transition
- type TransitionError
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidState = errors.New("invalid state") ErrInvalidEvent = errors.New("invalid event") ErrNoTransition = errors.New("no transition registered for state and event") ErrStateNotRegistered = errors.New("state not registered") ErrStateAlreadyExists = errors.New("state already exists") ErrStateSizeExceeded = errors.New("maximum number of states exceeded") )
FSM operation errors
var ( ErrActionNil = errors.New("action function is nil") ErrActionFailed = errors.New("action execution failed") ErrNoActionDefined = errors.New("no action function defined") )
Action errors
var ( ErrTransitionFailed = errors.New("state transition failed") ErrInvalidTransition = errors.New("invalid state transition") ErrTransitionAlreadyExists = errors.New("transition already exists") )
State transition errors
var ( ErrFSMNotInitialized = errors.New("FSM not initialized") ErrFSMAlreadyRunning = errors.New("FSM already running") ErrFSMStopped = errors.New("FSM has been stopped") )
FSM lifecycle errors
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action[T any] struct { Name string Fn ActionFunc[T] }
Action that can be executed during a state transition.
type ActionError ¶
func (*ActionError) Error ¶
func (e *ActionError) Error() string
func (*ActionError) Unwrap ¶
func (e *ActionError) Unwrap() error
type ActionFunc ¶
ActionFunc is a function that performs an action during a state transition.
type EventError ¶
func (*EventError) Error ¶
func (e *EventError) Error() string
func (*EventError) Unwrap ¶
func (e *EventError) Unwrap() error
type FSM ¶
type FSM[T any] struct { FSMOptions // contains filtered or unexported fields }
FSM is the Finite State Machine
func New ¶
func New[T any](initialState State, options ...FSMOptionFunc) *FSM[T]
New creates a new FSM instance
func (*FSM[T]) AddTransition ¶
AddTransition registers a new transition in the FSM from one state to another on a given event.
func (*FSM[T]) RegisterState ¶
RegisterState adds a new state to the FSM.
func (*FSM[T]) SetErrorHandler ¶
func (f *FSM[T]) SetErrorHandler(errorState State, handler ActionFunc[T])
SetErrorHandler configures an error handler and error state. When a transition error occurs, the error handler will be called and the FSM will transition to the error state.
func (*FSM[T]) SetLogLevel ¶
SetLogLevel updates the log level at runtime.
func (*FSM[T]) SetState ¶
SetState sets the current state of the FSM.
WARN: This bypasses the normal transition mechanism.
func (*FSM[T]) Trigger ¶
Trigger attempts to transition the FSM to a new state based on the given event.
Returns an error if no transition is registered for the current state or event, or if the action fails. If an error occurs and an error handler is configured, it will be called and the FSM will transition to the error state before returning the error.
type FSMOptionFunc ¶
type FSMOptionFunc func(*FSMOptions)
func WithLogConsole ¶
func WithLogConsole() FSMOptionFunc
WithLogConsole switches the FSM logger to human-friendly console output.
func WithLogLevel ¶
func WithLogLevel(level zerolog.Level) FSMOptionFunc
WithLogLevel sets the log level for the FSM.
func WithLogOutput ¶
func WithLogOutput(w io.Writer) FSMOptionFunc
WithLogOutput sets the writer where logs will be written.
func WithMaxStates ¶
func WithMaxStates(max int) FSMOptionFunc
WithMaxStates sets the maximum number of states allowed in the FSM.
type FSMOptions ¶
type FSMOptions struct {
LogLevel zerolog.Level
LogOutput io.Writer
UseStdOut bool
// contains filtered or unexported fields
}
FSMOptions holds configuration options for the FSM.
func DefaultOptions ¶
func DefaultOptions() FSMOptions
DefaultOptions returns the default FSM configuration.
type StateError ¶
func (*StateError) Error ¶
func (e *StateError) Error() string
func (*StateError) Unwrap ¶
func (e *StateError) Unwrap() error
type States ¶
type States struct {
// contains filtered or unexported fields
}
States manages a collection of unique states.
type Transition ¶
Transition triggered by an event.
type TransitionError ¶
func (*TransitionError) Error ¶
func (e *TransitionError) Error() string
func (*TransitionError) Unwrap ¶
func (e *TransitionError) Unwrap() error