fsm

package
v1.11.0-beta.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEventRejected = errors.New("event rejected")

ErrEventRejected is the error returned when the state machine cannot process an event in the state that it is in.

Functions

This section is empty.

Types

type Action

type Action func(ctx context.Context) (Event, error)

Action represents the action to be executed in a given state.

type Event

type Event string

Event represents an extensible event type in the state machine.

const (
	// NoOp represents a no-op event. State machine stops when this event is emitted.
	NoOp Event = "NoOp"
)

type Events

type Events map[Event]State

Events represents a mapping of events and states.

type Hook

type Hook func(ctx context.Context, currentState, nextState State, event Event) error

Hook is called on each state machine transition.

type State

type State string

State represents an extensible state type in the state machine.

type StateMachine

type StateMachine struct {
	// contains filtered or unexported fields
}

StateMachine represents the state machine.

func New

func New(state State, stateTransitions StateTransitions, hook Hook) *StateMachine

New returns initialized state machine.

func (*StateMachine) Current

func (s *StateMachine) Current() State

Current return current state machine state.

func (*StateMachine) Transition

func (s *StateMachine) Transition(ctx context.Context) error

Transition triggers current state action and sends event to the state machine.

type StateTransitions

type StateTransitions map[State]Transition

StateTransitions represents a mapping of states and their implementations.

type Transition

type Transition struct {
	Action Action
	Events Events
}

Transition binds a state with an action and a set of events it can handle.

Jump to

Keyboard shortcuts

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