1
0
Fork 0

Collect improvements

This commit is contained in:
Michał Rudowicz 2025-01-07 00:10:21 +01:00
parent dd02763e84
commit 499fa82d96
2 changed files with 7 additions and 5 deletions

View File

@ -25,16 +25,17 @@ func (impl *SyncFilterImpl[MsgType]) CallNext(msg MsgType) {
}
}
type CollectFromChannel struct{ SyncFilterImpl[GenericMessage] }
type CollectFromChannel[MsgType any] struct{ SyncFilterImpl[MsgType] }
func (collect *CollectFromChannel) Call(msg GenericMessage) {
func (collect *CollectFromChannel[MsgType]) Call(msg MsgType) {
collect.CallNext(msg)
}
func (collect CollectFromChannel) Collect(events <-chan GenericMessage, wg *sync.WaitGroup) {
func (collect CollectFromChannel[MsgType]) Collect(events <-chan MsgType, wg *sync.WaitGroup, onClose func()) {
wg.Add(1)
go func() {
defer wg.Done()
defer onClose()
for e := range events {
collect.Call(e)

View File

@ -345,13 +345,14 @@ func TestSyncCollect(t *testing.T) {
testEvents := make(chan GenericMessage)
wg := sync.WaitGroup{}
tested := CollectFromChannel{}
tested := CollectFromChannel[GenericMessage]{}
mock := &SyncMockFilter{}
mock2 := &SyncMockFilter{}
tested.Then(mock).Then(mock2)
tested.Collect(testEvents, &wg)
wg.Add(1)
tested.Collect(testEvents, &wg, func() { wg.Done() })
testEvents <- makeGenericMessage(satel.ArmedPartition, 1, true)
testEvents <- makeGenericMessage(satel.DoorOpened, 2, true)