1
0
Fork 0
hswro-alarm-bot/filters_sync.go

41 lines
707 B
Go
Raw Normal View History

2025-01-06 13:55:08 +00:00
package main
import "sync"
type SyncFilter interface {
Then(what SyncFilter) SyncFilter
Call(msg GenericMessage)
}
2025-01-06 19:53:08 +00:00
type SyncFilterImpl struct {
2025-01-06 13:55:08 +00:00
next SyncFilter
}
2025-01-06 19:53:08 +00:00
func (impl *SyncFilterImpl) Then(what SyncFilter) SyncFilter {
impl.next = what
2025-01-06 13:55:08 +00:00
return what
}
2025-01-06 19:53:08 +00:00
func (impl *SyncFilterImpl) CallNext(msg GenericMessage) {
if impl.next != nil {
impl.next.Call(msg)
2025-01-06 13:55:08 +00:00
}
}
2025-01-06 19:53:08 +00:00
type CollectFromChannel struct{ SyncFilterImpl }
func (collect *CollectFromChannel) Call(msg GenericMessage) {
collect.CallNext(msg)
}
func (collect CollectFromChannel) Collect(events <-chan GenericMessage, wg *sync.WaitGroup) {
2025-01-06 13:55:08 +00:00
wg.Add(1)
go func() {
defer wg.Done()
for e := range events {
2025-01-06 19:53:08 +00:00
collect.Call(e)
2025-01-06 13:55:08 +00:00
}
}()
}