diff --git a/get_clients.go b/get_clients.go index 6983cb9..3764c63 100644 --- a/get_clients.go +++ b/get_clients.go @@ -70,11 +70,26 @@ func GetClients() ([]*Client, error) { return clients, nil } -func WaitForClient(ctx context.Context, signature string) { +func WaitForClient(ctx context.Context, signature string) (*Client, error) { lockFilePath := fmt.Sprintf("/run/user/1000/hypr/%s/hyprland.lock", signature) - waitFor(func() bool { + lockFileExists := !waitFor(ctx, func() bool { _, err := os.Stat(lockFilePath) return !errors.Is(err, os.ErrNotExist) }) + + if !lockFileExists { + return nil, errors.New("hyprland lock file not found") + } + + client, err := GetClient(signature) + if err != nil { + return nil, err + } + + if client.WaitUntilReachable(context.Background()) { + return nil, errors.New("hyprland not reachable") + } + + return client, nil } diff --git a/utils.go b/utils.go index ce670d1..7ab3ddc 100644 --- a/utils.go +++ b/utils.go @@ -1,6 +1,7 @@ package hypr import ( + "context" "encoding/json" "io" "net" @@ -51,17 +52,22 @@ func readSocket[T any](socket string, body io.Reader) (T, error) { return *value, nil } -func waitFor(condition func() bool) { +func waitFor(ctx context.Context, condition func() bool) bool { if condition() { - return + return true } ticker := time.NewTicker(100 * time.Millisecond) defer ticker.Stop() - for range ticker.C { - if condition() { - break + for { + select { + case <-ticker.C: + if condition() { + return true + } + case <-ctx.Done(): + return false } } }