Documentation
¶
Overview ¶
Package statechart provides a minimal, generic statechart implementation (~200 LOC) supporting hierarchical states, parallel states, history states, guards, and actions.
Index ¶
- type Action
- type Context
- type EventType
- type Guard
- type Runtime
- func (r *Runtime[S, E, C]) AddState(id StateID[S], stateType StateType, parent StateID[S]) *State[S, E, C]
- func (r *Runtime[S, E, C]) AddTransition(from, to StateID[S], event EventType[E], guard Guard[S, E, C], ...)
- func (r *Runtime[S, E, C]) GetContext() *Context[C]
- func (r *Runtime[S, E, C]) GetCurrentState() StateID[S]
- func (r *Runtime[S, E, C]) IsInState(id StateID[S]) bool
- func (r *Runtime[S, E, C]) SendEvent(ctx context.Context, event EventType[E]) error
- func (r *Runtime[S, E, C]) SetInitialState(id StateID[S]) error
- func (r *Runtime[S, E, C]) Start(ctx context.Context) error
- func (r *Runtime[S, E, C]) Stop(ctx context.Context) error
- type State
- type StateID
- type StateType
- type Transition
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Action ¶
type Action[S comparable, E comparable, C any] func(ctx context.Context, from, to StateID[S], event EventType[E], context *Context[C]) error
type EventType ¶
type EventType[E comparable] struct{ Value E }
type Guard ¶
type Guard[S comparable, E comparable, C any] func(ctx context.Context, from StateID[S], event EventType[E], context *Context[C]) bool
Function types for guards and actions
type Runtime ¶
type Runtime[S comparable, E comparable, C any] struct { // contains filtered or unexported fields }
Runtime represents the statechart runtime engine
func NewRuntime ¶
func NewRuntime[S comparable, E comparable, C any](initialContext Context[C]) *Runtime[S, E, C]
NewRuntime creates a new statechart runtime
func (*Runtime[S, E, C]) AddState ¶
func (r *Runtime[S, E, C]) AddState(id StateID[S], stateType StateType, parent StateID[S]) *State[S, E, C]
AddState adds a state to the statechart
func (*Runtime[S, E, C]) AddTransition ¶
func (r *Runtime[S, E, C]) AddTransition(from, to StateID[S], event EventType[E], guard Guard[S, E, C], action Action[S, E, C])
AddTransition adds a transition between states
func (*Runtime[S, E, C]) GetContext ¶
GetContext returns the current context
func (*Runtime[S, E, C]) GetCurrentState ¶
GetCurrentState returns the current state ID
func (*Runtime[S, E, C]) IsInState ¶
IsInState checks if the statechart is currently in the specified state (including ancestors)
func (*Runtime[S, E, C]) SetInitialState ¶
SetInitialState sets the initial state
type State ¶
type State[S comparable, E comparable, C any] struct { ID StateID[S] Type StateType Parent *State[S, E, C] Children []*State[S, E, C] OnEntry Action[S, E, C] OnExit Action[S, E, C] LastChild *State[S, E, C] // For history states }
State represents a state in the statechart
type Transition ¶
type Transition[S comparable, E comparable, C any] struct { From StateID[S] To StateID[S] Event EventType[E] Guard Guard[S, E, C] Action Action[S, E, C] }
Transition represents a state transition