From e3086743375cac6a832d2e06ea83b8e0bab02c92 Mon Sep 17 00:00:00 2001 From: Milarin Date: Fri, 9 Feb 2024 21:30:14 +0100 Subject: [PATCH] added context for subscribe method --- subscribe_event.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/subscribe_event.go b/subscribe_event.go index d2d10b6..8a3ec6c 100644 --- a/subscribe_event.go +++ b/subscribe_event.go @@ -2,12 +2,13 @@ package hypr import ( "bufio" + "context" "strings" "git.milar.in/milarin/slices" ) -func (i *Instance) Subscribe(events ...EventType) (<-chan Event, error) { +func (i *Instance) Subscribe(ctx context.Context, events ...EventType) (<-chan Event, error) { r, err := readSocketRaw(i.EventSocketPath(), strings.NewReader("")) if err != nil { return nil, err @@ -21,7 +22,8 @@ func (i *Instance) Subscribe(events ...EventType) (<-chan Event, error) { defer close(out) sc := bufio.NewScanner(r) - for sc.Scan() { + + for ctx.Err() == nil && sc.Scan() { event := parseEvent(sc.Text()) if _, ok := eventMap[event.Type]; ok { out <- event