47 lines
769 B
Go
47 lines
769 B
Go
package thestore
|
|
|
|
import "sync"
|
|
|
|
type Store sync.Map
|
|
|
|
func NewStore() *Store {
|
|
s := sync.Map{}
|
|
store := Store(s)
|
|
return &store
|
|
}
|
|
|
|
// todo pass context.context and failfast
|
|
func (store *Store) Dump() []Event {
|
|
result := make([]Event, 0)
|
|
(*sync.Map)(store).Range(func(_, v any) bool {
|
|
result = append(result, v.(Event))
|
|
return true
|
|
})
|
|
return result
|
|
}
|
|
|
|
func (store *Store) Push(op Event) {
|
|
k := op.ID
|
|
event, ok := store.Get(k)
|
|
if ok {
|
|
event = event.Push(op)
|
|
} else {
|
|
event = op
|
|
}
|
|
store.Set(event)
|
|
}
|
|
|
|
func (store *Store) Set(v Event) {
|
|
k := v.ID
|
|
(*sync.Map)(store).Store(k, v)
|
|
}
|
|
|
|
func (store *Store) Get(k string) (Event, bool) {
|
|
got, ok := (*sync.Map)(store).Load(k)
|
|
if !ok {
|
|
return Event{}, false
|
|
}
|
|
event := got.(Event)
|
|
return event, true
|
|
}
|