-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage.go
49 lines (40 loc) · 1.17 KB
/
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
40
41
42
43
44
45
46
47
48
49
package eventbus
import "sync"
//--------------------------------------------------
// Storage
//--------------------------------------------------
type Storage interface {
StoreEvent(e *Event) error
ListEvents(t EventType) ([]*Event, error)
}
type storageInst struct {
events map[EventType][]*Event
}
//--------------------------------------------------
// Methods
//--------------------------------------------------
//StoreEvent store events in memory
func (s *storageInst) StoreEvent(e *Event) error {
es := s.events[e.Type]
es = append(es, e)
s.events[e.Type] = es
return nil
}
//ListEvents list all event in memory filtered by type t
func (s *storageInst) ListEvents(t EventType) ([]*Event, error) {
return s.events[t], nil
}
//--------------------------------------------------
// Singleton Implementation (lazy)
// by: http://marcio.io/2015/07/singleton-pattern-in-go/
//--------------------------------------------------
var stoInst *storageInst
var stoOnce = new(sync.Once)
//GetStorageInst get events store instance
func GetStorageInst() Storage {
stoOnce.Do(func() {
stoInst = new(storageInst)
stoInst.events = make(map[EventType][]*Event)
})
return stoInst
}