From 98eba7e6bae76531d4d237706dd1dbaf72f71c4f Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sun, 1 Sep 2024 00:46:02 +0100 Subject: [PATCH 1/3] event/event: add Close() method --- event/event.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/event/event.go b/event/event.go index 76df414..1603dd4 100644 --- a/event/event.go +++ b/event/event.go @@ -36,6 +36,11 @@ func NewEventClient(socket string) (*EventClient, error) { return &EventClient{conn: conn}, nil } +// Close the underlying connection. +func (c *EventClient) Close() error { + return c.conn.Close() +} + // Low-level receive event method, should be avoided unless there is no // alternative. func (c *EventClient) Receive() ([]ReceivedData, error) { From 9ed06b149d5313e9b856197aec643a87c5aa324e Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sun, 1 Sep 2024 00:46:16 +0100 Subject: [PATCH 2/3] event/event: MustEventClient() -> MustClient, NewEventClient() -> NewClient --- event/event.go | 8 ++++---- event/event_test.go | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/event/event.go b/event/event.go index 1603dd4..07150f6 100644 --- a/event/event.go +++ b/event/event.go @@ -20,15 +20,15 @@ const ( // automatically find the proper socket to connect and use the // HYPRLAND_INSTANCE_SIGNATURE for the current user. // If you need to connect to arbitrary user instances or need a method that -// will not panic on error, use [NewEventClient] instead. -func MustEventClient() *EventClient { - return assert.Must1(NewEventClient(helpers.MustSocket(".socket2.sock"))) +// will not panic on error, use [NewClient] instead. +func MustClient() *EventClient { + return assert.Must1(NewClient(helpers.MustSocket(".socket2.sock"))) } // Initiate a new event client. // Receive as parameters a socket that is generally localised in // '$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock'. -func NewEventClient(socket string) (*EventClient, error) { +func NewClient(socket string) (*EventClient, error) { conn, err := net.Dial("unix", socket) if err != nil { return nil, fmt.Errorf("error while connecting to socket: %w", err) diff --git a/event/event_test.go b/event/event_test.go index 85170d7..e747966 100644 --- a/event/event_test.go +++ b/event/event_test.go @@ -31,7 +31,8 @@ func TestReceive(t *testing.T) { }() // We must capture this event - c := MustEventClient() + c := MustClient() + defer c.Close() data, err := c.Receive() assert.NoError(t, err) From 48aa02e1821f6a2c05a9d630129d137e11793a51 Mon Sep 17 00:00:00 2001 From: Thiago Kenji Okada Date: Sun, 1 Sep 2024 00:47:43 +0100 Subject: [PATCH 3/3] examples/events: close connection --- examples/events/events.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/examples/events/events.go b/examples/events/events.go index 1a2b2b4..f2befd1 100644 --- a/examples/events/events.go +++ b/examples/events/events.go @@ -20,7 +20,9 @@ func (e *ev) ActiveWindow(w event.ActiveWindow) { } func main() { - c := event.MustEventClient() + c := event.MustClient() + defer c.Close() + event.Subscribe( c, &ev{}, event.EventWorkspace,