Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 31, 2022. It is now read-only.
/ fsm Public archive

Simple finite state machine library for golang

License

edge/fsm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FSM

godoc Go Report Card Coverage

A simple easy to use go Finite State Machine library. FSM is a programmatic map of whitelisted transitions, where each transition triggers a synchronous operation within the context of a State.

Install

go get github.com/edge/fsm

Usage

// Create new instance of FSM with a context
ctx := context.Background()
f := fsm.New().WithContext(ctx)

// FromAny states are useful for transitioning to
// an Error, Shutdown or other universally accessible states.
f.NewState().FromAny().To("ERROR").OnEnter(func(*fsm.State) {
	// Do something
})

// Parallel transitions are none blocking.
f.NewState().From("INITIALIZING").To("READY").OnEnter(func(*fsm.State) {
	// Do something here
}).Parallel(true)

// FromStart identifies transitions that can come from the launch state.
f.NewState().FromStart().To("READY").OnEnter(func(*fsm.State) {
	// Do something here
})

// Each state has a context that is closed before the state changes.
// You can use this with methods called within the state OnEnter method.
f.NewState().From("FETCHING_DATA").To("STARTING_SERVER").OnEnter(func(st *fsm.State) {
	doSomething(st.Context())
})

// Run an action before each transition
f.BeforeTransition(func(t *fsm.Transition) {
	fmt.Printf(`Transition to %s`, t.To.Destination)
})

f.OnStart(func(st *fsm.State) {
	runLaunchCode(st.Context())
})

// Start tells the state machine to enter the initial state.
f.Start()

About

Simple finite state machine library for golang

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages