initial
This commit is contained in:
46
internal/thestore/store.go
Normal file
46
internal/thestore/store.go
Normal file
@@ -0,0 +1,46 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user