Avoid elements being removed multiple times

as this leads to multiple deletion events in the monitoring.
This commit is contained in:
Maximilian Paß
2022-07-31 22:45:53 +02:00
committed by Sebastian Serth
parent d10b31a1fb
commit 6e52b8660d

View File

@ -95,8 +95,11 @@ func (s *localStorage[T]) Get(id string) (o T, ok bool) {
func (s *localStorage[T]) Delete(id string) {
s.Lock()
defer s.Unlock()
s.sendMonitoringData(id, s.objects[id], true, s.unsafeLength()-1)
delete(s.objects, id)
o, ok := s.objects[id]
if ok {
delete(s.objects, id)
s.sendMonitoringData(id, o, true, s.unsafeLength())
}
}
func (s *localStorage[T]) Pop(id string) (T, bool) {
@ -118,8 +121,8 @@ func (s *localStorage[T]) Sample() (o T, ok bool) {
s.Lock()
defer s.Unlock()
for key, object := range s.objects {
s.sendMonitoringData(key, object, true, s.unsafeLength()-1)
delete(s.objects, key)
s.sendMonitoringData(key, object, true, s.unsafeLength())
return object, true
}
return o, false