hypr/subscribe_event.go

34 lines
554 B
Go
Raw Normal View History

2024-02-09 20:57:18 +01:00
package hypr
import (
"bufio"
"strings"
"git.milar.in/milarin/slices"
)
func (i *Instance) Subscribe(events ...EventType) (<-chan Event, error) {
r, err := readSocketRaw(i.EventSocketPath(), strings.NewReader(""))
if err != nil {
return nil, err
}
out := make(chan Event, 10)
eventMap := slices.ToStructMap(events)
go func() {
defer r.Close()
defer close(out)
sc := bufio.NewScanner(r)
for sc.Scan() {
event := parseEvent(sc.Text())
if _, ok := eventMap[event.Type]; ok {
out <- event
}
}
}()
return out, nil
}