forked from hallgren/eventsourcing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventstream.go
216 lines (191 loc) · 5.52 KB
/
eventstream.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package eventsourcing
import (
"fmt"
"reflect"
"sync"
)
// EventStream struct what handles event subscription
type EventStream struct {
// holds subscribers of aggregate types events
aggregateTypes map[string][]*Subscription
// holds subscribers of specific aggregates (type and identifier)
specificAggregates map[string][]*Subscription
// holds subscribers of specific events
specificEvents map[reflect.Type][]*Subscription
// holds subscribers of all events
allEvents []*Subscription
// makes sure events are delivered in order and subscriptions are persistent
lock sync.Mutex
}
// Subscription holding the subscribe / unsubscribe / and func to be called when
// event matches the subscription
type Subscription struct {
f func(e Event)
unsubF func()
subF func()
}
// Unsubscribe stops the subscription
func (s *Subscription) Unsubscribe() {
s.unsubF()
}
// Subscribe starts the subscription
func (s *Subscription) Subscribe() {
s.subF()
}
// NewEventStream factory function
func NewEventStream() *EventStream {
return &EventStream{
aggregateTypes: make(map[string][]*Subscription),
specificAggregates: make(map[string][]*Subscription),
specificEvents: make(map[reflect.Type][]*Subscription),
allEvents: []*Subscription{},
}
}
// Update calls the functions that are subscribing to event
func (e *EventStream) Update(agg aggregate, events []Event) {
// the lock prevent other event updates get mixed with this update
e.lock.Lock()
defer e.lock.Unlock()
for _, event := range events {
// call all functions that has registered for all events
for _, s := range e.allEvents {
s.f(event)
}
// call all functions that has registered for the specific event
t := reflect.TypeOf(event.Data)
if subs, ok := e.specificEvents[t]; ok {
for _, s := range subs {
s.f(event)
}
}
ref := fmt.Sprintf("%s_%s", agg.path(), event.AggregateType)
// call all functions that has registered for the aggregate type events
if subs, ok := e.aggregateTypes[ref]; ok {
for _, s := range subs {
s.f(event)
}
}
// call all functions that has registered for the aggregate type and id events
// ref also include the package name ensuring that Aggregate Types can have the same name.
ref = fmt.Sprintf("%s_%s", ref, agg.id())
if subs, ok := e.specificAggregates[ref]; ok {
for _, s := range subs {
s.f(event)
}
}
}
}
// SubscriberAll bind the f function to be called on all events independent on aggregate or event type
func (e *EventStream) SubscriberAll(f func(e Event)) *Subscription {
s := Subscription{
f: f,
}
s.unsubF = func() {
e.lock.Lock()
defer e.lock.Unlock()
for i, sub := range e.allEvents {
if &s == sub {
e.allEvents = append(e.allEvents[:i], e.allEvents[i+1:]...)
break
}
}
}
s.subF = func() {
e.lock.Lock()
defer e.lock.Unlock()
e.allEvents = append(e.allEvents, &s)
}
return &s
}
// SubscriberSpecificAggregate bind the f function to be called on events that belongs to aggregate based on type and id
func (e *EventStream) SubscriberSpecificAggregate(f func(e Event), aggregates ...aggregate) *Subscription {
s := Subscription{
f: f,
}
s.unsubF = func() {
e.lock.Lock()
defer e.lock.Unlock()
for _, a := range aggregates {
name := reflect.TypeOf(a).Elem().Name()
ref := fmt.Sprintf("%s_%s_%s", a.path(), name, a.id())
for i, sub := range e.specificAggregates[ref] {
if &s == sub {
e.specificAggregates[ref] = append(e.specificAggregates[ref][:i], e.specificAggregates[ref][i+1:]...)
break
}
}
}
}
s.subF = func() {
e.lock.Lock()
defer e.lock.Unlock()
for _, a := range aggregates {
name := reflect.TypeOf(a).Elem().Name()
ref := fmt.Sprintf("%s_%s_%s", a.path(), name, a.id())
// adds one more function to the aggregate
e.specificAggregates[ref] = append(e.specificAggregates[ref], &s)
}
}
return &s
}
// SubscriberAggregateType bind the f function to be called on events on the aggregate type
func (e *EventStream) SubscriberAggregateType(f func(e Event), aggregates ...aggregate) *Subscription {
s := Subscription{
f: f,
}
s.unsubF = func() {
e.lock.Lock()
defer e.lock.Unlock()
for _, a := range aggregates {
name := reflect.TypeOf(a).Elem().Name()
ref := fmt.Sprintf("%s_%s", a.path(), name)
for i, sub := range e.aggregateTypes[ref] {
if &s == sub {
e.aggregateTypes[ref] = append(e.aggregateTypes[ref][:i], e.aggregateTypes[ref][i+1:]...)
break
}
}
}
}
s.subF = func() {
e.lock.Lock()
defer e.lock.Unlock()
for _, a := range aggregates {
name := reflect.TypeOf(a).Elem().Name()
ref := fmt.Sprintf("%s_%s", a.path(), name)
// adds one more function to the aggregate
e.aggregateTypes[ref] = append(e.aggregateTypes[ref], &s)
}
}
return &s
}
// SubscriberSpecificEvent bind the f function to be called on specific events
func (e *EventStream) SubscriberSpecificEvent(f func(e Event), events ...interface{}) *Subscription {
s := Subscription{
f: f,
}
s.unsubF = func() {
e.lock.Lock()
defer e.lock.Unlock()
for _, event := range events {
t := reflect.TypeOf(event)
for i, sub := range e.specificEvents[t] {
if &s == sub {
e.specificEvents[t] = append(e.specificEvents[t][:i], e.specificEvents[t][i+1:]...)
break
}
}
}
}
s.subF = func() {
e.lock.Lock()
defer e.lock.Unlock()
// subscribe to specified events
for _, event := range events {
t := reflect.TypeOf(event)
// adds one more property to the event type
e.specificEvents[t] = append(e.specificEvents[t], &s)
}
}
return &s
}