-
-
Notifications
You must be signed in to change notification settings - Fork 156
/
channel.go
66 lines (60 loc) · 1.76 KB
/
channel.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
package playwright
type channel struct {
eventEmitter
guid string
connection *connection
owner *channelOwner // to avoid type conversion
object interface{} // retain type info (for fromChannel needed)
}
func (c *channel) Send(method string, options ...interface{}) (interface{}, error) {
return c.connection.WrapAPICall(func() (interface{}, error) {
return c.innerSend(method, false, options...)
}, false)
}
func (c *channel) SendReturnAsDict(method string, options ...interface{}) (interface{}, error) {
return c.connection.WrapAPICall(func() (interface{}, error) {
return c.innerSend(method, true, options...)
}, true)
}
func (c *channel) innerSend(method string, returnAsDict bool, options ...interface{}) (interface{}, error) {
params := transformOptions(options...)
callback, err := c.connection.sendMessageToServer(c.owner, method, params, false)
if err != nil {
return nil, err
}
result, err := callback.GetResult()
if err != nil {
return nil, err
}
if result == nil {
return nil, nil
}
if returnAsDict {
return result, nil
}
if mapV, ok := result.(map[string]interface{}); ok && len(mapV) <= 1 {
for key := range mapV {
return mapV[key], nil
}
return nil, nil
}
return result, nil
}
func (c *channel) SendNoReply(method string, options ...interface{}) {
params := transformOptions(options...)
_, err := c.connection.WrapAPICall(func() (interface{}, error) {
return c.connection.sendMessageToServer(c.owner, method, params, true)
}, false)
if err != nil {
logger.Printf("SendNoReply failed: %v\n", err)
}
}
func newChannel(owner *channelOwner, object interface{}) *channel {
channel := &channel{
connection: owner.connection,
guid: owner.guid,
owner: owner,
object: object,
}
return channel
}