-
-
Notifications
You must be signed in to change notification settings - Fork 332
/
context.go
95 lines (80 loc) · 2.57 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
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 creates a clone with a context that inherits the previous one
func (b *Browser) Context(ctx context.Context) *Browser {
newObj := *b
newObj.ctx = ctx
return &newObj
}
// Timeout for 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 context and reset the context to previous one
func (b *Browser) CancelTimeout() *Browser {
val := b.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)
val.cancel()
return b.Context(val.parent)
}
// Sleeper for chained sub-operations
func (b *Browser) Sleeper(sleeper func() utils.Sleeper) *Browser {
newObj := *b
newObj.sleeper = ensureSleeper(sleeper)
return &newObj
}
// Context creates a clone with a context that inherits the previous one
func (p *Page) Context(ctx context.Context) *Page {
newObj := *p
newObj.ctx = ctx
return &newObj
}
// Timeout for 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 context and reset the context to previous one
func (p *Page) CancelTimeout() *Page {
val := p.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)
val.cancel()
return p.Context(val.parent)
}
// Sleeper for chained sub-operations
func (p *Page) Sleeper(sleeper func() utils.Sleeper) *Page {
newObj := *p
newObj.sleeper = ensureSleeper(sleeper)
return &newObj
}
// Context creates a clone with a context that inherits the previous one
func (el *Element) Context(ctx context.Context) *Element {
newObj := *el
newObj.ctx = ctx
return &newObj
}
// Timeout for 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 context and reset the context to previous one
func (el *Element) CancelTimeout() *Element {
val := el.ctx.Value(timeoutContextKey{}).(*timeoutContextVal)
val.cancel()
return el.Context(val.parent)
}
// Sleeper for chained sub-operations
func (el *Element) Sleeper(sleeper func() utils.Sleeper) *Element {
newObj := *el
newObj.sleeper = ensureSleeper(sleeper)
return &newObj
}