-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
registry.go
57 lines (44 loc) · 1.32 KB
/
registry.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
package journal
import "sync"
// EventTypeRegistry is a component that constructs tracked EventType tokens,
// for usage with a Journal.
type EventTypeRegistry interface {
// RegisterEventType introduces a new event type to a journal, and
// returns an EventType token that components can later use to check whether
// journalling for that type is enabled/suppressed, and to tag journal
// entries appropriately.
RegisterEventType(system, event string) EventType
}
// eventTypeRegistry is an embeddable mixin that takes care of tracking disabled
// event types, and returning initialized/safe EventTypes when requested.
type eventTypeRegistry struct {
sync.Mutex
m map[string]EventType
}
var _ EventTypeRegistry = (*eventTypeRegistry)(nil)
func NewEventTypeRegistry(disabled DisabledEvents) EventTypeRegistry {
ret := &eventTypeRegistry{
m: make(map[string]EventType, len(disabled)+32), // + extra capacity.
}
for _, et := range disabled {
et.enabled, et.safe = false, true
ret.m[et.System+":"+et.Event] = et
}
return ret
}
func (d *eventTypeRegistry) RegisterEventType(system, event string) EventType {
d.Lock()
defer d.Unlock()
key := system + ":" + event
if et, ok := d.m[key]; ok {
return et
}
et := EventType{
System: system,
Event: event,
enabled: true,
safe: true,
}
d.m[key] = et
return et
}