forked from go-rod/rod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
128 lines (107 loc) · 3.76 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package rod
import (
"context"
"time"
"github.com/go-rod/rod/lib/utils"
)
type timeoutContextKey struct{}
type timeoutContextVal struct {
parent context.Context
cancel context.CancelFunc
}
// Context returns a clone with the specified ctx for chained sub-operations
func (b *Browser) Context(ctx context.Context) *Browser {
newObj := *b
newObj.ctx = ctx
return &newObj
}
// GetContext of current instance
func (b *Browser) GetContext() context.Context {
return b.ctx
}
// Timeout returns a clone with the specified total timeout of all chained sub-operations
func (b *Browser) Timeout(d time.Duration) *Browser {
ctx, cancel := context.WithTimeout(b.ctx, d)
return b.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{b.ctx, cancel}))
}
// CancelTimeout cancels the current timeout context and returns a clone with the parent context
func (b *Browser) CancelTimeout() *Browser {
val := b.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)
val.cancel()
return b.Context(val.parent)
}
// WithCancel returns a clone with a context cancel function
func (b *Browser) WithCancel() (*Browser, func()) {
ctx, cancel := context.WithCancel(b.ctx)
return b.Context(ctx), cancel
}
// Sleeper returns a clone with the specified sleeper for chained sub-operations
func (b *Browser) Sleeper(sleeper func() utils.Sleeper) *Browser {
newObj := *b
newObj.sleeper = sleeper
return &newObj
}
// Context returns a clone with the specified ctx for chained sub-operations
func (p *Page) Context(ctx context.Context) *Page {
newObj := *p
newObj.ctx = ctx
return &newObj
}
// GetContext of current instance
func (p *Page) GetContext() context.Context {
return p.ctx
}
// Timeout returns a clone with the specified total timeout of all chained sub-operations
func (p *Page) Timeout(d time.Duration) *Page {
ctx, cancel := context.WithTimeout(p.ctx, d)
return p.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{p.ctx, cancel}))
}
// CancelTimeout cancels the current timeout context and returns a clone with the parent context
func (p *Page) CancelTimeout() *Page {
val := p.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)
val.cancel()
return p.Context(val.parent)
}
// WithCancel returns a clone with a context cancel function
func (p *Page) WithCancel() (*Page, func()) {
ctx, cancel := context.WithCancel(p.ctx)
return p.Context(ctx), cancel
}
// Sleeper returns a clone with the specified sleeper for chained sub-operations
func (p *Page) Sleeper(sleeper func() utils.Sleeper) *Page {
newObj := *p
newObj.sleeper = sleeper
return &newObj
}
// Context returns a clone with the specified ctx for chained sub-operations
func (el *Element) Context(ctx context.Context) *Element {
newObj := *el
newObj.ctx = ctx
return &newObj
}
// GetContext of current instance
func (el *Element) GetContext() context.Context {
return el.ctx
}
// Timeout returns a clone with the specified total timeout of all chained sub-operations
func (el *Element) Timeout(d time.Duration) *Element {
ctx, cancel := context.WithTimeout(el.ctx, d)
return el.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{el.ctx, cancel}))
}
// CancelTimeout cancels the current timeout context and returns a clone with the parent context
func (el *Element) CancelTimeout() *Element {
val := el.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)
val.cancel()
return el.Context(val.parent)
}
// WithCancel returns a clone with a context cancel function
func (el *Element) WithCancel() (*Element, func()) {
ctx, cancel := context.WithCancel(el.ctx)
return el.Context(ctx), cancel
}
// Sleeper returns a clone with the specified sleeper for chained sub-operations
func (el *Element) Sleeper(sleeper func() utils.Sleeper) *Element {
newObj := *el
newObj.sleeper = sleeper
return &newObj
}