bbgo_origin/pkg/strategy/common/callbacks.go
chiahung.lin 6e661c805a fix
2024-01-10 14:37:07 +08:00

39 lines
778 B
Go

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