2020-02-08 14:31:47 +03:00
|
|
|
package dispatcher
|
|
|
|
|
|
|
|
import "github.com/asaskevich/EventBus"
|
|
|
|
|
2020-04-20 14:29:33 +03:00
|
|
|
type Subscriber interface {
|
2020-02-16 13:23:47 +03:00
|
|
|
Subscribe(topic string, fn interface{})
|
2020-04-20 14:29:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type Emitter interface {
|
2020-02-16 13:23:47 +03:00
|
|
|
Emit(topic string, args ...interface{})
|
2020-02-08 14:31:47 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:29:33 +03:00
|
|
|
type Dispatcher interface {
|
|
|
|
Subscriber
|
|
|
|
Emitter
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:42:38 +03:00
|
|
|
type localEventDispatcher struct {
|
2020-02-08 14:31:47 +03:00
|
|
|
bus EventBus.Bus
|
|
|
|
}
|
|
|
|
|
2020-04-16 19:42:38 +03:00
|
|
|
func (d *localEventDispatcher) Subscribe(topic string, fn interface{}) {
|
2020-02-16 13:23:47 +03:00
|
|
|
_ = d.bus.Subscribe(topic, fn)
|
2020-02-08 14:31:47 +03:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:42:38 +03:00
|
|
|
func (d *localEventDispatcher) Emit(topic string, args ...interface{}) {
|
2020-02-16 13:23:47 +03:00
|
|
|
d.bus.Publish(topic, args...)
|
2020-02-08 14:31:47 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:29:33 +03:00
|
|
|
func New() Dispatcher {
|
2020-04-16 19:42:38 +03:00
|
|
|
return &localEventDispatcher{
|
2020-02-08 14:31:47 +03:00
|
|
|
bus: EventBus.New(),
|
|
|
|
}
|
|
|
|
}
|