forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
63 lines (56 loc) · 1.48 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
package playwright
import (
"fmt"
"log"
"reflect"
)
type channel struct {
eventEmitter
guid string
connection *connection
object interface{}
}
func (c *channel) Send(method string, options ...interface{}) (interface{}, error) {
return c.innerSend(method, false, options...)
}
func (c *channel) SendReturnAsDict(method string, options ...interface{}) (interface{}, error) {
return c.innerSend(method, true, options...)
}
func (c *channel) innerSend(method string, returnAsDict bool, options ...interface{}) (interface{}, error) {
params := transformOptions(options...)
result, err := c.connection.SendMessageToServer(c.guid, method, params)
if err != nil {
return nil, fmt.Errorf("could not send message to server: %w", err)
}
if result == nil {
return nil, nil
}
if returnAsDict {
return result, nil
}
if reflect.TypeOf(result).Kind() == reflect.Map {
mapV := result.(map[string]interface{})
if len(mapV) == 0 {
return nil, nil
}
for key := range mapV {
return mapV[key], nil
}
}
return result, nil
}
func (c *channel) SendNoReply(method string, options ...interface{}) {
params := transformOptions(options...)
_, err := c.connection.SendMessageToServer(c.guid, method, params)
if err != nil {
log.Printf("could not send message to server from noreply: %v", err)
}
}
func newChannel(connection *connection, guid string) *channel {
channel := &channel{
connection: connection,
guid: guid,
}
channel.initEventEmitter()
return channel
}