-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
93 lines (73 loc) · 2.11 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
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
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/go-gulfstream/gulfstream/pkg/command"
"github.com/go-gulfstream/gulfstream/pkg/eventbus"
"github.com/go-gulfstream/gulfstream/pkg/event"
"github.com/go-gulfstream/gulfstream/pkg/stream"
"github.com/google/uuid"
)
func main() {
orderStreamStorage := stream.NewStorage(orderStream, orderStreamFactory)
ctx := context.Background()
idx := new(someLocalIndexInMem)
channel := eventbus.NewChannel()
channel.Subscribe(orderStream,
eventbus.HandlerFunc(addedToCartEvent,
func(ctx context.Context, e *event.Event) error {
idx.Increment()
fmt.Printf("event: addedToCart%v\nversion: %d\nstream: %s\n",
e.Payload().(*addedToCart), e.Version(), e.StreamID())
return nil
}, nil),
)
channel.Subscribe(orderStream,
eventbus.HandlerFunc(activatedEvent,
func(ctx context.Context, e *event.Event) error {
fmt.Printf("event: activated\nversion: %d\nstream: %s\n",
e.Version(), e.StreamID())
return nil
}, nil),
)
go func() {
checkError(channel.Listen(ctx))
}()
mutator := stream.NewMutator(orderStreamStorage, channel)
mutator.AddCommandController(addToCartCommand,
newAddToCartController(idx), stream.WithCommandControllerCreateIfNotExists())
mutator.AddCommandController(activateCommand,
stream.ControllerFunc(func(ctx context.Context, s *stream.Stream, command *command.Command) (*command.Reply, error) {
s.Mutate(activatedEvent, nil)
return nil, nil
}))
addToCartCmd := newAddToCartCommand(&addToCart{
ShopID: owner,
Name: "someProduct",
Price: 2.33,
})
_, err := mutator.CommandSink(ctx, addToCartCmd)
checkError(err)
activateCmd := newActivateOrderCommand()
_, err = mutator.CommandSink(ctx, activateCmd)
checkError(err)
currentStream, err := orderStreamStorage.Load(ctx, streamID)
checkError(err)
fmt.Printf("currentStream: %s\n", currentStream.State().(*order))
<-time.After(2 * time.Second)
}
func orderStreamFactory() *stream.Stream {
return stream.New(
orderStream,
uuid.UUID{},
&order{},
)
}
func checkError(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}