Skip to content

Commit

Permalink
Improve mapPageOn naming
Browse files Browse the repository at this point in the history
Co-authored-by: Ankur <[email protected]>
  • Loading branch information
inancgumus and ankur22 committed Oct 11, 2024
1 parent 5224570 commit 7a10632
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions browser/page_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,22 +419,22 @@ func mapPage(vu moduleVU, p *common.Page) mapping { //nolint:gocognit,cyclop
}

// mapPageOn maps the requested page.on event to the Sobek runtime.
// It generalizes the handling of page.on events on a taskqueue.
func mapPageOn(vu moduleVU, p *common.Page) func(common.PageOnEventName, sobek.Callable) error { //nolint:funlen
// It generalizes the handling of page.on events.
func mapPageOn(vu moduleVU, p *common.Page) func(common.PageOnEventName, sobek.Callable) error {
rt := vu.Runtime()

pageOnEvents := map[common.PageOnEventName]struct {
mapp func(vu moduleVU, event common.PageOnEvent) mapping
prep func() error
wait bool // should we wait for the handler to complete?
init func() error // If set, runs before the event handler.
wait bool // Whether to wait for the handler to complete.
}{
common.EventPageConsoleAPICalled: {
mapp: mapConsoleMessage,
wait: false,
},
common.EventPageMetricCalled: {
mapp: mapMetricEvent,
prep: prepK6BrowserRegExChecker(rt),
init: prepK6BrowserRegExChecker(rt),
wait: true,
},
}
Expand All @@ -445,17 +445,17 @@ func mapPageOn(vu moduleVU, p *common.Page) func(common.PageOnEventName, sobek.C
return fmt.Errorf("unknown page on event: %q", eventName)
}

// Prepare the environment for the page.on event handler if necessary.
if pageOnEvent.prep != nil {
if err := pageOnEvent.prep(); err != nil {
// Initializes the environment for the event handler if necessary.
if pageOnEvent.init != nil {
if err := pageOnEvent.init(); err != nil {
return fmt.Errorf("initiating page.on('%s'): %w", eventName, err)
}
}

// Queue the event handler in the task queue.
// Wait for the handler to complete if necessary.
// Run the the event handler in the task queue to
// ensure that the handler is executed on the event loop.
tq := vu.taskQueueRegistry.get(vu.Context(), p.TargetID())
queueHandler := func(event common.PageOnEvent) {
eventHandler := func(event common.PageOnEvent) {
mapping := pageOnEvent.mapp(vu, event)

done := make(chan struct{})
Expand All @@ -479,9 +479,7 @@ func mapPageOn(vu moduleVU, p *common.Page) func(common.PageOnEventName, sobek.C
}
}

// Run the the event handler in the task queue to ensure that
// the handler is executed in the event loop.
return p.On(eventName, queueHandler) //nolint:wrapcheck
return p.On(eventName, eventHandler) //nolint:wrapcheck
}
}

Expand Down

0 comments on commit 7a10632

Please sign in to comment.