chromedp/actions.go

50 lines
1.1 KiB
Go
Raw Normal View History

2017-01-24 15:09:23 +00:00
package chromedp
import (
"context"
"time"
2017-01-26 07:28:34 +00:00
"github.com/knq/chromedp/cdp"
2017-01-24 15:09:23 +00:00
)
// Action is a single atomic action.
type Action interface {
2017-01-26 07:28:34 +00:00
Do(context.Context, cdp.FrameHandler) error
2017-01-24 15:09:23 +00:00
}
// ActionFunc is a single action func.
2017-01-26 07:28:34 +00:00
type ActionFunc func(context.Context, cdp.FrameHandler) error
2017-01-24 15:09:23 +00:00
// Do executes the action using the provided context.
2017-01-26 07:28:34 +00:00
func (f ActionFunc) Do(ctxt context.Context, h cdp.FrameHandler) error {
2017-01-24 15:09:23 +00:00
return f(ctxt, h)
}
// Tasks is a list of Actions that can be used as a single Action.
type Tasks []Action
// Do executes the list of Tasks using the provided context.
2017-01-26 07:28:34 +00:00
func (t Tasks) Do(ctxt context.Context, h cdp.FrameHandler) error {
2017-01-24 15:09:23 +00:00
var err error
// TODO: put individual task timeouts from context here
for _, a := range t {
// ctxt, cancel = context.WithTimeout(ctxt, timeout)
// defer cancel()
err = a.Do(ctxt, h)
if err != nil {
return err
}
}
return nil
}
// Sleep is an empty action that calls time.Sleep with the specified duration.
func Sleep(d time.Duration) Action {
2017-01-26 07:28:34 +00:00
return ActionFunc(func(context.Context, cdp.FrameHandler) error {
2017-01-24 15:09:23 +00:00
time.Sleep(d)
return nil
})
}