1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-04-19 12:12:16 +02:00

70 lines
1.2 KiB
Go
Raw Normal View History

2018-09-25 17:58:57 -04:00
package events
2018-09-23 04:33:20 -04:00
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"github.com/MontFerret/ferret/pkg/runtime/values"
2018-09-25 17:58:57 -04:00
"github.com/MontFerret/ferret/pkg/stdlib/html/driver/browser/eval"
2018-09-23 04:33:20 -04:00
"github.com/mafredri/cdp"
"time"
)
type WaitTask struct {
client *cdp.Client
predicate string
timeout time.Duration
polling time.Duration
2018-09-23 04:33:20 -04:00
}
const DefaultPolling = time.Millisecond * time.Duration(200)
2018-09-23 04:33:20 -04:00
func NewWaitTask(
client *cdp.Client,
predicate string,
timeout time.Duration,
polling time.Duration,
2018-09-23 04:33:20 -04:00
) *WaitTask {
return &WaitTask{
client,
predicate,
2018-09-23 04:33:20 -04:00
timeout,
polling,
2018-09-23 04:33:20 -04:00
}
}
func (task *WaitTask) Run() (core.Value, error) {
timer := time.NewTimer(task.timeout)
for {
2018-09-23 04:33:20 -04:00
select {
case <-timer.C:
return values.None, core.ErrTimeout
2018-09-23 04:33:20 -04:00
default:
2018-09-25 17:58:57 -04:00
out, err := eval.Eval(
task.client,
task.predicate,
true,
false,
)
2018-09-23 04:33:20 -04:00
// JS expression failed
// terminating
if err != nil {
2018-09-23 04:33:20 -04:00
timer.Stop()
return values.None, err
2018-09-23 04:33:20 -04:00
}
// JS output is not empty
// terminating
2018-09-23 04:33:20 -04:00
if out != values.None {
timer.Stop()
return out, nil
2018-09-23 04:33:20 -04:00
}
// Nothing yet, let's wait before the next try
time.Sleep(task.polling)
2018-09-23 04:33:20 -04:00
}
}
}