mirror of
https://github.com/vacp2p/dasy.git
synced 2026-01-08 22:37:59 -05:00
* started working on payload concept * added crypto * moved into new package, added feed * added nicer feed stuff * minor readme for now * subscribe implemented badly * doc * doc block * cleaned up a little * making a mutex * mock, empty test * test wrapper * started playing around * updated mock * formatted * updated * updated interface * updated * updated * updated mock, rewrote test stuff * todos * added tests * reuse * dont need var
62 lines
927 B
Go
62 lines
927 B
Go
package state
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
type memorySyncState struct {
|
|
sync.Mutex
|
|
|
|
state []State
|
|
}
|
|
|
|
func NewSyncState() *memorySyncState {
|
|
return &memorySyncState{}
|
|
}
|
|
|
|
func (s *memorySyncState) Add(newState State) error {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
s.state = append(s.state, newState)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *memorySyncState) Remove(id MessageID, peer PeerID) error {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
var newState []State
|
|
|
|
for _, state := range s.state {
|
|
if state.MessageID != id || state.PeerID != peer {
|
|
newState = append(newState, state)
|
|
}
|
|
}
|
|
|
|
s.state = newState
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *memorySyncState) All() ([]State, error) {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
return s.state, nil
|
|
}
|
|
|
|
func (s *memorySyncState) Map(epoch int64, process func(State) State) error {
|
|
s.Lock()
|
|
defer s.Unlock()
|
|
|
|
for i, state := range s.state {
|
|
if state.SendEpoch > epoch {
|
|
continue
|
|
}
|
|
|
|
s.state[i] = process(state)
|
|
}
|
|
|
|
return nil
|
|
}
|