vendor/github.com/go-rod/rod/context.go

132 lines · 3997 bytes

  1package rod
  2
  3import (
  4	"context"
  5	"time"
  6
  7	"github.com/go-rod/rod/lib/utils"
  8)
  9
 10type (
 11	timeoutContextKey struct{}
 12	timeoutContextVal struct {
 13		parent context.Context
 14		cancel context.CancelFunc
 15	}
 16)
 17
 18// Context returns a clone with the specified ctx for chained sub-operations.
 19func (b *Browser) Context(ctx context.Context) *Browser {
 20	newObj := *b
 21	newObj.ctx = ctx
 22	return &newObj
 23}
 24
 25// GetContext of current instance.
 26func (b *Browser) GetContext() context.Context {
 27	return b.ctx
 28}
 29
 30// Timeout returns a clone with the specified total timeout of all chained sub-operations.
 31func (b *Browser) Timeout(d time.Duration) *Browser {
 32	ctx, cancel := context.WithTimeout(b.ctx, d)
 33	return b.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{b.ctx, cancel}))
 34}
 35
 36// CancelTimeout cancels the current timeout context and returns a clone with the parent context.
 37func (b *Browser) CancelTimeout() *Browser {
 38	val := b.ctx.Value(timeoutContextKey{}).(*timeoutContextVal) //nolint:forcetypeassert
 39	val.cancel()
 40	return b.Context(val.parent)
 41}
 42
 43// WithCancel returns a clone with a context cancel function.
 44func (b *Browser) WithCancel() (*Browser, func()) {
 45	ctx, cancel := context.WithCancel(b.ctx)
 46	return b.Context(ctx), cancel
 47}
 48
 49// Sleeper returns a clone with the specified sleeper for chained sub-operations.
 50func (b *Browser) Sleeper(sleeper func() utils.Sleeper) *Browser {
 51	newObj := *b
 52	newObj.sleeper = sleeper
 53	return &newObj
 54}
 55
 56// Context returns a clone with the specified ctx for chained sub-operations.
 57func (p *Page) Context(ctx context.Context) *Page {
 58	p.helpersLock.Lock()
 59	newObj := *p
 60	p.helpersLock.Unlock()
 61	newObj.ctx = ctx
 62	return &newObj
 63}
 64
 65// GetContext of current instance.
 66func (p *Page) GetContext() context.Context {
 67	return p.ctx
 68}
 69
 70// Timeout returns a clone with the specified total timeout of all chained sub-operations.
 71func (p *Page) Timeout(d time.Duration) *Page {
 72	ctx, cancel := context.WithTimeout(p.ctx, d)
 73	return p.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{p.ctx, cancel}))
 74}
 75
 76// CancelTimeout cancels the current timeout context and returns a clone with the parent context.
 77func (p *Page) CancelTimeout() *Page {
 78	val := p.ctx.Value(timeoutContextKey{}).(*timeoutContextVal) //nolint: forcetypeassert
 79	val.cancel()
 80	return p.Context(val.parent)
 81}
 82
 83// WithCancel returns a clone with a context cancel function.
 84func (p *Page) WithCancel() (*Page, func()) {
 85	ctx, cancel := context.WithCancel(p.ctx)
 86	return p.Context(ctx), cancel
 87}
 88
 89// Sleeper returns a clone with the specified sleeper for chained sub-operations.
 90func (p *Page) Sleeper(sleeper func() utils.Sleeper) *Page {
 91	newObj := *p
 92	newObj.sleeper = sleeper
 93	return &newObj
 94}
 95
 96// Context returns a clone with the specified ctx for chained sub-operations.
 97func (el *Element) Context(ctx context.Context) *Element {
 98	newObj := *el
 99	newObj.ctx = ctx
100	return &newObj
101}
102
103// GetContext of current instance.
104func (el *Element) GetContext() context.Context {
105	return el.ctx
106}
107
108// Timeout returns a clone with the specified total timeout of all chained sub-operations.
109func (el *Element) Timeout(d time.Duration) *Element {
110	ctx, cancel := context.WithTimeout(el.ctx, d)
111	return el.Context(context.WithValue(ctx, timeoutContextKey{}, &timeoutContextVal{el.ctx, cancel}))
112}
113
114// CancelTimeout cancels the current timeout context and returns a clone with the parent context.
115func (el *Element) CancelTimeout() *Element {
116	val := el.ctx.Value(timeoutContextKey{}).(*timeoutContextVal) //nolint: forcetypeassert
117	val.cancel()
118	return el.Context(val.parent)
119}
120
121// WithCancel returns a clone with a context cancel function.
122func (el *Element) WithCancel() (*Element, func()) {
123	ctx, cancel := context.WithCancel(el.ctx)
124	return el.Context(ctx), cancel
125}
126
127// Sleeper returns a clone with the specified sleeper for chained sub-operations.
128func (el *Element) Sleeper(sleeper func() utils.Sleeper) *Element {
129	newObj := *el
130	newObj.sleeper = sleeper
131	return &newObj
132}