Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 733ab54

Browse files
committed
Move WaitForEventOption parsing to mapping
1 parent fc7daac commit 733ab54

File tree

3 files changed

+48
-55
lines changed

3 files changed

+48
-55
lines changed

browser/browser_context_mapping.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"reflect"
7+
"time"
78

89
"github.com/grafana/sobek"
910

@@ -109,14 +110,12 @@ func mapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //nolin
109110
})
110111
},
111112
"waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) {
112-
ctx := vu.Context()
113-
popts := common.NewWaitForEventOptions(
114-
bc.Timeout(),
115-
)
116-
if err := popts.Parse(ctx, optsOrPredicate); err != nil {
117-
return nil, fmt.Errorf("parsing waitForEvent options: %w", err)
113+
popts, err := parseWaitForEventOptions(vu.Runtime(), optsOrPredicate, bc.Timeout())
114+
if err != nil {
115+
return nil, fmt.Errorf("parsing wait for event options: %w", err)
118116
}
119117

118+
ctx := vu.Context()
120119
return k6ext.Promise(ctx, func() (result any, reason error) {
121120
var runInTaskQueue func(p *common.Page) (bool, error)
122121
if popts.PredicateFn != nil {
@@ -206,3 +205,42 @@ func parseGrantPermissionOptions(rt *sobek.Runtime, opts sobek.Value) (common.Gr
206205

207206
return g, nil
208207
}
208+
209+
// parseWaitForEventOptions parses optsOrPredicate into a WaitForEventOptions.
210+
// It returns a WaitForEventOptions with the default timeout if optsOrPredicate is nil,
211+
// or not a callable predicate function.
212+
// It can parse only a callable predicate function or an object which contains a
213+
// callable predicate function and a timeout.
214+
func parseWaitForEventOptions(
215+
rt *sobek.Runtime, optsOrPredicate sobek.Value, defaultTime time.Duration,
216+
) (*common.WaitForEventOptions, error) {
217+
w := &common.WaitForEventOptions{
218+
Timeout: defaultTime,
219+
}
220+
221+
if !sobekValueExists(optsOrPredicate) {
222+
return w, nil
223+
}
224+
var isCallable bool
225+
w.PredicateFn, isCallable = sobek.AssertFunction(optsOrPredicate)
226+
if isCallable {
227+
return w, nil
228+
}
229+
230+
opts := optsOrPredicate.ToObject(rt)
231+
for _, k := range opts.Keys() {
232+
switch k {
233+
case "predicate":
234+
w.PredicateFn, isCallable = sobek.AssertFunction(opts.Get(k))
235+
if !isCallable {
236+
return nil, errors.New("predicate function is not callable")
237+
}
238+
case "timeout":
239+
w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
240+
default:
241+
return nil, fmt.Errorf("unknown option: %s", k)
242+
}
243+
}
244+
245+
return w, nil
246+
}

browser/sync_browser_context_mapping.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ func syncMapBrowserContext(vu moduleVU, bc *common.BrowserContext) mapping { //n
6161
"setHTTPCredentials": bc.SetHTTPCredentials, //nolint:staticcheck
6262
"setOffline": bc.SetOffline,
6363
"waitForEvent": func(event string, optsOrPredicate sobek.Value) (*sobek.Promise, error) {
64-
ctx := vu.Context()
65-
popts := common.NewWaitForEventOptions(
66-
bc.Timeout(),
67-
)
68-
if err := popts.Parse(ctx, optsOrPredicate); err != nil {
69-
return nil, fmt.Errorf("parsing waitForEvent options: %w", err)
64+
popts, err := parseWaitForEventOptions(vu.Runtime(), optsOrPredicate, bc.Timeout())
65+
if err != nil {
66+
return nil, fmt.Errorf("parsing wait for event options: %w", err)
7067
}
7168

69+
ctx := vu.Context()
7270
return k6ext.Promise(ctx, func() (result any, reason error) {
7371
var runInTaskQueue func(p *common.Page) (bool, error)
7472
if popts.PredicateFn != nil {

common/browser_context_options.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package common
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"time"
87

@@ -104,48 +103,6 @@ type WaitForEventOptions struct {
104103
PredicateFn sobek.Callable
105104
}
106105

107-
// NewWaitForEventOptions created a new instance of WaitForEventOptions with a
108-
// default timeout.
109-
func NewWaitForEventOptions(defaultTimeout time.Duration) *WaitForEventOptions {
110-
return &WaitForEventOptions{
111-
Timeout: defaultTimeout,
112-
}
113-
}
114-
115-
// Parse will parse the options or a callable predicate function. It can parse
116-
// only a callable predicate function or an object which contains a callable
117-
// predicate function and a timeout.
118-
func (w *WaitForEventOptions) Parse(ctx context.Context, optsOrPredicate sobek.Value) error {
119-
if !sobekValueExists(optsOrPredicate) {
120-
return nil
121-
}
122-
123-
var (
124-
isCallable bool
125-
rt = k6ext.Runtime(ctx)
126-
)
127-
128-
w.PredicateFn, isCallable = sobek.AssertFunction(optsOrPredicate)
129-
if isCallable {
130-
return nil
131-
}
132-
133-
opts := optsOrPredicate.ToObject(rt)
134-
for _, k := range opts.Keys() {
135-
switch k {
136-
case "predicate":
137-
w.PredicateFn, isCallable = sobek.AssertFunction(opts.Get(k))
138-
if !isCallable {
139-
return errors.New("predicate function is not callable")
140-
}
141-
case "timeout": //nolint:goconst
142-
w.Timeout = time.Duration(opts.Get(k).ToInteger()) * time.Millisecond
143-
}
144-
}
145-
146-
return nil
147-
}
148-
149106
// GrantPermissionsOptions is used by BrowserContext.GrantPermissions.
150107
type GrantPermissionsOptions struct {
151108
Origin string

0 commit comments

Comments
 (0)