IsReachable and WaitUntilReachable implemented

This commit is contained in:
Milarin 2024-02-19 10:02:33 +01:00
parent cfc682a36f
commit 9bea148d40
2 changed files with 41 additions and 4 deletions

37
check_reachable.go Normal file
View File

@ -0,0 +1,37 @@
package hypr
import (
"context"
"strings"
"time"
)
// IsReachable pings the unix socket to check
// if Hyprland is ready to accept incoming requests
func (c *Client) IsReachable() bool {
str, err := readSocketString(c.SocketPath(), strings.NewReader("dispatch exec echo"))
if err != nil {
return false
}
return str == "ok"
}
// WaitUntilReachable periodically calls IsReachable
// and returns true as soon as IsReachable returns true.
// When ctx is closed before Hyprland is reachable, false is returned
func (c *Client) WaitUntilReachable(ctx context.Context) bool {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
select {
case <-ticker.C:
if c.IsReachable() {
return true
}
case <-ctx.Done():
return false
}
return false
}

View File

@ -6,12 +6,12 @@ import (
"strings"
)
func (i *Client) Dispatch(cmd string) (io.ReadCloser, error) {
return readSocketRaw(i.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd)))
func (c *Client) Dispatch(cmd string) (io.ReadCloser, error) {
return readSocketRaw(c.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd)))
}
func (i *Client) DispatchExpectOK(cmd string) error {
str, err := readSocketString(i.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd)))
func (c *Client) DispatchExpectOK(cmd string) error {
str, err := readSocketString(c.SocketPath(), strings.NewReader(fmt.Sprintf("dispatch %s", cmd)))
if err != nil {
return err
}