From b8552676043b10124c927f7580fd4f58b1e7a05c Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 14 Sep 2022 02:53:32 +0800 Subject: [PATCH] bbgo: wrap keyboard removal in defer func --- pkg/bbgo/interact.go | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/pkg/bbgo/interact.go b/pkg/bbgo/interact.go index 75dc34f5e..d39cff261 100644 --- a/pkg/bbgo/interact.go +++ b/pkg/bbgo/interact.go @@ -283,6 +283,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { } return nil }).Next(func(signature string, reply interact.Reply) error { + defer func() { + if kc, ok := reply.(interact.KeyboardController); ok { + kc.RemoveKeyboard() + } + }() + strategy, ok := it.exchangeStrategies[signature] if !ok { reply.Message("Strategy not found") @@ -297,10 +303,6 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { status := controller.GetStatus() - if kc, ok := reply.(interact.KeyboardController); ok { - kc.RemoveKeyboard() - } - if status == types.StrategyStatusRunning { reply.Message(fmt.Sprintf("Strategy %s is running.", signature)) } else if status == types.StrategyStatusStopped { @@ -321,6 +323,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { } return nil }).Next(func(signature string, reply interact.Reply) error { + defer func() { + if kc, ok := reply.(interact.KeyboardController); ok { + kc.RemoveKeyboard() + } + }() + strategy, ok := it.exchangeStrategies[signature] if !ok { reply.Message("Strategy not found") @@ -339,16 +347,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { return nil } - if kc, ok := reply.(interact.KeyboardController); ok { - kc.RemoveKeyboard() - } - if err := controller.Suspend(); err != nil { reply.Message(fmt.Sprintf("Failed to suspend the strategy, %s", err.Error())) return err } - reply.Message(fmt.Sprintf("Strategy %s suspended.", signature)) + reply.Message(fmt.Sprintf("Strategy %s is now suspended.", signature)) return nil }) @@ -363,6 +367,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { } return nil }).Next(func(signature string, reply interact.Reply) error { + defer func() { + if kc, ok := reply.(interact.KeyboardController); ok { + kc.RemoveKeyboard() + } + }() + strategy, ok := it.exchangeStrategies[signature] if !ok { reply.Message("Strategy not found") @@ -381,16 +391,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) { return nil } - if kc, ok := reply.(interact.KeyboardController); ok { - kc.RemoveKeyboard() - } - if err := controller.Resume(); err != nil { reply.Message(fmt.Sprintf("Failed to resume the strategy, %s", err.Error())) return err } - reply.Message(fmt.Sprintf("Strategy %s resumed.", signature)) + reply.Message(fmt.Sprintf("Strategy %s is now resumed.", signature)) return nil })