refactor: remove obsolete wait task binding code (#9679)

This commit is contained in:
jrandolf 2023-02-15 10:57:24 -08:00 committed by GitHub
parent 2b3cf3ace9
commit cfb60d01a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 96 deletions

View File

@ -18,7 +18,6 @@ import {Protocol} from 'devtools-protocol';
import {JSHandle} from '../api/JSHandle.js';
import {assert} from '../util/assert.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';
import {isErrorLike} from '../util/ErrorLike.js';
import {CDPSession} from './Connection.js';
import {ExecutionContext} from './ExecutionContext.js';
import {Frame} from './Frame.js';
@ -40,8 +39,6 @@ import {TaskManager, WaitTask} from './WaitTask.js';
import type {ElementHandle} from '../api/ElementHandle.js';
import {Binding} from './Binding.js';
import {LazyArg} from './LazyArg.js';
import {stringifyFunction} from '../util/Function.js';
/**
* @public
@ -421,61 +418,6 @@ export class IsolatedWorld {
await binding?.run(context, seq, args, isTrivial);
};
async _waitForSelectorInPage(
queryOne: Function,
root: ElementHandle<Node> | undefined,
selector: string,
options: WaitForSelectorOptions,
bindings = new Map<string, (...args: never[]) => unknown>()
): Promise<JSHandle<unknown> | null> {
const {
visible: waitForVisible = false,
hidden: waitForHidden = false,
timeout = this.#timeoutSettings.timeout(),
} = options;
try {
const handle = await this.waitForFunction(
async (PuppeteerUtil, query, selector, root, visible) => {
if (!PuppeteerUtil) {
return;
}
const node = (await PuppeteerUtil.createFunction(query)(
root ?? document,
selector,
PuppeteerUtil
)) as Node | null;
return PuppeteerUtil.checkVisibility(node, visible);
},
{
bindings,
polling: waitForVisible || waitForHidden ? 'raf' : 'mutation',
root,
timeout,
},
LazyArg.create(context => {
return context.puppeteerUtil;
}),
stringifyFunction(queryOne as (...args: unknown[]) => unknown),
selector,
root,
waitForVisible ? true : waitForHidden ? false : undefined
);
const elementHandle = handle.asElement();
if (!elementHandle) {
await handle.dispose();
return null;
}
return elementHandle;
} catch (error) {
if (!isErrorLike(error)) {
throw error;
}
error.message = `Waiting for selector \`${selector}\` failed: ${error.message}`;
throw error;
}
}
waitForFunction<
Params extends unknown[],
Func extends EvaluateFunc<InnerLazyParams<Params>> = EvaluateFunc<
@ -487,14 +429,12 @@ export class IsolatedWorld {
polling?: 'raf' | 'mutation' | number;
timeout?: number;
root?: ElementHandle<Node>;
bindings?: Map<string, (...args: never[]) => unknown>;
} = {},
...args: Params
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
const {
polling = 'raf',
timeout = this.#timeoutSettings.timeout(),
bindings,
root,
} = options;
if (typeof polling === 'number' && polling < 0) {
@ -503,7 +443,6 @@ export class IsolatedWorld {
const waitTask = new WaitTask(
this,
{
bindings,
polling,
root,
timeout,

View File

@ -17,6 +17,7 @@
import {ElementHandle} from '../api/ElementHandle.js';
import type PuppeteerUtil from '../injected/injected.js';
import {assert} from '../util/assert.js';
import {isErrorLike} from '../util/ErrorLike.js';
import {interpolateFunction, stringifyFunction} from '../util/Function.js';
import type {Frame} from './Frame.js';
import {transposeIterableHandle} from './HandleIterator.js';
@ -146,6 +147,9 @@ export class QueryHandler {
/**
* Waits until a single node appears for a given selector and
* {@link ElementHandle}.
*
* This will always query the handle in the Puppeteer world and migrate the
* result to the main world.
*/
static async waitFor(
elementOrFrame: ElementHandle<Node> | Frame,
@ -160,19 +164,51 @@ export class QueryHandler {
frame = elementOrFrame.frame;
element = await frame.worlds[PUPPETEER_WORLD].adoptHandle(elementOrFrame);
}
const result = await frame.worlds[PUPPETEER_WORLD]._waitForSelectorInPage(
this._querySelector,
element,
selector,
options
);
if (element) {
await element.dispose();
const {visible = false, hidden = false, timeout} = options;
try {
const handle = await frame.worlds[PUPPETEER_WORLD].waitForFunction(
async (PuppeteerUtil, query, selector, root, visible) => {
const querySelector = PuppeteerUtil.createFunction(
query
) as QuerySelector;
const node = await querySelector(
root ?? document,
selector,
PuppeteerUtil
);
return PuppeteerUtil.checkVisibility(node, visible);
},
{
polling: visible || hidden ? 'raf' : 'mutation',
root: element,
timeout,
},
LazyArg.create(context => {
return context.puppeteerUtil;
}),
stringifyFunction(this._querySelector),
selector,
element,
visible ? true : hidden ? false : undefined
);
if (!(handle instanceof ElementHandle)) {
await handle.dispose();
return null;
}
return frame.worlds[MAIN_WORLD].transferHandle(handle);
} catch (error) {
if (!isErrorLike(error)) {
throw error;
}
error.message = `Waiting for selector \`${selector}\` failed: ${error.message}`;
throw error;
} finally {
if (element) {
await element.dispose();
}
}
if (!(result instanceof ElementHandle)) {
await result?.dispose();
return null;
}
return frame.worlds[MAIN_WORLD].transferHandle(result);
}
}

View File

@ -19,7 +19,6 @@ import {JSHandle} from '../api/JSHandle.js';
import type {Poller} from '../injected/Poller.js';
import {createDeferredPromise} from '../util/DeferredPromise.js';
import {stringifyFunction} from '../util/Function.js';
import {Binding} from './Binding.js';
import {TimeoutError} from './Errors.js';
import {IsolatedWorld} from './IsolatedWorld.js';
import {LazyArg} from './LazyArg.js';
@ -29,7 +28,6 @@ import {HandleFor} from './types.js';
* @internal
*/
export interface WaitTaskOptions {
bindings?: Map<string, (...args: never[]) => unknown>;
polling: 'raf' | 'mutation' | number;
root?: ElementHandle<Node>;
timeout: number;
@ -40,7 +38,6 @@ export interface WaitTaskOptions {
*/
export class WaitTask<T = unknown> {
#world: IsolatedWorld;
#bindings: Map<string, (...args: never[]) => unknown>;
#polling: 'raf' | 'mutation' | number;
#root?: ElementHandle<Node>;
@ -60,7 +57,6 @@ export class WaitTask<T = unknown> {
...args: unknown[]
) {
this.#world = world;
this.#bindings = options.bindings ?? new Map();
this.#polling = options.polling;
this.#root = options.root;
@ -84,15 +80,6 @@ export class WaitTask<T = unknown> {
}, options.timeout);
}
if (this.#bindings.size !== 0) {
for (const [name, fn] of this.#bindings) {
this.#world._bindings.set(
name,
new Binding(name, fn as (...args: unknown[]) => unknown)
);
}
}
this.rerun();
}
@ -102,15 +89,6 @@ export class WaitTask<T = unknown> {
async rerun(): Promise<void> {
try {
if (this.#bindings.size !== 0) {
const context = await this.#world.executionContext();
await Promise.all(
[...this.#bindings].map(async ([name]) => {
return await this.#world._addBindingToContext(context, name);
})
);
}
switch (this.#polling) {
case 'raf':
this.#poller = await this.#world.evaluateHandle(