...
1 package playwright
2
3 import (
4 "fmt"
5 "log"
6 "reflect"
7 )
8
9 type channel struct {
10 eventEmitter
11 guid string
12 connection *connection
13 object interface{}
14 }
15
16 func (c *channel) Send(method string, options ...interface{}) (interface{}, error) {
17 return c.innerSend(method, false, options...)
18 }
19
20 func (c *channel) SendReturnAsDict(method string, options ...interface{}) (interface{}, error) {
21 return c.innerSend(method, true, options...)
22 }
23
24 func (c *channel) innerSend(method string, returnAsDict bool, options ...interface{}) (interface{}, error) {
25 params := transformOptions(options...)
26 result, err := c.connection.SendMessageToServer(c.guid, method, params)
27 if err != nil {
28 return nil, fmt.Errorf("could not send message to server: %w", err)
29 }
30 if result == nil {
31 return nil, nil
32 }
33 if returnAsDict {
34 return result, nil
35 }
36 if reflect.TypeOf(result).Kind() == reflect.Map {
37 mapV := result.(map[string]interface{})
38 if len(mapV) == 0 {
39 return nil, nil
40 }
41 for key := range mapV {
42 return mapV[key], nil
43 }
44 }
45 return result, nil
46 }
47
48 func (c *channel) SendNoReply(method string, options ...interface{}) {
49 params := transformOptions(options...)
50 _, err := c.connection.SendMessageToServer(c.guid, method, params)
51 if err != nil {
52 log.Printf("could not send message to server from noreply: %v", err)
53 }
54 }
55
56 func newChannel(connection *connection, guid string) *channel {
57 channel := &channel{
58 connection: connection,
59 guid: guid,
60 }
61 channel.initEventEmitter()
62 return channel
63 }
64
View as plain text