Skip to content

Commit

Permalink
Merge pull request #20 from thiagokokada/test-subscribe-without-loop
Browse files Browse the repository at this point in the history
event: create subscribeOnce to allow testing loop handling
  • Loading branch information
thiagokokada authored Aug 31, 2024
2 parents 6c8a305 + f804916 commit 5238002
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
20 changes: 13 additions & 7 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 2 additions & 15 deletions event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions event/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5238002

Please sign in to comment.