fsm

package module
v0.0.0-...-88244d5 Latest Latest
Warning

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

Go to latest
Published: Apr 27, 2025 License: MIT Imports: 4 Imported by: 0

README

FSM-Go: A Lightweight Finite State Machine for Go

Go Reference Go Report Card

FSM-Go is a lightweight, high-performance, stateless finite state machine implementation in Go, inspired by Alibaba's COLA state machine component.

中文文档

✨ Features

  • 🪶 Lightweight - Minimal, stateless design for high performance
  • 🔒 Type-safe - Built with Go generics for compile-time type checking
  • 🔄 Fluent API - Intuitive builder pattern for defining state machines
  • 🔀 Versatile Transitions - Support for external, internal, and parallel transitions
  • 🧪 Conditional Logic - Flexible conditions to control when transitions occur
  • 🎬 Action Execution - Custom actions that execute during transitions
  • 🔄 Thread-safe - Designed for concurrent use in multi-threaded environments
  • 📊 Visualization - Built-in support for generating state machine diagrams

📦 Installation

go get github.com/lingcoder/fsm-go

🚀 Usage

package main

import (
	"fmt"
	"log"

	"github.com/lingcoder/fsm-go"
)

// Define states
type OrderState string

const (
	OrderCreated   OrderState = "CREATED"
	OrderPaid      OrderState = "PAID"
	OrderShipped   OrderState = "SHIPPED"
	OrderDelivered OrderState = "DELIVERED"
	OrderCancelled OrderState = "CANCELLED"
)

// Define events
type OrderEvent string

const (
	EventPay     OrderEvent = "PAY"
	EventShip    OrderEvent = "SHIP"
	EventDeliver OrderEvent = "DELIVER"
	EventCancel  OrderEvent = "CANCEL"
)

// Define payload
type OrderPayload struct {
	OrderID string
	Amount  float64
}

func main() {
	// Create a builder
	builder := fsm.NewStateMachineBuilder[OrderState, OrderEvent, OrderPayload]()

	// Define the state machine
	builder.ExternalTransition().
		From(OrderCreated).
		To(OrderPaid).
		On(EventPay).
		WhenFunc(func(payload OrderPayload) bool {
			// Check if amount is valid
			return payload.Amount > 0
		}).
		PerformFunc(func(from, to OrderState, event OrderEvent, payload OrderPayload) error {
			fmt.Printf("Order %s transitioning from %s to %s on event %s\n",
				payload.OrderID, from, to, event)
			return nil
		})

	builder.ExternalTransition().
		From(OrderPaid).
		To(OrderShipped).
		On(EventShip).
		WhenFunc(func(payload OrderPayload) bool {
			return true
		}).
		PerformFunc(func(from, to OrderState, event OrderEvent, payload OrderPayload) error {
			fmt.Printf("Order %s is being shipped\n", payload.OrderID)
			return nil
		})

	// Define multiple source transitions
	builder.ExternalTransitions().
		FromAmong(OrderCreated, OrderPaid, OrderShipped).
		To(OrderCancelled).
		On(EventCancel).
		WhenFunc(func(payload OrderPayload) bool {
			return true
		}).
		PerformFunc(func(from, to OrderState, event OrderEvent, payload OrderPayload) error {
			fmt.Printf("Order %s cancelled from %s state\n", payload.OrderID, from)
			return nil
		})

	// Build the state machine
	stateMachine, err := builder.Build("OrderStateMachine")
	if err != nil {
		log.Fatalf("Failed to build state machine: %v", err)
	}

	// Create payload
	payload := OrderPayload{
		OrderID: "ORD-20250425-001",
		Amount:  100.0,
	}

	// Transition from CREATED to PAID
	newState, err := stateMachine.FireEvent(OrderCreated, EventPay, payload)
	if err != nil {
		log.Fatalf("Transition failed: %v", err)
	}

	fmt.Printf("New state: %v\n", newState)
}

🧩 Core Concepts

Concept Description
State Represents a specific state in your business process
Event Triggers state transitions
Transition Defines how states change in response to events
Condition Logic that determines if a transition should occur
Action Logic executed when a transition occurs
StateMachine The core component that manages states and transitions
Types of Transitions
  • External Transition: Transition between different states
  • Internal Transition: Actions within the same state
  • Parallel Transition: Transition to multiple states simultaneously

📚 Examples

Check the examples directory for more detailed examples:

  • examples/order: Order processing workflow
  • examples/workflow: Approval workflow
  • examples/game: Game state management

⚡ Performance

FSM-Go is designed for high performance:

  • Stateless design minimizes memory usage
  • Efficient transition lookup
  • Thread-safe for concurrent use
  • Benchmarks included in the test suite

📊 Visualization

FSM-Go provides a unified way to visualize your state machine with different formats:

// Default format (PlantUML)
plantUML := stateMachine.GenerateDiagram()
fmt.Println(plantUML)

// Generate specific format
table := stateMachine.GenerateDiagram(fsm.MarkdownTable)     // Markdown table format
fmt.Println(table)

flow := stateMachine.GenerateDiagram(fsm.MarkdownFlowchart)  // Markdown flowchart format
fmt.Println(flow)

// Generate multiple formats separately
diagrams := stateMachine.GenerateDiagram(fsm.PlantUML, fsm.MarkdownTable, fsm.MarkdownFlowchart, fsm.MarkdownStateDiagram)
fmt.Println(diagrams)

📄 License

MIT © LingCoder

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStateMachineNotFound     = errors.New("state machine not found")
	ErrStateMachineAlreadyExist = errors.New("state machine already exists")
	ErrStateNotFound            = errors.New("state not found")
	ErrTransitionNotFound       = errors.New("no transition found")
	ErrConditionNotMet          = errors.New("transition conditions not met")
	ErrActionExecutionFailed    = errors.New("action execution failed")
	ErrStateMachineNotReady     = errors.New("state machine is not ready yet")
	ErrInternalTransition       = errors.New("internal transition source and target states must be the same")
)

Error constants - standard error definitions

Functions

func ListStateMachines

func ListStateMachines() []string

ListStateMachines returns a list of all registered state machine IDs Returns:

Slice of state machine IDs

func RegisterStateMachine

func RegisterStateMachine[S comparable, E comparable, P any](machineId string, stateMachine StateMachine[S, E, P]) error

RegisterStateMachine adds a state machine to the registry Parameters:

machineId: Unique identifier for the state machine
stateMachine: State machine instance to register

Returns:

Error if a state machine with the same ID already exists

func RemoveStateMachine

func RemoveStateMachine(machineId string) bool

RemoveStateMachine removes a state machine from the registry Parameters:

machineId: Unique identifier for the state machine to remove

Returns:

True if the state machine was found and removed, false otherwise

Types

type Action

type Action[S comparable, E comparable, P any] interface {
	// Execute runs the action during a state transition
	Execute(from, to S, event E, payload P) error
}

Action is an interface for transition actions

type ActionFunc

type ActionFunc[S comparable, E comparable, P any] func(from, to S, event E, payload P) error

ActionFunc is a function type that implements Action interface

func (ActionFunc[S, E, P]) Execute

func (f ActionFunc[S, E, P]) Execute(from, to S, event E, payload P) error

Execute implements Action interface

type Condition

type Condition[P any] interface {
	// IsSatisfied returns true if the condition is met
	IsSatisfied(payload P) bool
}

Condition is an interface for transition conditions

type ConditionFunc

type ConditionFunc[P any] func(payload P) bool

ConditionFunc is a function type that implements Condition interface

func (ConditionFunc[P]) IsSatisfied

func (f ConditionFunc[P]) IsSatisfied(payload P) bool

IsSatisfied implements Condition interface

type DiagramFormat

type DiagramFormat int

DiagramFormat defines the supported diagram formats

const (
	// PlantUML format for UML diagrams
	PlantUML DiagramFormat = iota
	// MarkdownTable format for tabular representation
	MarkdownTable
	// MarkdownFlowchart format for flowcharts
	MarkdownFlowchart
	// MarkdownStateDiagram format for Mermaid state diagrams
	MarkdownStateDiagram
)

type ExternalParallelTransitionBuilder

type ExternalParallelTransitionBuilder[S comparable, E comparable, P any] struct {
	// contains filtered or unexported fields
}

ExternalParallelTransitionBuilder builds external parallel transitions

func (*ExternalParallelTransitionBuilder[S, E, P]) From

func (b *ExternalParallelTransitionBuilder[S, E, P]) From(state S) ParallelFromInterface[S, E, P]

From specifies the source state Parameters:

state: Source state

Returns:

The parallel from builder for method chaining

type ExternalParallelTransitionBuilderInterface

type ExternalParallelTransitionBuilderInterface[S comparable, E comparable, P any] interface {
	// From specifies the source state
	From(state S) ParallelFromInterface[S, E, P]
}

ExternalParallelTransitionBuilderInterface is the interface for building external parallel transitions

type ExternalTransitionBuilderInterface

type ExternalTransitionBuilderInterface[S comparable, E comparable, P any] interface {
	// From specifies the source state
	From(state S) FromInterface[S, E, P]
}

ExternalTransitionBuilderInterface is the interface for building external transitions

type ExternalTransitionsBuilder

type ExternalTransitionsBuilder[S comparable, E comparable, P any] struct {
	// contains filtered or unexported fields
}

ExternalTransitionsBuilder builds external transitions from multiple source states to a single target state

func (*ExternalTransitionsBuilder[S, E, P]) FromAmong

func (b *ExternalTransitionsBuilder[S, E, P]) FromAmong(states ...S) FromInterface[S, E, P]

FromAmong specifies multiple source states Parameters:

states: Multiple source states

Returns:

The from builder for method chaining

type ExternalTransitionsBuilderInterface

type ExternalTransitionsBuilderInterface[S comparable, E comparable, P any] interface {
	// FromAmong specifies multiple source states
	FromAmong(states ...S) FromInterface[S, E, P]
}

ExternalTransitionsBuilderInterface is the interface for building multiple external transitions

type FromBuilder

type FromBuilder[S comparable, E comparable, P any, Next any] struct {
	// contains filtered or unexported fields
}

FromBuilder builds the "from" part of multiple transitions

func (*FromBuilder[S, E, P, Next]) On

func (b *FromBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P]

On specifies the triggering event Parameters:

event: The event that triggers these transitions

Returns:

The from builder for method chaining

func (*FromBuilder[S, E, P, Next]) Perform

func (b *FromBuilder[S, E, P, Next]) Perform(action Action[S, E, P])

Perform specifies the action to execute during all transitions Parameters:

action: The action to execute when the transitions occur

func (*FromBuilder[S, E, P, Next]) PerformFunc

func (b *FromBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error)

PerformFunc specifies a function as the action to execute during all transitions Parameters:

actionFunc: The function to execute when the transitions occur

func (*FromBuilder[S, E, P, Next]) To

func (b *FromBuilder[S, E, P, Next]) To(state S) ToInterface[S, E, P]

To specifies the target state Parameters:

state: Target state

Returns:

The from builder for method chaining

func (*FromBuilder[S, E, P, Next]) When

func (b *FromBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P]

When specifies the condition for all transitions Parameters:

condition: The condition that must be satisfied for the transitions to occur

Returns:

The from builder for method chaining

func (*FromBuilder[S, E, P, Next]) WhenFunc

func (b *FromBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P]

WhenFunc specifies a function as the condition for all transitions Parameters:

conditionFunc: The function that must return true for the transitions to occur

Returns:

The from builder for method chaining

type FromInterface

type FromInterface[S comparable, E comparable, P any] interface {
	// To specifies the target state
	To(state S) ToInterface[S, E, P]
}

FromInterface is the interface for specifying the source state of a transition

type FromStep

type FromStep interface{}

FromStep Step marker interfaces to enforce the correct order of method calls

type InternalTransitionBuilder

type InternalTransitionBuilder[S comparable, E comparable, P any] struct {
	// contains filtered or unexported fields
}

InternalTransitionBuilder builds internal transitions

func (*InternalTransitionBuilder[S, E, P]) Within

func (b *InternalTransitionBuilder[S, E, P]) Within(state S) ToInterface[S, E, P]

Within specifies the state where the internal transition occurs Parameters:

state: The state where the internal transition occurs

Returns:

The internal transition builder for method chaining

type InternalTransitionBuilderInterface

type InternalTransitionBuilderInterface[S comparable, E comparable, P any] interface {
	// Within specifies the state where the internal transition occurs
	Within(state S) ToInterface[S, E, P]
}

InternalTransitionBuilderInterface is the interface for building internal transitions

type OnInterface

type OnInterface[S comparable, E comparable, P any] interface {
	// When specifies the condition for the transition
	When(condition Condition[P]) WhenInterface[S, E, P]

	// WhenFunc specifies a function as the condition for the transition
	WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P]
}

OnInterface is the interface for specifying the triggering event of a transition

type OnStep

type OnStep interface{}

type OnTransitionBuilder

type OnTransitionBuilder[S comparable, E comparable, P any, Next any] struct {
	// contains filtered or unexported fields
}

OnTransitionBuilder builds the "on" part of an internal transition

func (*OnTransitionBuilder[S, E, P, Next]) On

func (b *OnTransitionBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P]

On specifies the triggering event Parameters:

event: The event that triggers this transition

Returns:

The on transition builder for method chaining

func (*OnTransitionBuilder[S, E, P, Next]) Perform

func (b *OnTransitionBuilder[S, E, P, Next]) Perform(action Action[S, E, P])

Perform specifies the action to execute during the transition Parameters:

action: The action to execute when the transition occurs

func (*OnTransitionBuilder[S, E, P, Next]) PerformFunc

func (b *OnTransitionBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error)

PerformFunc specifies a function as the action to execute during the transition Parameters:

actionFunc: The function to execute when the transition occurs

func (*OnTransitionBuilder[S, E, P, Next]) To

func (b *OnTransitionBuilder[S, E, P, Next]) To(state S) ToInterface[S, E, P]

To specifies the target state Parameters:

state: Target state

Returns:

The on transition builder for method chaining

func (*OnTransitionBuilder[S, E, P, Next]) When

func (b *OnTransitionBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P]

When specifies the condition for the transition Parameters:

condition: The condition that must be satisfied for the transition to occur

Returns:

The on transition builder for method chaining

func (*OnTransitionBuilder[S, E, P, Next]) WhenFunc

func (b *OnTransitionBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P]

WhenFunc specifies a function as the condition for the transition Parameters:

conditionFunc: The function that must return true for the transition to occur

Returns:

The on transition builder for method chaining

type ParallelFromBuilder

type ParallelFromBuilder[S comparable, E comparable, P any, Next any] struct {
	// contains filtered or unexported fields
}

ParallelFromBuilder builds the "from" part of a parallel transition

func (*ParallelFromBuilder[S, E, P, Next]) On

func (b *ParallelFromBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P]

On specifies the triggering event Parameters:

event: The event that triggers these transitions

Returns:

The parallel from builder for method chaining

func (*ParallelFromBuilder[S, E, P, Next]) Perform

func (b *ParallelFromBuilder[S, E, P, Next]) Perform(action Action[S, E, P])

Perform specifies the action to execute during all transitions Parameters:

action: The action to execute when the transitions occur

func (*ParallelFromBuilder[S, E, P, Next]) PerformFunc

func (b *ParallelFromBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error)

PerformFunc specifies a function as the action to execute during all transitions Parameters:

actionFunc: The function to execute when the transitions occur

func (*ParallelFromBuilder[S, E, P, Next]) ToAmong

func (b *ParallelFromBuilder[S, E, P, Next]) ToAmong(states ...S) ToInterface[S, E, P]

ToAmong specifies multiple target states Parameters:

states: Multiple target states

Returns:

The parallel from builder for method chaining

func (*ParallelFromBuilder[S, E, P, Next]) When

func (b *ParallelFromBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P]

When specifies the condition for all transitions Parameters:

condition: The condition that must be satisfied for the transitions to occur

Returns:

The parallel from builder for method chaining

func (*ParallelFromBuilder[S, E, P, Next]) WhenFunc

func (b *ParallelFromBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P]

WhenFunc specifies a function as the condition for all transitions Parameters:

conditionFunc: The function that must return true for the transitions to occur

Returns:

The parallel from builder for method chaining

type ParallelFromInterface

type ParallelFromInterface[S comparable, E comparable, P any] interface {
	// ToAmong specifies multiple target states
	ToAmong(states ...S) ToInterface[S, E, P]
}

ParallelFromInterface is the interface for specifying the source state of a parallel transition

type PerformStep

type PerformStep interface{}

type State

type State[S comparable, E comparable, P any] struct {
	// contains filtered or unexported fields
}

State represents a state in the state machine

func NewState

func NewState[S comparable, E comparable, P any](id S) *State[S, E, P]

NewState creates a new state

func (*State[S, E, P]) AddParallelTransitions

func (s *State[S, E, P]) AddParallelTransitions(event E, targets []*State[S, E, P], transType TransitionType) []*Transition[S, E, P]

AddParallelTransitions adds multiple transitions for the same event to different target states

func (*State[S, E, P]) AddTransition

func (s *State[S, E, P]) AddTransition(event E, target *State[S, E, P], transType TransitionType) *Transition[S, E, P]

AddTransition adds a transition to this state

func (*State[S, E, P]) GetEventTransitions

func (s *State[S, E, P]) GetEventTransitions(event E) []*Transition[S, E, P]

GetEventTransitions returns all transitions for a given event

func (*State[S, E, P]) GetID

func (s *State[S, E, P]) GetID() S

GetID returns the state ID

type StateMachine

type StateMachine[S comparable, E comparable, P any] interface {
	// FireEvent triggers a state transition based on the current state and event
	// Returns the new state and any error that occurred
	FireEvent(sourceState S, event E, payload P) (S, error)

	// FireParallelEvent triggers parallel state transitions based on the current state and event
	// Returns a slice of new states and any error that occurred
	FireParallelEvent(sourceState S, event E, payload P) ([]S, error)

	// Verify checks if there is a valid transition for the given state and event
	// Returns true if a transition exists, false otherwise
	Verify(sourceState S, event E) bool

	// ShowStateMachine returns a string representation of the state machine
	ShowStateMachine() string

	// GenerateDiagram returns a diagram of the state machine in the specified formats
	// If formats is nil or empty, defaults to PlantUML
	// If multiple formats are provided, returns all requested formats concatenated
	GenerateDiagram(formats ...DiagramFormat) string
}

StateMachine is a generic state machine interface S: State type, must be comparable (e.g., string, int) E: Event type, must be comparable P: Payload type, can be any type, used to pass data during state transitions

func GetStateMachine

func GetStateMachine[S comparable, E comparable, P any](machineId string) (StateMachine[S, E, P], error)

GetStateMachine retrieves a state machine by ID Parameters:

machineId: Unique identifier for the state machine to retrieve

Returns:

The state machine instance and error if not found or type mismatch

type StateMachineBuilder

type StateMachineBuilder[S comparable, E comparable, P any] struct {
	// contains filtered or unexported fields
}

StateMachineBuilder builds state machines with a fluent API

func NewStateMachineBuilder

func NewStateMachineBuilder[S comparable, E comparable, P any]() *StateMachineBuilder[S, E, P]

NewStateMachineBuilder creates a new builder Returns:

A new state machine builder instance

func (*StateMachineBuilder[S, E, P]) Build

func (b *StateMachineBuilder[S, E, P]) Build(machineId string) (StateMachine[S, E, P], error)

Build finalizes the state machine with the given ID Parameters:

machineId: Unique identifier for the state machine

Returns:

The built state machine and possible error

func (*StateMachineBuilder[S, E, P]) ExternalParallelTransition

func (b *StateMachineBuilder[S, E, P]) ExternalParallelTransition() ExternalParallelTransitionBuilderInterface[S, E, P]

ExternalParallelTransition starts defining an external parallel transition Returns:

A external parallel transition builder for configuring the parallel transition

func (*StateMachineBuilder[S, E, P]) ExternalTransition

func (b *StateMachineBuilder[S, E, P]) ExternalTransition() ExternalTransitionBuilderInterface[S, E, P]

ExternalTransition starts defining an external transition Returns:

A transition builder for configuring the external transition

func (*StateMachineBuilder[S, E, P]) ExternalTransitions

func (b *StateMachineBuilder[S, E, P]) ExternalTransitions() ExternalTransitionsBuilderInterface[S, E, P]

ExternalTransitions starts defining multiple external transitions from different source states to the same target state Returns:

A external transitions builder for configuring the transitions

func (*StateMachineBuilder[S, E, P]) InternalTransition

func (b *StateMachineBuilder[S, E, P]) InternalTransition() InternalTransitionBuilderInterface[S, E, P]

InternalTransition starts defining an internal transition Returns:

A internal transition builder for configuring the internal transition

type StateMachineImpl

type StateMachineImpl[S comparable, E comparable, P any] struct {
	// contains filtered or unexported fields
}

StateMachineImpl implements the StateMachine interface

func (*StateMachineImpl[S, E, P]) FireEvent

func (sm *StateMachineImpl[S, E, P]) FireEvent(sourceStateId S, event E, payload P) (S, error)

FireEvent triggers a state transition based on the current state and event

func (*StateMachineImpl[S, E, P]) FireParallelEvent

func (sm *StateMachineImpl[S, E, P]) FireParallelEvent(sourceStateId S, event E, payload P) ([]S, error)

FireParallelEvent triggers parallel state transitions based on the current state and event

func (*StateMachineImpl[S, E, P]) GenerateDiagram

func (sm *StateMachineImpl[S, E, P]) GenerateDiagram(formats ...DiagramFormat) string

GenerateDiagram returns a diagram of the state machine in the specified formats If formats is nil or empty, defaults to PlantUML If multiple formats are provided, returns all requested formats concatenated

func (*StateMachineImpl[S, E, P]) GetState

func (sm *StateMachineImpl[S, E, P]) GetState(stateId S) *State[S, E, P]

GetState returns a state by ID, creating it if it doesn't exist

func (*StateMachineImpl[S, E, P]) SetReady

func (sm *StateMachineImpl[S, E, P]) SetReady(ready bool)

SetReady marks the state machine as ready

func (*StateMachineImpl[S, E, P]) ShowStateMachine

func (sm *StateMachineImpl[S, E, P]) ShowStateMachine() string

ShowStateMachine returns a string representation of the state machine

func (*StateMachineImpl[S, E, P]) Verify

func (sm *StateMachineImpl[S, E, P]) Verify(sourceStateId S, event E) bool

Verify checks if there is a valid transition for the given state and event

type ToAmongStep

type ToAmongStep interface{}

type ToInterface

type ToInterface[S comparable, E comparable, P any] interface {
	// On specifies the triggering event
	On(event E) OnInterface[S, E, P]
}

ToInterface is the interface for specifying the target state of a transition

type ToStep

type ToStep interface{}

type Transition

type Transition[S comparable, E comparable, P any] struct {
	Source    *State[S, E, P]
	Target    *State[S, E, P]
	Event     E
	Condition Condition[P]
	Action    Action[S, E, P]
	TransType TransitionType
}

Transition represents a state transition

func (*Transition[S, E, P]) Transit

func (t *Transition[S, E, P]) Transit(payload P, checkCondition bool) (*State[S, E, P], error)

Transit executes the transition

type TransitionBuilder

type TransitionBuilder[S comparable, E comparable, P any, Next any] struct {
	// contains filtered or unexported fields
}

TransitionBuilder builds individual transitions

func (*TransitionBuilder[S, E, P, Next]) From

func (b *TransitionBuilder[S, E, P, Next]) From(state S) FromInterface[S, E, P]

From specifies the source state Parameters:

state: Source state

Returns:

The transition builder for method chaining

func (*TransitionBuilder[S, E, P, Next]) On

func (b *TransitionBuilder[S, E, P, Next]) On(event E) OnInterface[S, E, P]

On specifies the triggering event Parameters:

event: The event that triggers this transition

Returns:

The transition builder for method chaining

func (*TransitionBuilder[S, E, P, Next]) Perform

func (b *TransitionBuilder[S, E, P, Next]) Perform(action Action[S, E, P])

Perform specifies the action to execute during the transition Parameters:

action: The action to execute when the transition occurs

func (*TransitionBuilder[S, E, P, Next]) PerformFunc

func (b *TransitionBuilder[S, E, P, Next]) PerformFunc(actionFunc func(from, to S, event E, payload P) error)

PerformFunc specifies a function as the action to execute during the transition Parameters:

actionFunc: The function to execute when the transition occurs

func (*TransitionBuilder[S, E, P, Next]) To

func (b *TransitionBuilder[S, E, P, Next]) To(state S) ToInterface[S, E, P]

To specifies the target state Parameters:

state: Target state

Returns:

The transition builder for method chaining

func (*TransitionBuilder[S, E, P, Next]) When

func (b *TransitionBuilder[S, E, P, Next]) When(condition Condition[P]) WhenInterface[S, E, P]

When specifies the condition for the transition Parameters:

condition: The condition that must be satisfied for the transition to occur

Returns:

The transition builder for method chaining

func (*TransitionBuilder[S, E, P, Next]) WhenFunc

func (b *TransitionBuilder[S, E, P, Next]) WhenFunc(conditionFunc func(payload P) bool) WhenInterface[S, E, P]

WhenFunc specifies a function as the condition for the transition Parameters:

conditionFunc: The function that must return true for the transition to occur

Returns:

The transition builder for method chaining

type TransitionStarterInterface

type TransitionStarterInterface[S comparable, E comparable, P any] interface {
	// ExternalTransition starts defining an external transition
	ExternalTransition() ExternalTransitionBuilderInterface[S, E, P]

	// InternalTransition starts defining an internal transition
	InternalTransition() InternalTransitionBuilderInterface[S, E, P]

	// ExternalTransitions starts defining multiple external transitions
	ExternalTransitions() ExternalTransitionsBuilderInterface[S, E, P]

	// ExternalParallelTransition starts defining an external parallel transition
	ExternalParallelTransition() ExternalParallelTransitionBuilderInterface[S, E, P]
}

TransitionStarterInterface is the interface for starting a transition definition

type TransitionType

type TransitionType int

TransitionType defines the type of transition

const (
	// External transitions change the state from source to target
	External TransitionType = iota
	// Internal transitions don't change the state
	Internal
)

type WhenInterface

type WhenInterface[S comparable, E comparable, P any] interface {
	// Perform specifies the action to execute during the transition
	Perform(action Action[S, E, P])

	// PerformFunc specifies a function as the action to execute during the transition
	PerformFunc(actionFunc func(from, to S, event E, payload P) error)
}

WhenInterface is the interface for specifying the condition of a transition

type WhenStep

type WhenStep interface{}

type WithinStep

type WithinStep interface{}

Jump to

Keyboard shortcuts

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