-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
62 lines (53 loc) · 1.44 KB
/
main.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
package main
import (
"context"
"errors"
"fmt"
"time"
"github.com/theskyinflames/cqrs-eda/pkg/bus"
"github.com/theskyinflames/cqrs-eda/pkg/events"
"github.com/google/uuid"
)
const eventName = "anEvent"
/*
This is an example of events bus use case.
*/
func main() {
var (
eventsChan = make(chan events.Event)
eventHandlers = []events.Handler{
func(e events.Event) {
fmt.Printf("eh1, received %s event, with id %s\n", e.Name(), e.AggregateID().String())
},
func(e events.Event) {
fmt.Printf("eh2, received %s event, with id %s\n", e.Name(), e.AggregateID().String())
},
}
eventsListener = events.NewListener(eventsChan, eventName, eventHandlers...)
busHandler bus.Handler = func(_ context.Context, d bus.Dispatchable) (interface{}, error) {
e, ok := d.(events.Event)
if !ok {
return nil, errors.New("unexpected dispatchable")
}
eventsChan <- e
return "dispatchable processed", nil
}
)
// Start the events listener
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errChan := make(chan error)
go func() {
for err := range errChan {
fmt.Printf("events listener: %s", err.Error())
}
}()
go eventsListener.Listen(ctx, errChan)
// Start the events bus
bus := bus.New()
bus.Register(eventName, busHandler)
// Dispatch an event
bus.Dispatch(ctx, events.NewEventBasic(uuid.New(), eventName, nil))
// Give time to output the logs
time.Sleep(time.Second)
}