Documentation
¶
Index ¶
- Constants
- Variables
- func UseLogger(logger btclog.Logger)
- type Action
- type CachedObserver
- type ErrConfigError
- type ErrWaitingForStateTimeout
- type EventContext
- type EventType
- type ExampleFSM
- type ExampleService
- type ExampleStore
- type FixedSizeSlice
- type InitStuffRequest
- type Notification
- type Observer
- type State
- type StateMachine
- type StateType
- type States
- type Transitions
Constants ¶
const ( InitFSM = StateType("InitFSM") StuffSentOut = StateType("StuffSentOut") WaitingForStuff = StateType("WaitingForStuff") StuffFailed = StateType("StuffFailed") StuffSuccess = StateType("StuffSuccess") )
States.
const ( // Default represents the default state of the system. Default StateType = "" // NoOp represents a no-op event. NoOp EventType = "NoOp" // OnError can be used when an action returns a generic error. OnError EventType = "OnError" // ContextValidationFailed can be when the passed context if // not of the expected type. ContextValidationFailed EventType = "ContextValidationFailed" )
const Subsystem = "FSM"
Subsystem defines the sub system name of this package.
Variables ¶
var ( OnRequestStuff = EventType("OnRequestStuff") OnStuffSentOut = EventType("OnStuffSentOut") OnStuffSuccess = EventType("OnStuffSuccess") )
Events.
var ( ErrEventRejected = errors.New("event rejected") ErrWaitForStateTimedOut = errors.New( "timed out while waiting for event", ) ErrInvalidContextType = errors.New("invalid context") )
ErrEventRejected is the error returned when the state machine cannot process an event in the state that it is in.
Functions ¶
Types ¶
type Action ¶
type Action func(eventCtx EventContext) EventType
Action represents the action to be executed in a given state.
type CachedObserver ¶
type CachedObserver struct {
// contains filtered or unexported fields
}
CachedObserver is an observer that caches all states and transitions of the observed state machine.
func NewCachedObserver ¶
func NewCachedObserver(maxElements int) *CachedObserver
NewCachedObserver creates a new cached observer with the given maximum number of cached notifications.
func (*CachedObserver) GetCachedNotifications ¶
func (c *CachedObserver) GetCachedNotifications() []Notification
GetCachedNotifications returns a copy of the cached notifications.
func (*CachedObserver) Notify ¶
func (c *CachedObserver) Notify(notification Notification)
Notify implements the Observer interface.
func (*CachedObserver) WaitForState ¶
func (s *CachedObserver) WaitForState(ctx context.Context, timeout time.Duration, state StateType) error
WaitForState waits for the state machine to reach the given state.
type ErrConfigError ¶
type ErrConfigError error
ErrConfigError is an error returned when the state machine is misconfigured.
func NewErrConfigError ¶
func NewErrConfigError(msg string) ErrConfigError
NewErrConfigError creates a new ErrConfigError.
type ErrWaitingForStateTimeout ¶
type ErrWaitingForStateTimeout error
ErrWaitingForStateTimeout is an error returned when the state machine times out while waiting for a state.
func NewErrWaitingForStateTimeout ¶
func NewErrWaitingForStateTimeout(expected, actual StateType) ErrWaitingForStateTimeout
NewErrWaitingForStateTimeout creates a new ErrWaitingForStateTimeout.
type EventContext ¶
type EventContext interface{}
EventContext represents the context to be passed to the action implementation.
type EventType ¶
type EventType string
EventType represents an extensible event type in the state machine.
func NoOpAction ¶
func NoOpAction(_ EventContext) EventType
NoOpAction is a no-op action that can be used by states that don't need to execute any action.
type ExampleFSM ¶
type ExampleFSM struct { *StateMachine // contains filtered or unexported fields }
ExampleFSM implements the FSM and uses the ExampleService and ExampleStore to implement the actions.
func NewExampleFSMContext ¶
func NewExampleFSMContext(service ExampleService, store ExampleStore) *ExampleFSM
NewExampleFSMContext creates a new example FSM context.
func (*ExampleFSM) GetStates ¶
func (e *ExampleFSM) GetStates() States
GetStates returns the states for the example FSM.
type ExampleService ¶
ExampleService is an example service that we want to wait for in the FSM.
type ExampleStore ¶
type ExampleStore interface {
StoreStuff() error
}
ExampleStore is an example store that we want to use in our exitFunc.
type FixedSizeSlice ¶
FixedSizeSlice is a slice with a fixed size.
func NewFixedSizeSlice ¶
func NewFixedSizeSlice[T any](maxLen int) *FixedSizeSlice[T]
NewFixedSlice initializes a new FixedSlice with a given maximum length.
func (*FixedSizeSlice[T]) Add ¶
func (fs *FixedSizeSlice[T]) Add(element T)
Add appends a new element to the slice. If the slice reaches its maximum length, the first element is removed.
func (*FixedSizeSlice[T]) Get ¶
func (fs *FixedSizeSlice[T]) Get() []T
Get returns a copy of the slice.
func (*FixedSizeSlice[T]) GetElement ¶
func (fs *FixedSizeSlice[T]) GetElement(index int) T
GetElement returns the element at the given index.
type InitStuffRequest ¶
type InitStuffRequest struct { Stuff string // contains filtered or unexported fields }
InitStuffRequest is the event context for the InitFSM state.
type Notification ¶
type Notification struct { // PreviousState is the state the state machine was in before the event // was processed. PreviousState StateType // NextState is the state the state machine is in after the event was // processed. NextState StateType // Event is the event that was processed. Event EventType }
Notification represents a notification sent to the state machine's notification channel.
type Observer ¶
type Observer interface {
Notify(Notification)
}
Observer is an interface that can be implemented by types that want to observe the state machine.
type State ¶
type State struct { // EntryFunc is a function that is called when the state is entered. EntryFunc func() // ExitFunc is a function that is called when the state is exited. ExitFunc func() // Action is the action to be executed in the state. Action Action // Transitions is a mapping of events and states. Transitions Transitions }
State binds a state with an action and a set of events it can handle.
type StateMachine ¶
type StateMachine struct { // Context represents the state machine context. States States // ActionEntryFunc is a function that is called before an action is // executed. ActionEntryFunc func() // ActionExitFunc is a function that is called after an action is // executed. ActionExitFunc func() // LastActionError is an error set by the last action executed. LastActionError error // contains filtered or unexported fields }
StateMachine represents the state machine.
func NewStateMachine ¶
func NewStateMachine(states States) *StateMachine
NewStateMachine creates a new state machine.
func (*StateMachine) HandleError ¶
func (s *StateMachine) HandleError(err error) EventType
HandleError is a helper function that can be used by actions to handle errors.
func (*StateMachine) RegisterObserver ¶
func (s *StateMachine) RegisterObserver(observer Observer)
RegisterObserver registers an observer with the state machine.
func (*StateMachine) RemoveObserver ¶
func (s *StateMachine) RemoveObserver(observer Observer) bool
RemoveObserver removes an observer from the state machine. It returns true if the observer was removed, false otherwise.
func (*StateMachine) SendEvent ¶
func (s *StateMachine) SendEvent(event EventType, eventCtx EventContext) error
SendEvent sends an event to the state machine. It returns an error if the event cannot be processed in the current state. Otherwise, it only returns nil if the event for the last action is a no-op.
type StateType ¶
type StateType string
StateType represents an extensible state type in the state machine.
type Transitions ¶
Transitions represents a mapping of events and states.