Skip to content

Commit

Permalink
Merge pull request #21 from thiagokokada/add-close
Browse files Browse the repository at this point in the history
Add event.Close()
  • Loading branch information
thiagokokada authored Aug 31, 2024
2 parents 5238002 + 48aa02e commit f4d1b2c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
13 changes: 9 additions & 4 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,27 @@ 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)
}
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) {
Expand Down
3 changes: 2 additions & 1 deletion event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion examples/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit f4d1b2c

Please sign in to comment.