diff --git a/event/event.go b/event/event.go index 3ec8011..76df414 100644 --- a/event/event.go +++ b/event/event.go @@ -74,15 +74,21 @@ func (c *EventClient) Receive() ([]ReceivedData, error) { // the events you want to handle and all event types you want to handle. func Subscribe(c *EventClient, ev EventHandler, events ...EventType) error { for { - msg, err := c.Receive() - if err != nil { - return err - } + subscribeOnce(c, ev, events...) + } +} - for _, data := range msg { - processEvent(ev, data, events) - } +func subscribeOnce(c eventClient, ev EventHandler, events ...EventType) error { + msg, err := c.Receive() + if err != nil { + return err + } + + for _, data := range msg { + processEvent(ev, data, events) } + + return nil } func processEvent(ev EventHandler, msg ReceivedData, events []EventType) { diff --git a/event/event_test.go b/event/event_test.go index 2f48960..85170d7 100644 --- a/event/event_test.go +++ b/event/event_test.go @@ -27,7 +27,7 @@ func TestReceive(t *testing.T) { go func() { c := hyprland.MustClient() time.Sleep(100 * time.Millisecond) - c.Dispatch("exec kitty") + c.Dispatch("exec kitty sh -c 'echo Testing hyprland-go events && sleep 1'") }() // We must capture this event @@ -45,7 +45,7 @@ func TestReceive(t *testing.T) { func TestSubscribe(t *testing.T) { h := &FakeEventHandler{t: t} c := &FakeEventClient{} - err := SubscribeWithoutLoop(*c, h, AllEvents...) + err := subscribeOnce(c, h, AllEvents...) assert.NoError(t, err) } @@ -204,16 +204,3 @@ func (h *FakeEventHandler) Screencast(s Screencast) { assert.Equal(h.t, s.Owner, "0") assert.Equal(h.t, s.Sharing, true) } - -func SubscribeWithoutLoop(c FakeEventClient, ev EventHandler, events ...EventType) error { - msg, err := c.Receive() - if err != nil { - return err - } - - for _, data := range msg { - processEvent(ev, data, events) - } - - return nil -} diff --git a/event/event_types.go b/event/event_types.go index 2b9819b..92c9983 100644 --- a/event/event_types.go +++ b/event/event_types.go @@ -7,6 +7,11 @@ type EventClient struct { conn net.Conn } +// Event Client interface, right now only used for testing. +type eventClient interface { + Receive() ([]ReceivedData, error) +} + type RawData string type EventType string