-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.go
39 lines (33 loc) · 896 Bytes
/
storage.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 eventmanager
// Memory storage structure
type Memory struct {
storage map[string][]Subscriber
}
// Attaches a subscriber for an event
func (m *Memory) Attach(eventName string, subscriber Subscriber) {
subscribers := m.storage[eventName]
m.storage[eventName] = append(subscribers, subscriber)
}
// Retrieve all subscribers of a given event
func (m *Memory) Subscribers(eventName string) []Subscriber {
sr := m.storage[eventName]
return sr
}
// De attaches a subscriber
func (m *Memory) DeAttach(eventName string, subscriber Subscriber) {
var key int
for k, v := range m.storage[eventName] {
if subscriber == v {
key = k
break
}
}
m.storage[eventName] = append(m.storage[eventName][:key], m.storage[eventName][1+key:]...)
}
// Factory method for the storage structure
func NewMemoryStorage() *Memory {
m := make(map[string][]Subscriber)
return &Memory{
m,
}
}