-
Notifications
You must be signed in to change notification settings - Fork 24
/
inmemory-eventbus.go
41 lines (33 loc) · 1.09 KB
/
inmemory-eventbus.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
package cqrs
// InMemoryEventBus provides an inmemory implementation of the VersionedEventPublisher VersionedEventReceiver interfaces
type InMemoryEventBus struct {
publishedEventsChannel chan VersionedEvent
startReceiving bool
}
// NewInMemoryEventBus constructor
func NewInMemoryEventBus() *InMemoryEventBus {
publishedEventsChannel := make(chan VersionedEvent, 0)
return &InMemoryEventBus{publishedEventsChannel, false}
}
// PublishEvents publishes events to the event bus
func (bus *InMemoryEventBus) PublishEvents(events []VersionedEvent) error {
for _, event := range events {
bus.publishedEventsChannel <- event
}
return nil
}
// ReceiveEvents starts a go routine that monitors incoming events and routes them to a receiver channel specified within the options
func (bus *InMemoryEventBus) ReceiveEvents(options VersionedEventReceiverOptions) error {
go func() {
for {
select {
case ch := <-options.Close:
ch <- nil
case versionedEvent := <-bus.publishedEventsChannel:
if err := options.ReceiveEvent(versionedEvent); err != nil {
}
}
}
}()
return nil
}