Last active
October 9, 2020 03:05
-
-
Save dipeshdulal/f41f05d3ef69dc2f5795b0fd626058ea to your computer and use it in GitHub Desktop.
State Machine Transition Engine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IMachine machine interface | |
type IMachine interface { | |
Transition() string | |
Current() string | |
} | |
// Current returns current state | |
func (m *Machine) Current() string { | |
if m.current == "" { | |
return m.Initial | |
} | |
return m.current | |
} | |
// Transition transitions to next state | |
func (m *Machine) Transition(event string) string { | |
current := m.Current() | |
transitions := m.States[current].On | |
next := transitions[event].To | |
if next != "" { | |
m.current = next | |
return next | |
} | |
return current | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment