-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
waiter.go
181 lines (169 loc) · 3.95 KB
/
waiter.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package playwright
import (
"context"
"fmt"
"reflect"
"sync"
"sync/atomic"
"time"
)
type (
waiter struct {
mu sync.Mutex
timeout float64
fulfilled atomic.Bool
listeners []eventListener
errChan chan error
waitFunc func() (interface{}, error)
}
eventListener struct {
emitter EventEmitter
event string
handler interface{}
}
)
// RejectOnEvent sets the Waiter to return an error when an event occurs (and the predicate returns true)
func (w *waiter) RejectOnEvent(emitter EventEmitter, event string, err error, predicates ...interface{}) *waiter {
w.mu.Lock()
defer w.mu.Unlock()
if w.waitFunc != nil {
w.reject(fmt.Errorf("waiter: call RejectOnEvent before WaitForEvent"))
return w
}
handler := func(ev ...interface{}) {
if w.fulfilled.Load() {
return
}
if len(predicates) == 0 {
w.reject(err)
} else {
result := reflect.ValueOf(predicates[0]).Call([]reflect.Value{reflect.ValueOf(ev[0])})
if result[0].Bool() {
w.reject(err)
}
}
}
emitter.On(event, handler)
w.listeners = append(w.listeners, eventListener{
emitter: emitter,
event: event,
handler: handler,
})
return w
}
// WithTimeout sets timeout, in milliseconds, for the waiter. 0 means no timeout.
func (w *waiter) WithTimeout(timeout float64) *waiter {
w.mu.Lock()
defer w.mu.Unlock()
if w.waitFunc != nil {
w.reject(fmt.Errorf("waiter: please set timeout before WaitForEvent"))
return w
}
w.timeout = timeout
return w
}
// WaitForEvent sets the Waiter to return when an event occurs (and the predicate returns true)
func (w *waiter) WaitForEvent(emitter EventEmitter, event string, predicate interface{}) *waiter {
w.mu.Lock()
defer w.mu.Unlock()
if w.waitFunc != nil {
w.reject(fmt.Errorf("waiter: WaitForEvent can only be called once"))
return w
}
evChan := make(chan interface{}, 1)
handler := w.createHandler(evChan, predicate)
ctx, cancel := context.WithCancel(context.Background())
if w.timeout != 0 {
timeout := w.timeout
go func() {
select {
case <-time.After(time.Duration(timeout) * time.Millisecond):
err := fmt.Errorf("%w:Timeout %.2fms exceeded.", ErrTimeout, timeout)
w.reject(err)
return
case <-ctx.Done():
return
}
}()
}
emitter.On(event, handler)
w.listeners = append(w.listeners, eventListener{
emitter: emitter,
event: event,
handler: handler,
})
w.waitFunc = func() (interface{}, error) {
var (
err error
val interface{}
)
select {
case err = <-w.errChan:
break
case val = <-evChan:
break
}
cancel()
w.mu.Lock()
defer w.mu.Unlock()
for _, l := range w.listeners {
l.emitter.RemoveListener(l.event, l.handler)
}
close(evChan)
if err != nil {
return nil, err
}
return val, nil
}
return w
}
// Wait waits for the waiter to return. It needs to call WaitForEvent once first.
func (w *waiter) Wait() (interface{}, error) {
if w.waitFunc == nil {
return nil, fmt.Errorf("waiter: call WaitForEvent first")
}
return w.waitFunc()
}
// RunAndWait waits for the waiter to return after calls func.
func (w *waiter) RunAndWait(cb func() error) (interface{}, error) {
if w.waitFunc == nil {
return nil, fmt.Errorf("waiter: call WaitForEvent first")
}
if cb != nil {
if err := cb(); err != nil {
w.errChan <- err
}
}
return w.waitFunc()
}
func (w *waiter) createHandler(evChan chan<- interface{}, predicate interface{}) func(...interface{}) {
return func(ev ...interface{}) {
if w.fulfilled.Load() {
return
}
if predicate == nil || reflect.ValueOf(predicate).IsNil() {
w.fulfilled.Store(true)
if len(ev) == 1 {
evChan <- ev[0]
} else {
evChan <- nil
}
} else {
result := reflect.ValueOf(predicate).Call([]reflect.Value{reflect.ValueOf(ev[0])})
if result[0].Bool() {
w.fulfilled.Store(true)
evChan <- ev[0]
}
}
}
}
func (w *waiter) reject(err error) {
w.fulfilled.Store(true)
w.errChan <- err
}
func newWaiter() *waiter {
w := &waiter{
errChan: make(chan error, 1),
}
return w
}