feature: use callbackgen

This commit is contained in:
Andy Cheng 2022-04-21 17:54:16 +08:00
parent cbf6bf78bc
commit f6ec931bed
2 changed files with 45 additions and 39 deletions

View File

@ -4,13 +4,14 @@ import (
"github.com/c9s/bbgo/pkg/types"
)
//go:generate callbackgen -type StrategyController -interface
type StrategyController struct {
Status types.StrategyStatus
// Callbacks
SuspendCallback func() error
ResumeCallback func() error
EmergencyStopCallback func() error
SuspendCallbacks []func()
ResumeCallbacks []func()
EmergencyStopCallbacks []func()
}
func (s *StrategyController) GetStatus() types.StrategyStatus {
@ -20,55 +21,25 @@ func (s *StrategyController) GetStatus() types.StrategyStatus {
func (s *StrategyController) Suspend() error {
s.Status = types.StrategyStatusStopped
return s.EmitSuspend()
}
s.EmitSuspend()
func (s *StrategyController) OnSuspend(cb func() error) {
s.SuspendCallback = cb
}
func (s *StrategyController) EmitSuspend() error {
if s.SuspendCallback != nil {
return s.SuspendCallback()
} else {
return nil
}
return nil
}
func (s *StrategyController) Resume() error {
s.Status = types.StrategyStatusRunning
return s.EmitResume()
}
s.EmitResume()
func (s *StrategyController) OnResume(cb func() error) {
s.ResumeCallback = cb
}
func (s *StrategyController) EmitResume() error {
if s.ResumeCallback != nil {
return s.ResumeCallback()
} else {
return nil
}
return nil
}
func (s *StrategyController) EmergencyStop() error {
s.Status = types.StrategyStatusStopped
return s.EmitEmergencyStop()
}
s.EmitEmergencyStop()
func (s *StrategyController) OnEmergencyStop(cb func() error) {
s.EmergencyStopCallback = cb
}
func (s *StrategyController) EmitEmergencyStop() error {
if s.EmergencyStopCallback != nil {
return s.EmergencyStopCallback()
} else {
return nil
}
return nil
}
type StrategyStatusReader interface {

View File

@ -0,0 +1,35 @@
// Code generated by "callbackgen -type StrategyController strategy_controller.go"; DO NOT EDIT.
package bbgo
import ()
func (s *StrategyController) OnSuspend(cb func()) {
s.SuspendCallbacks = append(s.SuspendCallbacks, cb)
}
func (s *StrategyController) EmitSuspend() {
for _, cb := range s.SuspendCallbacks {
cb()
}
}
func (s *StrategyController) OnResume(cb func()) {
s.ResumeCallbacks = append(s.ResumeCallbacks, cb)
}
func (s *StrategyController) EmitResume() {
for _, cb := range s.ResumeCallbacks {
cb()
}
}
func (s *StrategyController) OnEmergencyStop(cb func()) {
s.EmergencyStopCallbacks = append(s.EmergencyStopCallbacks, cb)
}
func (s *StrategyController) EmitEmergencyStop() {
for _, cb := range s.EmergencyStopCallbacks {
cb()
}
}