Documentation
¶
Overview ¶
Package fsm allows you to add Finite State Machines to your code.
const ( StateFoo fsm.State = iota StateBar ) const ( EventFoo fsm.Event = iota ) f := fsm.New(StateFoo) f.Transition( fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Dst(StateBar), )
You can have custom checks or actions :
f.Transition(
fsm.Src(StateFoo), fsm.Check(func() bool {
// check something
}),
fsm.Call(func() {
// do something
}),
)
Index ¶
- type Event
- type FSM
- func (f *FSM) Current() State
- func (f *FSM) Enter(fn func(state State))
- func (f *FSM) EnterState(state State, fn func())
- func (f *FSM) Event(e Event) bool
- func (f *FSM) Exit(fn func(state State))
- func (f *FSM) ExitState(state State, fn func())
- func (f *FSM) Reset()
- func (f *FSM) Transition(opts ...Option)
- type Option
- type State
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
type Event int
Event is the event type. You can define your own values as
const ( EventFoo fsm.Event = iota EventBar )
type FSM ¶
type FSM struct {
// contains filtered or unexported fields
}
FSM is a finite state machine.
func (*FSM) EnterState ¶ added in v1.0.1
EnterState sets a func that will be called when entering a state.
func (*FSM) Event ¶
Event send an Event to a machine, applying at most one transition. true is returned if a transition is applied.
func (*FSM) ExitState ¶ added in v1.0.1
ExitState sets a func that will be called when entering a state.
func (*FSM) Transition ¶
Transition creates a new transition, usually having trigger On an Event, from a Src State, to a Dst State.
type Option ¶
type Option func(*transition)
Option defines a transition option.
func Call ¶
func Call(fn func()) Option
Call defines a function that is called when a Transition occurs.
Example ¶
package main
import (
"fmt"
"github.com/cocoonspace/fsm"
)
const (
StateFoo fsm.State = iota
StateBar
)
const (
EventFoo fsm.Event = iota
EventBar
)
func main() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo),
fsm.Dst(StateBar), fsm.Call(func() {
fmt.Println("Call called")
}),
)
}
func Check ¶
Check is an external condition that allows a Transition only if fn returns true.
Example ¶
package main
import (
"github.com/cocoonspace/fsm"
)
const (
StateFoo fsm.State = iota
StateBar
)
const (
EventFoo fsm.Event = iota
EventBar
)
func main() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Check(func() bool {
return true
}),
fsm.Dst(StateBar),
)
}
func NotCheck ¶
NotCheck is an external condition that allows a Transition only if fn returns false.
func Times ¶
Times defines the number of consecutive times conditions must be valid before a Transition occurs. Times will not work if multiple Transitions are possible at the same time.
Example ¶
package main
import (
"github.com/cocoonspace/fsm"
)
const (
StateFoo fsm.State = iota
StateBar
)
const (
EventFoo fsm.Event = iota
EventBar
)
func main() {
f := fsm.New(StateFoo)
f.Transition(
fsm.On(EventFoo), fsm.Src(StateFoo), fsm.Times(2),
fsm.Dst(StateBar),
)
_ = f.Event(EventFoo) // no transition
_ = f.Event(EventFoo) // transition to StateBar
}