-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
39 lines (32 loc) · 942 Bytes
/
dispatcher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package data_dispatcher
import (
"context"
"github.com/lapuda/data_dispatcher/core"
"log"
)
import event "github.com/lapuda/event_center/core"
type Dispatcher struct {
Ctx context.Context
eventCenter *event.EventCenter
}
func (d *Dispatcher) initEnv() {
d.eventCenter = event.CreateEventCenter(d.Ctx)
d.eventCenter.Register(core.CollectEvent{}.Name())
}
func (d *Dispatcher) AddTarget(targetName string, writeFunc event.EventHandler) {
d.eventCenter.Subscribe(core.CollectEvent{}.Name(), event.HandlerName(targetName), writeFunc)
}
func (d *Dispatcher) Collect(data interface{}) {
error := d.eventCenter.SendEvent(core.CollectEvent{CollectData: data})
if error != nil {
log.Panicf("during collect data occer error: %v \n", error)
}
}
func (d *Dispatcher) Run() {
d.initEnv()
}
func NewDispatcherAndRun(ctx context.Context) *Dispatcher {
dispatcher := Dispatcher{Ctx: ctx}
dispatcher.Run()
return &dispatcher
}