-
Notifications
You must be signed in to change notification settings - Fork 0
/
wait_for.go
70 lines (58 loc) · 1.68 KB
/
wait_for.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package realtime_pubsub
import (
"context"
"fmt"
"time"
)
// WaitFor provides methods to wait for acknowledgments or replies.
type WaitFor struct {
client WaitForClient
id string
}
// WaitForClient defines the interface that the client must implement.
type WaitForClient interface {
WaitFor(eventName string, timeout time.Duration) (interface{}, error)
}
// WaitForAck waits for an acknowledgment event with a timeout.
func (w *WaitFor) WaitForAck(timeout time.Duration) (interface{}, error) {
eventName := fmt.Sprintf("ack.%v", w.id)
return w.client.WaitFor(eventName, timeout)
}
// WaitForReply waits for a reply event with a timeout.
func (w *WaitFor) WaitForReply(timeout time.Duration) (ResponseMessage, error) {
eventName := fmt.Sprintf("response.%v", w.id)
// Wait for the reply event
result, err := w.client.WaitFor(eventName, timeout)
if err != nil {
return nil, err
}
// Extract the reply message directly
args, ok := result.([]interface{})
if !ok || len(args) == 0 {
return nil, fmt.Errorf("invalid reply format")
}
msg, ok := args[0].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("invalid reply message format")
}
return msg, nil
}
// WaitFor waits for a specific event to occur within a timeout period.
func (c *Client) WaitFor(eventName string, timeout time.Duration) (interface{}, error) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
ch := make(chan interface{}, 1)
listenerID := c.On(eventName, func(args ...interface{}) {
select {
case ch <- args:
default:
}
})
defer c.Off(eventName, listenerID)
select {
case result := <-ch:
return result, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}