summaryrefslogtreecommitdiff
path: root/internal/fsm/fsm.go
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2024-08-22 13:33:19 +0200
committerJeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com>2024-08-22 13:41:58 +0200
commitb3dddce77023fd1d0e5c7451cad97bb88264aa23 (patch)
treeaddce01d61754fbd3e8f73a6e383b7569046b669 /internal/fsm/fsm.go
parent27cc558c39d47a35b9671aeec7852452d5eb046b (diff)
FSM: Re-ordering and cleanup constructor
Diffstat (limited to 'internal/fsm/fsm.go')
-rw-r--r--internal/fsm/fsm.go43
1 files changed, 17 insertions, 26 deletions
diff --git a/internal/fsm/fsm.go b/internal/fsm/fsm.go
index 950f7cf..5985aa5 100644
--- a/internal/fsm/fsm.go
+++ b/internal/fsm/fsm.go
@@ -3,11 +3,19 @@ package fsm
import "fmt"
+// State represents a single node in the graph.
+type State struct {
+ // Transitions indicates which out arrows this node has
+ Transitions []Transition
+}
+
type (
// StateID represents the Identifier of the state.
StateID int8
// StateIDSlice represents the list of state identifiers.
StateIDSlice []StateID
+ // States is the map from state identifier to the state itself
+ States map[StateID]State
)
// Len is defined here such that we can sort the slice
@@ -33,15 +41,6 @@ type Transition struct {
Description string
}
-// States is the map from state identifier to the state itself
-type States map[StateID]State
-
-// State represents a single node in the graph.
-type State struct {
- // Transitions indicates which out arrows this node has
- Transitions []Transition
-}
-
// FSM represents the total graph.
type FSM struct {
// States is the map from state ID to states
@@ -57,9 +56,6 @@ type FSM struct {
// It takes the old state, the new state and the data and returns if this is handled by the client
StateCallback func(StateID, StateID, interface{}) bool
- // Directory represents the path where the state graph is stored
- Directory string
-
// GetStateName gets the name of a state as a string
GetStateName func(StateID) string
@@ -67,20 +63,15 @@ type FSM struct {
initial StateID
}
-// Init initializes the state machine and sets it to the given current state.
-func (fsm *FSM) Init(
- current StateID,
- states States,
- callback func(StateID, StateID, interface{}) bool,
- directory string,
- nameGen func(StateID) string,
-) {
- fsm.States = states
- fsm.Current = current
- fsm.StateCallback = callback
- fsm.Directory = directory
- fsm.GetStateName = nameGen
- fsm.initial = current
+// NewFSM creates a new finite state machine
+func NewFSM(current StateID, states States, callback func(StateID, StateID, interface{}) bool, nameGen func(StateID) string) FSM {
+ return FSM{
+ States: states,
+ Current: current,
+ StateCallback: callback,
+ GetStateName: nameGen,
+ initial: current,
+ }
}
// InState returns whether or not the state machine is in the given 'check' state.