Repository navigation
Expand file tree
/
Copy pathchannel.go
More file actions
151 lines (134 loc) · 5.01 KB
/
Copy pathchannel.go
File metadata and controls
151 lines (134 loc) · 5.01 KB
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
package playwright
import (
"encoding/json"
"fmt"
)
type channel struct {
eventEmitter
guid string
connection *connection
owner *channelOwner // to avoid type conversion
object any // retain type info (for fromChannel needed)
}
// protocolCallOptions describes transport-level behavior for a single
// protocol call. timeoutAware is intentionally independent from timeout: a
// timeout-aware protocol method must strip a public `params.timeout` even when
// the caller deliberately omits metadata.timeout.
type protocolCallOptions struct {
timeout *float64
timeoutAware bool
}
func (c *channel) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]string{
"guid": c.guid,
})
}
// for catch errors of route handlers etc.
func (c *channel) CreateTask(fn func()) {
go func() {
defer func() {
if e := recover(); e != nil {
err, ok := e.(error)
if ok {
c.connection.err.Set(err)
} else {
c.connection.err.Set(fmt.Errorf("%v", e))
}
}
}()
fn()
}()
}
func (c *channel) Send(method string, options ...any) (any, error) {
return c.send(method, protocolCallOptions{}, options...)
}
// SendWithTimeout sends a protocol method with the call timeout carried in
// metadata.timeout (Playwright ≥1.62). timeout may be a pointer to zero
// (unlimited); a nil timeout omits metadata.timeout. Any "timeout" key present
// in the transformed params is stripped so it is not double-sent as a param.
func (c *channel) SendWithTimeout(method string, timeout *float64, options ...any) (any, error) {
return c.send(method, protocolCallOptions{timeout: timeout, timeoutAware: true}, options...)
}
func (c *channel) send(method string, callOptions protocolCallOptions, options ...any) (any, error) {
return c.connection.WrapAPICall(func() (any, error) {
result, err := c.innerSend(method, callOptions, options...).GetResultValue()
if err != nil {
return nil, err
}
// GUIDs are now always eagerly resolved in connection.Dispatch
return result, nil
}, c.owner.isInternalType)
}
func (c *channel) SendReturnAsDict(method string, options ...any) (map[string]any, error) {
return c.sendReturnAsDict(method, protocolCallOptions{}, options...)
}
// SendReturnAsDictWithTimeout is the timeout-aware form of SendReturnAsDict.
func (c *channel) SendReturnAsDictWithTimeout(method string, timeout *float64, options ...any) (map[string]any, error) {
return c.sendReturnAsDict(method, protocolCallOptions{timeout: timeout, timeoutAware: true}, options...)
}
func (c *channel) sendReturnAsDict(method string, callOptions protocolCallOptions, options ...any) (map[string]any, error) {
ret, err := c.connection.WrapAPICall(func() (any, error) {
result, err := c.innerSend(method, callOptions, options...).GetResult()
if err != nil {
return nil, err
}
// GUIDs are now always eagerly resolved in connection.Dispatch
return result, nil
}, c.owner.isInternalType)
if err != nil {
return nil, err
}
if ret == nil {
return make(map[string]any), nil
}
return ret.(map[string]any), nil
}
func (c *channel) innerSend(method string, callOptions protocolCallOptions, options ...any) *protocolCallback {
if err := c.connection.err.Get(); err != nil {
c.connection.err.Set(nil)
pc := newProtocolCallback(c.connection, false, c.connection.abort)
pc.SetError(err)
return pc
}
params := transformOptions(options...)
if callOptions.timeoutAware {
// Timeout-aware boundary: call timeout travels in metadata, not params.
delete(params, "timeout")
}
return c.connection.sendMessageToServer(c.owner, method, params, false, callOptions.timeout)
}
// SendNoReply ignores return value and errors
// almost equivalent to `send(...).catch(() => {})`
func (c *channel) SendNoReply(method string, options ...any) {
c.innerSendNoReply(method, c.owner.isInternalType, protocolCallOptions{}, options...)
}
func (c *channel) SendNoReplyInternal(method string, options ...any) {
c.innerSendNoReply(method, true, protocolCallOptions{}, options...)
}
// SendNoReplyInternalWithTimeout is a fire-and-forget send that still carries
// metadata.timeout when needed (e.g. some internal driver ops).
func (c *channel) SendNoReplyInternalWithTimeout(method string, timeout *float64, options ...any) {
c.innerSendNoReply(method, true, protocolCallOptions{timeout: timeout, timeoutAware: true}, options...)
}
func (c *channel) innerSendNoReply(method string, isInternal bool, callOptions protocolCallOptions, options ...any) {
params := transformOptions(options...)
if callOptions.timeoutAware {
delete(params, "timeout")
}
_, err := c.connection.WrapAPICall(func() (any, error) {
return c.connection.sendMessageToServer(c.owner, method, params, true, callOptions.timeout).GetResult()
}, isInternal)
if err != nil {
// ignore error actively, log only for debug
logger.Error("SendNoReply failed", "error", err)
}
}
func newChannel(owner *channelOwner, object any) *channel {
channel := &channel{
connection: owner.connection,
guid: owner.guid,
owner: owner,
object: object,
}
return channel
}