Categorygithub.com/iotexproject/go-fsm
modulepackage
1.0.0
Repository: https://github.com/iotexproject/go-fsm.git
Documentation: pkg.go.dev

# README

go-fsm

It is a lightweight finite state machine (FSM) implementation in Golang. It has been inspired by the FSM used in Apache Hadoop YARN:

  • Thread safe
  • Multiple destination states
  • Dynamic destination state determination

Installation

go get github.com/zjshen14/go-fsm

Usage

package main

import (
	"fmt"

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

type evt struct {
	t fsm.EventType
}

func (e *evt) Type() fsm.EventType { return e.t }

func main() {
	fsm, _ := fsm.NewBuilder().
		AddInitialState("s1").
		AddStates("s2", "s3").
		AddTransition("s1", "e1", func(event fsm.Event) (fsm.State, error) { return "s2", nil }, []fsm.State{"s2"}).
		AddTransition("s2", "e2", func(event fsm.Event) (fsm.State, error) { return "s3", nil }, []fsm.State{"s3"}).
		AddTransition("s3", "e3", func(event fsm.Event) (fsm.State, error) { return "s1", nil }, []fsm.State{"s1"}).
		Build()

	fmt.Println(fsm.CurrentState())
	fsm.Handle(&evt{t: "e1"})
	fmt.Println(fsm.CurrentState())
	fsm.Handle(&evt{t: "e2"})
	fmt.Println(fsm.CurrentState())
	fsm.Handle(&evt{t: "e3"})
	fmt.Println(fsm.CurrentState())
}

License

go-fsm is under the Apache 2.0 license. See the LICENSE file for details.

# Packages

No description provided by the author

# Functions

NewBuilder creates an FSM builder instance with empty setup.

# Variables

ErrBuild represents the error of building an FSM.
ErrInvalidTransition indicates that the actual destination state after transition is not defined.
ErrTransitionNotFound indicates that there doesn't exist a transition defined on the source state and the event.

# Structs

Builder is an FSM builder to help construct an FSM.

# Interfaces

Event is the interface of the events that could be handled by an FSM.
FSM is the interface of an FSM (finite state machine).

# Type aliases

EventType represents a event type and it is a string.
State represents the state and it is a string.
Transition is the interface of the transition that would happen on a certain state when receiving a certain event.