2024-01-20 08:45:01 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
type LSMAction int
|
|
|
|
|
|
|
|
const (
|
|
|
|
LSMActionPause LSMAction = iota
|
|
|
|
LSMActionNext
|
|
|
|
LSMActionReset
|
|
|
|
LSMActionCancel
|
|
|
|
)
|
|
|
|
|
|
|
|
type LinearStateMachine struct {
|
|
|
|
Steps []func() LSMAction
|
|
|
|
|
|
|
|
index int
|
|
|
|
cancelled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLinearStateMachine(steps ...func() LSMAction) *LinearStateMachine {
|
|
|
|
return &LinearStateMachine{
|
|
|
|
Steps: steps,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the state machine until it pauses, finishes or is cancelled.
|
|
|
|
func (lsm *LinearStateMachine) Run() (cancelled bool, done bool) {
|
|
|
|
if lsm.index >= len(lsm.Steps) {
|
|
|
|
return lsm.cancelled, true
|
|
|
|
}
|
|
|
|
for lsm.index < len(lsm.Steps) {
|
|
|
|
action := lsm.Steps[lsm.index]()
|
|
|
|
switch action {
|
|
|
|
case LSMActionPause:
|
|
|
|
return false, false
|
|
|
|
case LSMActionNext:
|
|
|
|
lsm.index++
|
|
|
|
case LSMActionReset:
|
|
|
|
lsm.index = 0
|
|
|
|
case LSMActionCancel:
|
|
|
|
lsm.cancelled = true
|
|
|
|
return true, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
|
2024-01-27 20:45:11 +08:00
|
|
|
func (lsm *LinearStateMachine) AppendSteps(steps ...func() LSMAction) {
|
|
|
|
lsm.Steps = append(lsm.Steps, steps...)
|
|
|
|
}
|
|
|
|
|
2024-01-20 08:45:01 +08:00
|
|
|
func (lsm *LinearStateMachine) Reset() {
|
|
|
|
lsm.index = 0
|
|
|
|
lsm.cancelled = false
|
|
|
|
}
|