-
Notifications
You must be signed in to change notification settings - Fork 3
/
app_runtime.go
41 lines (36 loc) · 958 Bytes
/
app_runtime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main
import (
"context"
"fmt"
"github.com/wailsapp/wails/v2/pkg/runtime"
"golang.org/x/exp/rand"
)
type AppRuntime struct {
conversationID int
app *App
}
func (r *AppRuntime) WaitForApproval(ctx context.Context, message string) error {
approvalChan := make(chan struct{}, 1)
approvalID := make([]byte, 8)
rand.Read(approvalID)
r.app.m.Lock()
r.app.pendingApprovalRequests[r.conversationID] = approvalRequest{
approvalID: fmt.Sprintf("%x", approvalID),
approvalChan: approvalChan,
message: message,
}
r.app.m.Unlock()
runtime.EventsEmit(ctx, fmt.Sprintf("conversation-%d-approvals-updated", r.conversationID))
defer func() {
r.app.m.Lock()
delete(r.app.pendingApprovalRequests, r.conversationID)
r.app.m.Unlock()
runtime.EventsEmit(ctx, fmt.Sprintf("conversation-%d-approvals-updated", r.conversationID))
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-approvalChan:
return nil
}
}