Skip to content

Commit aab5cf9

Browse files
committed
docs(event): update package docs
1 parent dffc514 commit aab5cf9

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

backend/mongo/store.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ import (
1717
"github.com/modernice/goes/event/query/time"
1818
"github.com/modernice/goes/event/query/version"
1919
"github.com/modernice/goes/helper/pick"
20-
"github.com/modernice/goes/internal/slice"
21-
"go.mongodb.org/mongo-driver/bson"
20+
"go.mongodb.org/mongo-driver/bson"
2221
"go.mongodb.org/mongo-driver/mongo"
2322
"go.mongodb.org/mongo-driver/mongo/options"
2423
"go.mongodb.org/mongo-driver/x/mongo/driver"

event/doc.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Package event is the core of goes. It defines and implements a generic event
2+
// system that is used as the building block for all the other components
3+
// provided by goes.
4+
//
5+
// The core type of this package is the Event interface. An event is either an
6+
// application event or an aggregate event, depending on the provided data.
7+
// Read the documentation of the Event interface for more information.
8+
//
9+
// To create an event, pass at least the name of the event and some arbitrary
10+
// event data to the New function:
11+
//
12+
// evt := event.New("foo", 3)
13+
// // evt.ID() == uuid.UUID{...}
14+
// // evt.Name() == "foo"
15+
// // evt.Time() == time.Now()
16+
// // evt.Data() == 3
17+
//
18+
// Events can be published to subscribers over an event bus:
19+
//
20+
// var bus event.Bus
21+
// evt := event.New("foo", 3)
22+
// err := bus.Publish(context.TODO(), evt)
23+
//
24+
// Events can also be subscribed to using an event bus:
25+
//
26+
// // Subscribe to "foo", "bar", and "baz" events.
27+
// events, errs, err := bus.Subscribe(context.TODO(), "foo", "bar", "baz")
28+
//
29+
// Events can be inserted into and queried from an event store:
30+
//
31+
// var store event.Store
32+
// evt := event.New("foo", 3)
33+
// err := store.Insert(context.TODO(), evt)
34+
//
35+
// events, errs, err := store.Query(context.TODO(), query.New(
36+
// query.Name("foo"), // query "foo" events
37+
// query.SortByTime(), // sort events by time
38+
// ))
39+
//
40+
// Depending on the used event store and/or event bus implementations, it may be
41+
// required to pass an Encoding for your event data to the store/bus. Example
42+
// using encoding/gob for encoding of event data:
43+
//
44+
// enc := codec.Gob(event.NewRegistry())
45+
// codec.GobRegister[int](enc, "foo") // register "foo" as an int
46+
// codec.GobRegister[string](enc, "bar") // register "bar" as a string
47+
// codec.GobRegister[struct{Foo string}](enc, "baz") // register "baz" as a struct{Foo string}
48+
// store := mongo.NewEventStore(enc)
49+
package event

event/event.go

+19-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package event
22

3-
//go:generate mockgen -source=event.go -destination=./mocks/event.go
4-
53
import (
64
"sort"
75
stdtime "time"
@@ -12,31 +10,31 @@ import (
1210
"github.com/modernice/goes/internal/xtime"
1311
)
1412

15-
// Event is any event.
13+
// Event is an event with any data.
1614
type Event = Of[any]
1715

18-
// An event describes something that has happened in the application or
19-
// specifically something that has happened to an aggregate in the application.
16+
// Of is an event with a specific data type. An event has a unique id, a name,
17+
// some arbitrary event data, and a time at which the event was raised. Behavior
18+
// for events that do not provide this data is not defined.
19+
//
20+
// If an event belongs to the stream of an aggregate, the Aggregate() method of
21+
// the event should return the aggregate's id and name, and the optimistic
22+
// concurrency version of the event.
23+
//
24+
// Events can be published over a Bus. When published, the event is sent to all
25+
// subscribers of the event that are subscribed to the event via the same Bus.
2026
//
21-
// Publish & Subscribe
27+
// var bus event.Bus
28+
// var evt event.Event
29+
// err := bus.Publish(context.TODO(), evt)
2230
//
23-
// An event can be published through a Bus and sent to subscribers of Events
24-
// with the same name.
31+
// To subscribe to an event, call the Subscribe() method on a Bus:
2532
//
26-
// Example (publish):
27-
// var b event.Bus
28-
// evt := event.New("foo", someData{})
29-
// err := b.Publish(context.TODO(), evt)
30-
// // handle err
33+
// var bus event.Bus
34+
// // Subscribe to "foo", "bar", and "baz" events.
35+
// events, errs, err := bus.Subscribe(context.TODO(), "foo", "bar", "baz")
3136
//
32-
// Example (subscribe):
33-
// var b event.Bus
34-
// res, errs, err := b.Subscribe(context.TODO(), "foo")
35-
// // handle err
36-
// err := streams.Walk(context.TODO(), func(e event.Event) {
37-
// log.Println(fmt.Sprintf("Received %q event: %v", e.Name(), e))
38-
// }, res, errs)
39-
// // handle err
37+
// Read the documentation of Bus for more details.
4038
type Of[Data any] interface {
4139
// ID returns the unique id of the event.
4240
ID() uuid.UUID

0 commit comments

Comments
 (0)