/
event_store.go
38 lines (31 loc) · 1003 Bytes
/
event_store.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
package eventbus
// Event stores are used to persist events when they are created.
type EventStore interface {
// Write the event to the event store. Returns an error
// if the event fails to write.
WriteEvent(*Event) error
}
// Event store that simply discards events.
type NullEventStore struct {
}
// Construct a new NullEventStore and return a pointer to it.
func NewNullEventStore() *NullEventStore {
return &NullEventStore{}
}
// Silently discard the event.
func (store *NullEventStore) WriteEvent(event *Event) (err error) {
return
}
// A simple in-memory event store that puts all events in an Array.
type InMemoryEventStore struct {
Events []*Event
}
// Construct a new in-memory event store. Returns a pointer to the event store.
func NewInMemoryEventStore() *InMemoryEventStore {
return &InMemoryEventStore{}
}
// Save the event in an in-memory array.
func (store *InMemoryEventStore) WriteEvent(event *Event) (err error) {
store.Events = append(store.Events, event)
return
}