bbgo_origin/pkg/strategy/common/callbacks.go

39 lines
778 B
Go
Raw Normal View History

2024-01-10 06:37:07 +00:00
package common
2024-01-10 06:37:07 +00:00
//go:generate callbackgen -type StatusCallbacks
type StatusCallbacks struct {
readyCallbacks []func()
closedCallbacks []func()
errorCallbacks []func(error)
}
2024-01-10 06:37:07 +00:00
func (c *StatusCallbacks) OnReady(cb func()) {
c.readyCallbacks = append(c.readyCallbacks, cb)
}
2024-01-10 06:37:07 +00:00
func (c *StatusCallbacks) EmitReady() {
for _, cb := range c.readyCallbacks {
cb()
}
}
2024-01-10 06:37:07 +00:00
func (c *StatusCallbacks) OnClosed(cb func()) {
c.closedCallbacks = append(c.closedCallbacks, cb)
}
2024-01-10 06:37:07 +00:00
func (c *StatusCallbacks) EmitClosed() {
for _, cb := range c.closedCallbacks {
cb()
}
}
2024-01-10 06:37:07 +00:00
func (c *StatusCallbacks) OnError(cb func(err error)) {
c.errorCallbacks = append(c.errorCallbacks, cb)
}
2024-01-10 06:37:07 +00:00
func (c *StatusCallbacks) EmitError(err error) {
for _, cb := range c.errorCallbacks {
cb(err)
}
}