forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binding_call.go
64 lines (56 loc) · 1.75 KB
/
binding_call.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
package playwright
import (
"log"
)
type bindingCallImpl struct {
channelOwner
}
// BindingSource is the value passed to a binding call execution
type BindingSource struct {
Context BrowserContext
Page Page
Frame Frame
}
// ExposedFunction represents the func signature of an exposed function
type ExposedFunction = func(args ...interface{}) interface{}
// BindingCallFunction represents the func signature of an exposed binding call func
type BindingCallFunction func(source *BindingSource, args ...interface{}) interface{}
func (b *bindingCallImpl) Call(f BindingCallFunction) {
defer func() {
if r := recover(); r != nil {
if _, err := b.channel.Send("reject", map[string]interface{}{
"error": serializeError(r.(error)),
}); err != nil {
log.Printf("could not reject BindingCall: %v", err)
}
}
}()
frame := fromChannel(b.initializer["frame"]).(*frameImpl)
source := &BindingSource{
Context: frame.Page().Context(),
Page: frame.Page(),
Frame: frame,
}
var result interface{}
if handle, ok := b.initializer["handle"]; ok {
result = f(source, fromChannel(handle))
} else {
initializerArgs := b.initializer["args"].([]interface{})
funcArgs := []interface{}{}
for i := 0; i < len(initializerArgs); i++ {
funcArgs = append(funcArgs, parseResult(initializerArgs[i]))
}
result = f(source, funcArgs...)
}
_, err := b.channel.Send("resolve", map[string]interface{}{
"result": serializeArgument(result),
})
if err != nil {
log.Printf("could not resolve BindingCall: %v", err)
}
}
func newBindingCall(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *bindingCallImpl {
bt := &bindingCallImpl{}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
return bt
}