statemachine

package
v1.6.3 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package statemachine provides a generic finite state machine with callbacks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(resourceID, from, to string)

Callback is called when a state transition occurs.

type Machine

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

Machine is a generic finite state machine.

func New

func New(transitions []Transition) *Machine

New creates a new Machine with the given legal transitions.

func (*Machine) GetState

func (m *Machine) GetState(resourceID string) (string, error)

GetState returns the current state of a resource.

func (*Machine) OnTransition

func (m *Machine) OnTransition(cb Callback)

OnTransition registers a callback for state transitions.

func (*Machine) Remove

func (m *Machine) Remove(resourceID string)

Remove removes a resource from the state machine.

func (*Machine) Resources

func (m *Machine) Resources() map[string]string

Resources returns all resource IDs and their states.

func (*Machine) SetState

func (m *Machine) SetState(resourceID, state string)

SetState sets the initial state for a resource.

func (*Machine) String

func (m *Machine) String(resourceID string) string

String returns the state of a specific resource for debugging.

func (*Machine) Transition

func (m *Machine) Transition(resourceID, to string) error

Transition attempts to transition a resource to a new state.

type Transition

type Transition struct {
	From string
	To   string
}

Transition defines a legal state transition.

type TransitionMap

type TransitionMap map[string][]string

TransitionMap holds legal transitions indexed by "from" state.

func NewTransitionMap

func NewTransitionMap(transitions []Transition) TransitionMap

NewTransitionMap creates a TransitionMap from a list of transitions.

func (TransitionMap) IsAllowed

func (tm TransitionMap) IsAllowed(from, to string) bool

IsAllowed checks if a transition from one state to another is legal.

Jump to

Keyboard shortcuts

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