fsm

package
v0.0.0-...-90b53a1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package fsm contains tools for implementing simple state machines.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Start

func Start(ctx context.Context, initial State, options ...Option) error

Start runs the state machine until it is stopped or an error occurs.

Types

type Action

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

Action describes the action taken by a state.

func EnterState

func EnterState(next State) Action

EnterState returns an action that transitions to a new state.

func Fail

func Fail(err error) Action

Fail is an action that stops the state machine with an error.

func StayInCurrentState

func StayInCurrentState() Action

StayInCurrentState is an action that stays in the current state.

func Stop

func Stop() Action

Stop is an action that stops the state machine.

type Binding

type Binding[S any] struct {
	EnterState func(S) Action
}

Binding returns actions that can be performed by the state machine within the context of some additional arguments.

func With

func With[T1 any](v1 T1) Binding[State1[T1]]

With binds a state to an argument.

func With2

func With2[T1, T2 any](v1 T1, v2 T2) Binding[State2[T1, T2]]

With2 binds a state to 2 arguments.

type Option

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

Option is an option that changes the behavior of a state machine.

func WithFinalState

func WithFinalState(s State) Option

WithFinalState is an option that sets the final state of a state machine.

The final state is entered whenever the state machine stops. It may be used to prevent the state machine from stopping (by explicitly entering another state) and/or to perform cleanup actions.

type State

type State func(context.Context) Action

State is a function that implements the logic for a single state.

type State1

type State1[T1 any] func(context.Context, T1) Action

State1 is a state that accept a single argument.

type State2

type State2[T1, T2 any] func(context.Context, T1, T2) Action

State2 is a state that accepts two arguments.

Jump to

Keyboard shortcuts

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