added context for subscribe method
This commit is contained in:
parent
bec954ef31
commit
e308674337
@ -2,12 +2,13 @@ package hypr
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"context"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"git.milar.in/milarin/slices"
|
"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(""))
|
r, err := readSocketRaw(i.EventSocketPath(), strings.NewReader(""))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -21,7 +22,8 @@ func (i *Instance) Subscribe(events ...EventType) (<-chan Event, error) {
|
|||||||
defer close(out)
|
defer close(out)
|
||||||
|
|
||||||
sc := bufio.NewScanner(r)
|
sc := bufio.NewScanner(r)
|
||||||
for sc.Scan() {
|
|
||||||
|
for ctx.Err() == nil && sc.Scan() {
|
||||||
event := parseEvent(sc.Text())
|
event := parseEvent(sc.Text())
|
||||||
if _, ok := eventMap[event.Type]; ok {
|
if _, ok := eventMap[event.Type]; ok {
|
||||||
out <- event
|
out <- event
|
||||||
|
Loading…
Reference in New Issue
Block a user