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

Add event.Close() #21

Merged
merged 3 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
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
Loading