forked from playwright-community/playwright-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
102 lines (94 loc) · 2.61 KB
/
worker.go
File metadata and controls
102 lines (94 loc) · 2.61 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
package playwright
type workerImpl struct {
channelOwner
page *pageImpl
context *browserContextImpl
}
func (w *workerImpl) URL() string {
return w.initializer["url"].(string)
}
func (w *workerImpl) Evaluate(expression string, options ...interface{}) (interface{}, error) {
var arg interface{}
forceExpression := false
if !isFunctionBody(expression) {
forceExpression = true
}
if len(options) == 1 {
arg = options[0]
} else if len(options) == 2 {
arg = options[0]
forceExpression = options[1].(bool)
}
result, err := w.channel.Send("evaluateExpression", map[string]interface{}{
"expression": expression,
"isFunction": !forceExpression,
"arg": serializeArgument(arg),
})
if err != nil {
return nil, err
}
return parseResult(result), nil
}
func (w *workerImpl) EvaluateHandle(expression string, options ...interface{}) (JSHandle, error) {
var arg interface{}
forceExpression := false
if !isFunctionBody(expression) {
forceExpression = true
}
if len(options) == 1 {
arg = options[0]
} else if len(options) == 2 {
arg = options[0]
forceExpression = options[1].(bool)
}
result, err := w.channel.Send("evaluateExpressionHandle", map[string]interface{}{
"expression": expression,
"isFunction": !forceExpression,
"arg": serializeArgument(arg),
})
if err != nil {
return nil, err
}
return fromChannel(result).(*jsHandleImpl), nil
}
func (w *workerImpl) onClose() {
if w.page != nil {
w.page.Lock()
workers := make([]Worker, 0)
for i := 0; i < len(w.page.workers); i++ {
if w.page.workers[i] != w {
workers = append(workers, w.page.workers[i])
}
}
w.page.workers = workers
w.page.Unlock()
}
if w.context != nil {
w.context.Lock()
workers := make([]*workerImpl, 0)
for i := 0; i < len(w.context.serviceWorkers); i++ {
if w.context.serviceWorkers[i] != w {
workers = append(workers, w.context.serviceWorkers[i])
}
}
w.context.serviceWorkers = workers
w.context.Unlock()
}
w.Emit("close", w)
}
func (w *workerImpl) WaitForEvent(event string, predicate ...interface{}) interface{} {
return <-waitForEvent(w, event, predicate...)
}
func (w *workerImpl) ExpectEvent(event string, cb func() error, predicates ...interface{}) (interface{}, error) {
args := []interface{}{event}
if len(predicates) == 1 {
args = append(args, predicates[0])
}
return newExpectWrapper(w.WaitForEvent, args, cb)
}
func newWorker(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *workerImpl {
bt := &workerImpl{}
bt.createChannelOwner(bt, parent, objectType, guid, initializer)
bt.channel.On("close", bt.onClose)
return bt
}