WaitForClient improved

This commit is contained in:
Milarin 2024-08-26 21:48:18 +02:00
parent 9fad501f41
commit 8c1df2310c
2 changed files with 28 additions and 7 deletions

View File

@ -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
}

View File

@ -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
}
}
}