-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener.go
69 lines (51 loc) · 1020 Bytes
/
listener.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package warta
//go:generate mockery -name=listener -inpkg -testonly
import (
"reflect"
"github.com/google/uuid"
)
type listener interface {
Close()
Callback() interface{}
getID() string
}
type listen struct {
id string
topic topic
callback interface{}
}
func newListener(topic topic, callback interface{}) (l listener, err error) {
if reflect.ValueOf(callback).Kind() != reflect.Func {
err = ErrCallbackNotFunction
return
}
id, err := uuid.NewRandom()
if err != nil {
return
}
l = &listen{
id: id.String(),
topic: topic,
callback: callback,
}
return
}
func (l *listen) Close() {
mu := l.topic.getMutex()
mu.Lock()
defer mu.Unlock()
nListener := make(map[string]listener)
oListeners := l.topic.getListeners()
delete(oListeners, l.id)
for key, val := range oListeners {
nListener[key] = val
}
l.topic.setListeners(nListener)
return
}
func (l *listen) Callback() interface{} {
return l.callback
}
func (l *listen) getID() string {
return l.id
}