commit 54ff705f07c027d46366476834e5df16ab39d613
parent 7adfd6e5c641f7543fcf87e4c558579dc652fdf7
Author: hhvn <dev@hhvn.uk>
Date: Thu, 31 Aug 2023 10:42:58 +0100
Seperate event and error channels for Subscriber
Diffstat:
2 files changed, 18 insertions(+), 11 deletions(-)
diff --git a/bspc/bspc.go b/bspc/bspc.go
@@ -117,6 +117,7 @@ func LoadState() (*State, error) {
type Subscriber struct {
Event chan *Event
+ Err chan error
cleanup chan bool
cmd *exec.Cmd
pipe io.ReadCloser
@@ -124,7 +125,6 @@ type Subscriber struct {
}
type Event struct {
- Err error
Name string
Tokens []string
}
@@ -143,15 +143,23 @@ func Subscribe() (*Subscriber, error) {
var s Subscriber
s.Event = make(chan *Event)
+ s.Err = make(chan error)
s.cleanup = make(chan bool)
s.cmd = cmd
s.pipe = pipe
s.scanner = bufio.NewScanner(s.pipe)
go func() { for {
- if event := s.get(); event != nil {
+ event, err := s.get()
+
+ if event != nil {
s.Event <- event
}
+
+ if err != nil {
+ s.Err <- err
+ return
+ }
}}()
return &s, nil
@@ -173,7 +181,7 @@ func (s *Subscriber) getLine() (string, error) {
return "", nil
}
-func (s *Subscriber) get() (*Event) {
+func (s *Subscriber) get() (*Event, error) {
var line string
var err error
for {
@@ -181,10 +189,10 @@ func (s *Subscriber) get() (*Event) {
if len(line) > 0 && line[0] != 'W' { break }
}
- if line == "" { return nil }
- if err != nil { return &Event{err, "", nil} }
+ if line == "" { return nil, nil }
+ if err != nil { return nil, err }
name, tokens, _ := strings.Cut(line, " ")
- return &Event{nil, name, strings.Split(tokens, " ")}
+ return &Event{name, strings.Split(tokens, " ")}, nil
}
diff --git a/main.go b/main.go
@@ -45,11 +45,11 @@ func handleEvents(events *bspc.Subscriber, bar *bar.Handle) (chan error, chan bo
reload := make(chan bool)
go func(){ for {
- event = <-events.Event
-
- if event.Err != nil {
- errors <- event.Err
+ select {
+ case err := <-events.Err:
+ errors <- err
return
+ case event = <- events.Event:
}
if strings.HasPrefix(event.Name, "monitor_") {
@@ -115,7 +115,6 @@ func main() {
}
barhandle.NewState <- state
-
case err := <- everr:
common.Error("Couldn't read event: %s\n", err)
return