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

41 lines
798 B
Go
Raw Normal View History

2025-01-06 13:55:08 +00:00
package main
import "sync"
2025-01-06 22:29:54 +00:00
type SyncFilter[MsgType any] interface {
Then(what SyncFilter[MsgType]) SyncFilter[MsgType]
Call(msg MsgType)
2025-01-06 13:55:08 +00:00
}
2025-01-06 22:29:54 +00:00
type SyncFilterImpl[MsgType any] struct {
next SyncFilter[MsgType]
2025-01-06 13:55:08 +00:00
}
2025-01-06 22:29:54 +00:00
func (impl *SyncFilterImpl[MsgType]) Then(what SyncFilter[MsgType]) SyncFilter[MsgType] {
2025-01-06 19:53:08 +00:00
impl.next = what
2025-01-06 13:55:08 +00:00
return what
}
2025-01-06 22:29:54 +00:00
func (impl *SyncFilterImpl[MsgType]) CallNext(msg MsgType) {
2025-01-06 19:53:08 +00:00
if impl.next != nil {
impl.next.Call(msg)
2025-01-06 13:55:08 +00:00
}
}
2025-01-06 22:29:54 +00:00
type CollectFromChannel struct{ SyncFilterImpl[GenericMessage] }
2025-01-06 19:53:08 +00:00
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
}
}()
}