interact: add Cycle state builder

This commit is contained in:
c9s 2022-01-15 03:06:36 +08:00
parent 2a6b821908
commit 1c7d4d09cf
2 changed files with 16 additions and 1 deletions

View File

@ -60,7 +60,7 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
reply.Message("No any strategy supports PositionReader")
}
return nil
}).Next(func(signature string, reply interact.Reply) error {
}).Cycle(func(signature string, reply interact.Reply) error {
strategy, ok := it.exchangeStrategies[signature]
if !ok {
reply.Message("Strategy not found")

View File

@ -54,6 +54,21 @@ func (c *Command) NamedNext(n State, f interface{}) *Command {
return c
}
func (c *Command) Cycle(f interface{}) *Command {
var curState State
if c.lastState == "" {
curState = State(c.Name + "_" + strconv.Itoa(c.stateID))
} else {
curState = c.lastState
}
nextState := curState
c.states[curState] = nextState
c.statesFunc[curState] = f
c.lastState = nextState
return c
}
// Next defines the next state with the transition function from the last defined state.
func (c *Command) Next(f interface{}) *Command {
var curState State