Skip to content

Commit

Permalink
fix(hypripc): Allow closing with pending events
Browse files Browse the repository at this point in the history
  • Loading branch information
pdf committed Mar 9, 2024
1 parent f424a72 commit fea276a
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions internal/hypripc/hypripc.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ const (
DispatchCloseWindow = `closewindow`
)

var (
eventMatch = regexp.MustCompile(`^(?P<Event>[^>]+)>>(?P<Value>.*)$`)
)
var eventMatch = regexp.MustCompile(`^(?P<Event>[^>]+)>>(?P<Value>.*)$`)

// CancelFunc cancels a subscription when called.
type CancelFunc func()
Expand All @@ -109,6 +107,7 @@ type HyprIPC struct {
subscriptions map[Event]map[uuid.UUID]chan *eventv1.Event
evtConn net.Conn
evtBus chan []byte
quitCh chan struct{}
mu sync.RWMutex
}

Expand Down Expand Up @@ -245,6 +244,7 @@ func (h *HyprIPC) StartEvents() {

// Close terminates all connections, event loops, and closes all subscriptions.
func (h *HyprIPC) Close() {
close(h.quitCh)
h.evtConn.Close()
close(h.evtBus)
}
Expand All @@ -267,15 +267,33 @@ func (h *HyprIPC) eventloop() {
h.mu.RLock()
if _, ok := h.subscriptions[EventUnspecified]; ok {
for _, ch := range h.subscriptions[EventUnspecified] {
ch <- evt
select {
case <-h.quitCh:
return
default:
select {
case <-h.quitCh:
return
case ch <- evt:
}
}
}
}
if _, ok := h.subscriptions[name]; !ok {
h.mu.RUnlock()
continue
}
for _, ch := range h.subscriptions[name] {
ch <- evt
select {
case <-h.quitCh:
return
default:
select {
case <-h.quitCh:
return
case ch <- evt:
}
}
}
h.mu.RUnlock()
}
Expand Down Expand Up @@ -305,6 +323,7 @@ func New(log hclog.Logger) (*HyprIPC, error) {
log: log,
evtConn: evtConn,
evtBus: make(chan []byte, 10),
quitCh: make(chan struct{}),
subscriptions: make(map[Event]map[uuid.UUID]chan *eventv1.Event),
}

Expand Down

0 comments on commit fea276a

Please sign in to comment.