From 9fad501f412cf8d5bba4e57f218a61ed32a4fd66 Mon Sep 17 00:00:00 2001 From: Milarin Date: Mon, 26 Aug 2024 21:43:18 +0200 Subject: [PATCH] WaitForClient implemented --- get_clients.go | 10 ++++++++++ utils.go | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/get_clients.go b/get_clients.go index 9f1613e..6983cb9 100644 --- a/get_clients.go +++ b/get_clients.go @@ -1,6 +1,7 @@ package hypr import ( + "context" "errors" "fmt" "io" @@ -68,3 +69,12 @@ func GetClients() ([]*Client, error) { return clients, nil } + +func WaitForClient(ctx context.Context, signature string) { + lockFilePath := fmt.Sprintf("/run/user/1000/hypr/%s/hyprland.lock", signature) + + waitFor(func() bool { + _, err := os.Stat(lockFilePath) + return !errors.Is(err, os.ErrNotExist) + }) +} diff --git a/utils.go b/utils.go index 8823d53..ce670d1 100644 --- a/utils.go +++ b/utils.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io" "net" + "time" ) func readSocketRaw(socket string, body io.Reader) (io.ReadCloser, error) { @@ -49,3 +50,18 @@ func readSocket[T any](socket string, body io.Reader) (T, error) { return *value, nil } + +func waitFor(condition func() bool) { + if condition() { + return + } + + ticker := time.NewTicker(100 * time.Millisecond) + defer ticker.Stop() + + for range ticker.C { + if condition() { + break + } + } +}