2020-02-08 17:01:47 +05:30
|
|
|
package dispatcher
|
|
|
|
|
|
|
|
import "github.com/asaskevich/EventBus"
|
|
|
|
|
2020-04-20 16:59:33 +05:30
|
|
|
type Subscriber interface {
|
2020-02-16 15:53:47 +05:30
|
|
|
Subscribe(topic string, fn interface{})
|
2020-04-20 16:59:33 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type Emitter interface {
|
2020-02-16 15:53:47 +05:30
|
|
|
Emit(topic string, args ...interface{})
|
2020-02-08 17:01:47 +05:30
|
|
|
}
|
|
|
|
|
2020-04-20 16:59:33 +05:30
|
|
|
type Dispatcher interface {
|
|
|
|
Subscriber
|
|
|
|
Emitter
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:12:38 +05:30
|
|
|
type localEventDispatcher struct {
|
2020-02-08 17:01:47 +05:30
|
|
|
bus EventBus.Bus
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:12:38 +05:30
|
|
|
func (d *localEventDispatcher) Subscribe(topic string, fn interface{}) {
|
2020-02-16 15:53:47 +05:30
|
|
|
_ = d.bus.Subscribe(topic, fn)
|
2020-02-08 17:01:47 +05:30
|
|
|
}
|
|
|
|
|
2020-04-16 22:12:38 +05:30
|
|
|
func (d *localEventDispatcher) Emit(topic string, args ...interface{}) {
|
2020-02-16 15:53:47 +05:30
|
|
|
d.bus.Publish(topic, args...)
|
2020-02-08 17:01:47 +05:30
|
|
|
}
|
|
|
|
|
2020-04-20 16:59:33 +05:30
|
|
|
func New() Dispatcher {
|
2020-04-16 22:12:38 +05:30
|
|
|
return &localEventDispatcher{
|
2020-02-08 17:01:47 +05:30
|
|
|
bus: EventBus.New(),
|
|
|
|
}
|
|
|
|
}
|