Filter stream events before queueing them

This commit is contained in:
Ian Gulliver
2026-10-21 14:09:23 -07:00
parent 9f85714e44
commit ef10dc11c5
2 changed files with 9 additions and 9 deletions
+8 -5
View File
@@ -8,18 +8,18 @@ import (
type hub struct {
mu sync.Mutex
subs map[chan api.Event]bool
subs map[chan api.Event]func(api.Event) bool
}
func newHub() *hub {
return &hub{subs: map[chan api.Event]bool{}}
return &hub{subs: map[chan api.Event]func(api.Event) bool{}}
}
func (h *hub) subscribe() chan api.Event {
func (h *hub) subscribe(wanted func(api.Event) bool) chan api.Event {
h.mu.Lock()
defer h.mu.Unlock()
ch := make(chan api.Event, 256)
h.subs[ch] = true
h.subs[ch] = wanted
return ch
}
@@ -37,7 +37,10 @@ func (h *hub) unsubscribe(ch chan api.Event) {
func (h *hub) publish(ev api.Event) {
h.mu.Lock()
defer h.mu.Unlock()
for ch := range h.subs {
for ch, wanted := range h.subs {
if !wanted(ev) {
continue
}
select {
case ch <- ev:
default: