Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

event: create subscribeOnce to allow testing loop handling #20

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading