-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregate.go
54 lines (45 loc) · 1.28 KB
/
aggregate.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
package eventsourcerer
import "github.com/pkg/math"
type AggregateID int
type AggregateVersion int64
// The Aggregate (Aggregate Root) is to control and encapsulate access
// to it’s members in such a way as to protect it’s invariants.
type Aggregate interface {
ID() AggregateID
Consume(event Event)
Apply(events ...Event)
Replay(journal Journal)
}
// The BaseAggregate implements the basic structures for handling transient
// events.
type BaseAggregate struct {
Aggregate
// transientEvents keeps collection of all events which have not been
// written to the event-store.
transientEvents []Event
}
func CreateBaseAggregate() *BaseAggregate {
return &BaseAggregate{
transientEvents: make([]Event, 0, 100),
}
}
// Events will return all transient events as Journal
func (a *BaseAggregate) Journal() Journal {
result := make(Journal, math.Min(len(a.transientEvents), 1))
go func() {
for _, e := range a.transientEvents {
result <- e
}
close(result)
a.Clear()
}()
return result
}
// Append appends events to the list of transient events
func (a *BaseAggregate) Append(events ...Event) {
a.transientEvents = append(a.transientEvents, events...)
}
// Clear empties the list of transient events
func (a *BaseAggregate) Clear() {
a.transientEvents = a.transientEvents[:0]
}