forked from hallgren/eventsourcing
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
55 lines (48 loc) · 1.36 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
package main
import (
"fmt"
"github.com/hallgren/eventsourcing"
"github.com/hallgren/eventsourcing/eventstore/memory"
"time"
)
func main() {
var c = make(chan eventsourcing.Event)
// Setup a memory based event store
eventStore := memory.Create()
repo := eventsourcing.NewRepository(eventStore, nil)
f := func(e eventsourcing.Event) {
fmt.Printf("Event from stream %q\n", e)
// Its a good practice making this function as fast as possible not blocking the event sourcing call for to long
// Here we use a channel to store the events to be consumed async
c <- e
}
sub := repo.SubscriberAll(f)
sub.Subscribe()
// Read the event stream async
go func() {
for {
// advance to next value
event := <-c
fmt.Println("STREAM EVENT")
fmt.Println(event)
}
}()
// Creates the aggregate and adds a second event
aggregate := CreateFrequentFlierAccount("morgan")
aggregate.RecordFlightTaken(10, 5)
// saves the events to the memory backed eventstore
err := repo.Save(aggregate)
if err != nil {
panic("Could not save the aggregate")
}
// Load the saved aggregate
copy := FrequentFlierAccountAggregate{}
err = repo.Get(string(aggregate.ID()), ©)
if err != nil {
panic("Could not get aggregate")
}
// Sleep to make sure the events are delivered from the stream
time.Sleep(time.Millisecond * 100)
fmt.Println("AGGREGATE")
fmt.Println(copy)
}