fsm

package module
v0.2.1 Latest Latest
Warning

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

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

README

Finite State Machine

Tag and Release Coverage Status Go Reference

The fsm module implements a minimal finite state machine.

I used this logic in multiple projects, so I decided I may as well publish it. Honestly, this module is so simple you might be better off just copying the logic instead of importing it.

A little copying is better than a little dependency.

Usage

See the Go reference link above.

Documentation

Overview

Package fsm implements a simple finite state machine. Events cause Transitions between States, and entry/exit to a state may be hooked by defining one or more functions to be called. That's it!

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event int

Event represents an event that can occur which may cause a state transition.

type Machine

type Machine struct {

	// State is the current Machine state.
	State State
	// Transition is a slice of possible transitions from state to state.
	Transitions []Transition
	// OnEntry is a way of hooking transitions between functions. Each
	// func(Event, State) will be called just before the Machine enters the
	// associated State. The State passed to the TransitionFunc is the _source_
	// state of the transition.
	OnEntry map[State][]TransitionFunc
	// OnExit works similarly to OnEntry. Each func(Event, State) will be called
	// just before the Machine leaves the associated State. The State passed to
	// the TransitionFunc is the _destination_ state of the transition.
	OnExit map[State][]TransitionFunc
	// By default Occur() will return an UnexpectedEventError when an event
	// occurs with no matching transition given the current state. If
	// IgnoreUnexpectedEvent is true, unexpected events will be ignored instead.
	IgnoreUnexpectedEvent bool
	// contains filtered or unexported fields
}

Machine represents a finite state machine (FSM).

func (*Machine) Occur

func (m *Machine) Occur(e Event) error

Occur handles events which may cause a transition in the machine's state. It handles synchronisation via an internal mutex, so is safe to call from multiple goroutines.

The order of operations is: * OnExit functions of the source state. * OnEntry functions of the destination state. * Update Machine state.

type State

type State int

State represents a state that the FSM can be in.

type Transition

type Transition struct {
	Src   State
	Dst   State
	Event Event
}

Transition represents a transition that the FSM can make between states.

type TransitionFunc

type TransitionFunc func(Event, State) error

A TransitionFunc handles an event during a state transition.

type UnexpectedEventError

type UnexpectedEventError struct {
	Event Event
	State State
}

UnexpectedEventError is an error type that exposes the event/state that caused the error.

func (UnexpectedEventError) Error

func (e UnexpectedEventError) Error() string

Jump to

Keyboard shortcuts

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