Documentation ¶
Index ¶
- type Event
- type FSM
- func (f *FSM) AddTransitionRule(from State, event Event, to State)
- func (f *FSM) GetCurrentState() State
- func (f *FSM) SetOnEnterState(callback func(ctx context.Context, from State, to State, event Event))
- func (f *FSM) SetOnExitState(callback func(ctx context.Context, from State, to State, event Event))
- func (f *FSM) TriggerEvent(ctx context.Context, event Event) error
- type State
- type TransitionRuleSet
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FSM ¶
type FSM struct { CurrentState State TransitionRules TransitionRuleSet // Callbacks triggered on state transitions. OnEnterState func(ctx context.Context, from State, to State, event Event) OnExitState func(ctx context.Context, from State, to State, event Event) // contains filtered or unexported fields }
FSM represents a finite state machine.
func New ¶
New creates a new FSM with the given initial state. It initializes the TransitionRules to prevent nil map assignments.
func (*FSM) AddTransitionRule ¶
AddTransitionRule adds a transition rule to the FSM. It defines that when in the 'from' state, upon receiving 'event', the FSM should transition to the 'to' state.
func (*FSM) GetCurrentState ¶
GetCurrentState returns the current state of the FSM. It uses a read lock to allow concurrent reads.
func (*FSM) SetOnEnterState ¶
func (f *FSM) SetOnEnterState(callback func(ctx context.Context, from State, to State, event Event))
SetOnEnterState sets the callback function to be called when entering a new state. The callback receives a context, the previous state, the new state, and the triggering event.
func (*FSM) SetOnExitState ¶
SetOnExitState sets the callback function to be called when exiting a state. The callback receives a context, the previous state, the new state, and the triggering event.
func (*FSM) TriggerEvent ¶
TriggerEvent triggers an event and attempts to transition the FSM to the next state. It accepts a context for handling cancellation, deadlines, and passing request-scoped values. It returns an error if the transition is invalid.
type TransitionRuleSet ¶
TransitionRuleSet defines the allowed state transitions. It maps a State to a map of Events and their resulting States.