WaitForClient improved
This commit is contained in:
parent
9fad501f41
commit
8c1df2310c
@ -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
|
||||
}
|
||||
|
16
utils.go
16
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user