...
1 package playwright
2
3 type workerImpl struct {
4 channelOwner
5 page *pageImpl
6 context *browserContextImpl
7 }
8
9 func (w *workerImpl) URL() string {
10 return w.initializer["url"].(string)
11 }
12
13 func (w *workerImpl) Evaluate(expression string, options ...interface{}) (interface{}, error) {
14 var arg interface{}
15 forceExpression := false
16 if !isFunctionBody(expression) {
17 forceExpression = true
18 }
19 if len(options) == 1 {
20 arg = options[0]
21 } else if len(options) == 2 {
22 arg = options[0]
23 forceExpression = options[1].(bool)
24 }
25 result, err := w.channel.Send("evaluateExpression", map[string]interface{}{
26 "expression": expression,
27 "isFunction": !forceExpression,
28 "arg": serializeArgument(arg),
29 })
30 if err != nil {
31 return nil, err
32 }
33 return parseResult(result), nil
34 }
35
36 func (w *workerImpl) EvaluateHandle(expression string, options ...interface{}) (JSHandle, error) {
37 var arg interface{}
38 forceExpression := false
39 if !isFunctionBody(expression) {
40 forceExpression = true
41 }
42 if len(options) == 1 {
43 arg = options[0]
44 } else if len(options) == 2 {
45 arg = options[0]
46 forceExpression = options[1].(bool)
47 }
48 result, err := w.channel.Send("evaluateExpressionHandle", map[string]interface{}{
49 "expression": expression,
50 "isFunction": !forceExpression,
51 "arg": serializeArgument(arg),
52 })
53 if err != nil {
54 return nil, err
55 }
56 return fromChannel(result).(*jsHandleImpl), nil
57 }
58
59 func (w *workerImpl) onClose() {
60 if w.page != nil {
61 w.page.Lock()
62 workers := make([]Worker, 0)
63 for i := 0; i < len(w.page.workers); i++ {
64 if w.page.workers[i] != w {
65 workers = append(workers, w.page.workers[i])
66 }
67 }
68 w.page.workers = workers
69 w.page.Unlock()
70 }
71 if w.context != nil {
72 w.context.Lock()
73 workers := make([]*workerImpl, 0)
74 for i := 0; i < len(w.context.serviceWorkers); i++ {
75 if w.context.serviceWorkers[i] != w {
76 workers = append(workers, w.context.serviceWorkers[i])
77 }
78 }
79 w.context.serviceWorkers = workers
80 w.context.Unlock()
81 }
82 w.Emit("close", w)
83 }
84
85 func (w *workerImpl) WaitForEvent(event string, predicate ...interface{}) interface{} {
86 return <-waitForEvent(w, event, predicate...)
87 }
88
89 func (w *workerImpl) ExpectEvent(event string, cb func() error, predicates ...interface{}) (interface{}, error) {
90 args := []interface{}{event}
91 if len(predicates) == 1 {
92 args = append(args, predicates[0])
93 }
94 return newExpectWrapper(w.WaitForEvent, args, cb)
95 }
96
97 func newWorker(parent *channelOwner, objectType string, guid string, initializer map[string]interface{}) *workerImpl {
98 bt := &workerImpl{}
99 bt.createChannelOwner(bt, parent, objectType, guid, initializer)
100 bt.channel.On("close", bt.onClose)
101 return bt
102 }
103
View as plain text