forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
111 lines (92 loc) · 2.15 KB
/
context.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
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
package rod
import (
"context"
"time"
)
// Context creates a clone with a context that inherits the previous one
func (b *Browser) Context(ctx context.Context) *Browser {
ctx, cancel := context.WithCancel(ctx)
if b.ctx != nil {
go func() {
<-b.ctx.Done()
cancel()
}()
}
newObj := *b
newObj.ctx = ctx
newObj.ctxCancel = cancel
return &newObj
}
// GetContext returns the current context
func (b *Browser) GetContext() context.Context {
return b.ctx
}
// Timeout for chained sub-operations
func (b *Browser) Timeout(d time.Duration) *Browser {
ctx, cancel := context.WithTimeout(b.ctx, d)
b.timeoutCancel = cancel
return b.Context(ctx)
}
// CancelTimeout context
func (b *Browser) CancelTimeout() *Browser {
b.timeoutCancel()
return b
}
// Context creates a clone with a context that inherits the previous one
func (p *Page) Context(ctx context.Context) *Page {
ctx, cancel := context.WithCancel(ctx)
if p.ctx != nil {
go func() {
<-p.ctx.Done()
cancel()
}()
}
newObj := *p
newObj.ctx = ctx
newObj.ctxCancel = cancel
return &newObj
}
// GetContext returns the current context
func (p *Page) GetContext() context.Context {
return p.ctx
}
// Timeout for chained sub-operations
func (p *Page) Timeout(d time.Duration) *Page {
ctx, cancel := context.WithTimeout(p.ctx, d)
p.timeoutCancel = cancel
return p.Context(ctx)
}
// CancelTimeout context
func (p *Page) CancelTimeout() *Page {
p.timeoutCancel()
return p
}
// Context creates a clone with a context that inherits the previous one
func (el *Element) Context(ctx context.Context) *Element {
ctx, cancel := context.WithCancel(ctx)
if el.ctx != nil {
go func() {
<-el.ctx.Done()
cancel()
}()
}
newObj := *el
newObj.ctx = ctx
newObj.ctxCancel = cancel
return &newObj
}
// GetContext returns the current context
func (el *Element) GetContext() context.Context {
return el.ctx
}
// Timeout for chained sub-operations
func (el *Element) Timeout(d time.Duration) *Element {
ctx, cancel := context.WithTimeout(el.ctx, d)
el.timeoutCancel = cancel
return el.Context(ctx)
}
// CancelTimeout context
func (el *Element) CancelTimeout() *Element {
el.timeoutCancel()
return el
}