-
Notifications
You must be signed in to change notification settings - Fork 0
/
obsworker.go
50 lines (42 loc) · 1.09 KB
/
obsworker.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
40
41
42
43
44
45
46
47
48
49
50
package eventbus
import (
"log"
)
//--------------------------------------------------
// Storage Observer
//--------------------------------------------------
type ObserverWorker interface {
Notify(e *Event)
}
type observerWorkerImpl struct {
rep Repository
}
//--------------------------------------------------
// Methods
//--------------------------------------------------
func (o *observerWorkerImpl) Notify(e *Event) {
la := o.rep.Find(e.Type)
for i := 0; i < len(la); i++ {
//convert event to bytes
b, err := fromEvent(e)
if err != nil {
log.Printf("Error on notify listener about %s event", e.Type.Name)
}
//create frame
frameOut := NewFrame(RegRespFrameType, b)
//write in listener
l := la[i].(*listenerImpl)
err = WriteFrame(l.Conn, frameOut)
if err != nil {
log.Printf("Error on write notification %s", e.Type.Name)
}
}
}
//--------------------------------------------------
// Public Operations
//--------------------------------------------------
func NewObserverWorker(r Repository) ObserverWorker {
obs := new(observerWorkerImpl)
obs.rep = r
return obs
}