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

Commit cac0206

Browse files
Ivan Mirićimiric
authored andcommitted
Do waitForFunction predicate truthy check inside injected_script.js
This avoids having to export continuePolling and doing the check from the wrapper function in Go, since it's really a JS implementation detail. It also hopefully avoids the need for a comment explaining internal details of injected_script[1]. [1]: https://github.com/grafana/xk6-browser/pull/294/files#r866750362
1 parent d65182e commit cac0206

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

common/frame.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,7 @@ func (f *Frame) waitForFunction(
591591

592592
pageFn := `
593593
(injected, predicate, polling, timeout, ...args) => {
594-
const fn = (...args) => {
595-
return predicate(...args) || injected.continuePolling;
596-
}
597-
return injected.waitForPredicateFunction(fn, polling, timeout, ...args);
594+
return injected.waitForPredicateFunction(predicate, polling, timeout, ...args);
598595
}
599596
`
600597

common/js/injected_script.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ class InjectedScript {
138138
constructor() {
139139
this._replaceRafWithTimeout = false;
140140
this._stableRafCount = 10;
141-
this.continuePolling = continuePolling;
142141
this._queryEngines = {
143142
'css': new CSSQueryEngine(),
144143
'text': new TextQueryEngine(),
@@ -587,9 +586,12 @@ class InjectedScript {
587586
return 'done';
588587
}
589588

590-
async waitForPredicateFunction(predicate, polling, timeout, ...args) {
589+
async waitForPredicateFunction(predicateFn, polling, timeout, ...args) {
591590
let timedOut = false;
592591
let timeoutPoll = null;
592+
const predicate = () => {
593+
return predicateFn(...args) || continuePolling;
594+
};
593595
if (timeout !== undefined || timeout !== null) {
594596
setTimeout(() => {
595597
timedOut = true;
@@ -601,7 +603,7 @@ class InjectedScript {
601603
if (typeof polling === 'number') return await pollInterval(polling);
602604

603605
async function pollMutation() {
604-
const success = predicate(...args);
606+
const success = predicate();
605607
if (success !== continuePolling) return Promise.resolve(success);
606608

607609
let resolve, reject;
@@ -614,7 +616,7 @@ class InjectedScript {
614616
observer.disconnect();
615617
reject(`timed out after ${timeout}ms`);
616618
}
617-
const success = predicate(...args);
619+
const success = predicate();
618620
if (success !== continuePolling) {
619621
observer.disconnect();
620622
resolve(success);
@@ -646,7 +648,7 @@ class InjectedScript {
646648
reject(`timed out after ${timeout}ms`);
647649
return;
648650
}
649-
const success = predicate(...args);
651+
const success = predicate();
650652
if (success !== continuePolling) resolve(success);
651653
else requestAnimationFrame(onRaf);
652654
}
@@ -666,7 +668,7 @@ class InjectedScript {
666668
reject(`timed out after ${timeout}ms`);
667669
return;
668670
}
669-
const success = predicate(...args);
671+
const success = predicate();
670672
if (success !== continuePolling) resolve(success);
671673
else setTimeout(onTimeout, pollInterval);
672674
}

0 commit comments

Comments
 (0)