1
0
Fork 0

SyncFilterImpl

This commit is contained in:
Michał Rudowicz 2025-01-06 20:53:08 +01:00
parent 88c86a9581
commit f7bb03721c
2 changed files with 16 additions and 17 deletions

View File

@ -7,28 +7,34 @@ type SyncFilter interface {
Call(msg GenericMessage) Call(msg GenericMessage)
} }
type CollectFromChannel struct { type SyncFilterImpl struct {
next SyncFilter next SyncFilter
} }
func (self *CollectFromChannel) Then(what SyncFilter) SyncFilter { func (impl *SyncFilterImpl) Then(what SyncFilter) SyncFilter {
self.next = what impl.next = what
return what return what
} }
func (self *CollectFromChannel) Call(msg GenericMessage) { func (impl *SyncFilterImpl) CallNext(msg GenericMessage) {
if self.next != nil { if impl.next != nil {
self.next.Call(msg) impl.next.Call(msg)
} }
} }
func (self CollectFromChannel) Collect(events <-chan GenericMessage, wg *sync.WaitGroup) { type CollectFromChannel struct{ SyncFilterImpl }
func (collect *CollectFromChannel) Call(msg GenericMessage) {
collect.CallNext(msg)
}
func (collect CollectFromChannel) Collect(events <-chan GenericMessage, wg *sync.WaitGroup) {
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
for e := range events { for e := range events {
self.Call(e) collect.Call(e)
} }
}() }()
} }

View File

@ -332,20 +332,13 @@ func TestThrottle_ManyMessagesInOneEvent(t *testing.T) {
} }
type SyncMockFilter struct { type SyncMockFilter struct {
SyncFilterImpl
collected []GenericMessage collected []GenericMessage
next SyncFilter
}
func (self *SyncMockFilter) Then(what SyncFilter) SyncFilter {
self.next = what
return what
} }
func (self *SyncMockFilter) Call(msg GenericMessage) { func (self *SyncMockFilter) Call(msg GenericMessage) {
self.collected = append(self.collected, msg) self.collected = append(self.collected, msg)
if self.next != nil { self.CallNext(msg)
self.next.Call(msg)
}
} }
func TestSyncCollect(t *testing.T) { func TestSyncCollect(t *testing.T) {