This repository contains a Go implementation of a state machine. The state machine is managed using a state manager that can handle multiple events and states simultaneously.
- Go version 1.18 or higher
As a library
go get github.com/gavinin/go-state-machine
This method is used to create and initialize a new instance of StateManager
.
StateManager supports generics, the following documents will be replaced by the Example
class.
stateManager := state.NewStateManager[Example]()
This method is used to set the duration or frequency at which the StateManager
updates its states.
stateManager.SetDuration(10 * time.Millisecond)
NewStateManagerEvent(eventType EventType, id string, events chan Test, duration time.Duration, callback CallbackFunc) *Event
This method is used to create a new state event.
All data exchange of StateManager is completed through NewStateManagerEvent.
Whenever a new request is added, it needs to be completed through an event.
event := stateManager.NewStateManagerEvent(state.ADD, strconv.Itoa(i), tests, duration,
func(events chan Example) {
events <- Example{
}
})
The following are all supported EventTypes.
const (
ADD EventType = "ADD"
DEL EventType = "DEL"
RESET EventType = "RESET"
PAUSE EventType = "PAUSE"
RUNNING EventType = "RUNNING"
)
ADD
: Add a statusDEL
: Delete a statusRESET
: Reset timerPAUSE
: Pause this statusRUNNING
: This status is running
This method is used to send an event to the StateManager
.
stateManager.SendEvent(event)
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.