bbgo_origin/pkg/bbgo/notifier.go
2020-10-27 09:38:29 +08:00

60 lines
1.4 KiB
Go

package bbgo
type Notifier interface {
NotifyTo(channel, format string, args ...interface{}) error
Notify(format string, args ...interface{}) error
}
type NullNotifier struct{}
func (n *NullNotifier) NotifyTo(channel, format string, args ...interface{}) error {
return nil
}
func (n *NullNotifier) Notify(format string, args ...interface{}) error {
return nil
}
type Notifiability struct {
notifiers []Notifier
SessionChannelRouter *PatternChannelRouter
SymbolChannelRouter *PatternChannelRouter
ObjectChannelRouter *ObjectChannelRouter
}
func (m *Notifiability) RouteSymbol(symbol string) (channel string, ok bool) {
return m.SymbolChannelRouter.Route(symbol)
}
func (m *Notifiability) RouteSession(session string) (channel string, ok bool) {
return m.SessionChannelRouter.Route(session)
}
func (m *Notifiability) RouteObject(obj interface{}) (channel string, ok bool) {
return m.ObjectChannelRouter.Route(obj)
}
func (m *Notifiability) AddNotifier(notifier Notifier) {
m.notifiers = append(m.notifiers, notifier)
}
func (m *Notifiability) Notify(msg string, args ...interface{}) (err error) {
for _, n := range m.notifiers {
if err2 := n.Notify(msg, args...); err2 != nil {
err = err2
}
}
return err
}
func (m *Notifiability) NotifyTo(channel, msg string, args ...interface{}) (err error) {
for _, n := range m.notifiers {
if err2 := n.NotifyTo(channel, msg, args...); err2 != nil {
err = err2
}
}
return err
}