-
Notifications
You must be signed in to change notification settings - Fork 24
/
inmemory-commandbus.go
41 lines (34 loc) · 1.09 KB
/
inmemory-commandbus.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
// InMemoryCommandBus provides an inmemory implementation of the CommandPublisher CommandReceiver interfaces
type InMemoryCommandBus struct {
publishedCommandsChannel chan Command
startReceiving bool
}
// NewInMemoryCommandBus constructor
func NewInMemoryCommandBus() *InMemoryCommandBus {
publishedCommandsChannel := make(chan Command, 0)
return &InMemoryCommandBus{publishedCommandsChannel, false}
}
// PublishCommands publishes Commands to the Command bus
func (bus *InMemoryCommandBus) PublishCommands(commands []Command) error {
for _, command := range commands {
bus.publishedCommandsChannel <- command
}
return nil
}
// ReceiveCommands starts a go routine that monitors incoming Commands and routes them to a receiver channel specified within the options
func (bus *InMemoryCommandBus) ReceiveCommands(options CommandReceiverOptions) error {
go func() {
for {
select {
case ch := <-options.Close:
ch <- nil
case command := <-bus.publishedCommandsChannel:
err := options.ReceiveCommand(command)
if err != nil {
}
}
}
}()
return nil
}