-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
76 lines (66 loc) · 1.69 KB
/
repository.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
70
71
72
73
74
75
76
package eventbus
import (
"log"
"sync"
)
//--------------------------------------------------
// Listener Repository
//--------------------------------------------------
type Repository interface {
Keep(typ EventType, l Listener)
Find(typ EventType) []Listener
Remove(uuid string)
}
type repositoryInst struct {
listeners map[EventType][]Listener
}
//--------------------------------------------------
// Methods
//--------------------------------------------------
func (r *repositoryInst) Keep(typ EventType, ln Listener) {
//listener impl
l := ln.(*listenerImpl)
//log
log.Printf("Persisting listener %s on %s", l.UUID, l.Type.Name)
//add listeners
r.listeners[typ] = append(r.listeners[typ], ln)
}
func (r *repositoryInst) Find(typ EventType) []Listener {
//log
log.Printf("Finding listener for %s", typ.Name)
//
return r.listeners[typ]
}
func (r *repositoryInst) Remove(uuid string) {
//log
log.Printf("Removing listener %s", uuid)
//seek in listeners
for k, la := range r.listeners {
//create new listener arrays
lanew := make([]Listener, 0)
//seek of 0 to len(vs)
for i := 0; i < len(la); i++ {
l := la[i].(*listenerImpl)
if l.UUID == uuid {
l.Conn.Close()
continue
} else {
lanew = append(lanew, l)
}
}
r.listeners[k] = lanew
}
}
//--------------------------------------------------
// Singleton Implementation (lazy)
// by: http://marcio.io/2015/07/singleton-pattern-in-go/
//--------------------------------------------------
var repInst *repositoryInst
var repOnce = new(sync.Once)
func GetRepositoryInst() Repository {
repOnce.Do(func() {
repInst = new(repositoryInst)
repInst.listeners = make(map[EventType][]Listener)
})
return repInst
}