From cfb60d01a81fdf4fee1c6a6d9071530e0211c646 Mon Sep 17 00:00:00 2001 From: jrandolf <101637635+jrandolf@users.noreply.github.com> Date: Wed, 15 Feb 2023 10:57:24 -0800 Subject: [PATCH] refactor: remove obsolete wait task binding code (#9679) --- .../src/common/IsolatedWorld.ts | 61 ------------------ .../puppeteer-core/src/common/QueryHandler.ts | 62 +++++++++++++++---- .../puppeteer-core/src/common/WaitTask.ts | 22 ------- 3 files changed, 49 insertions(+), 96 deletions(-) diff --git a/packages/puppeteer-core/src/common/IsolatedWorld.ts b/packages/puppeteer-core/src/common/IsolatedWorld.ts index 88692b5274e..91eda600470 100644 --- a/packages/puppeteer-core/src/common/IsolatedWorld.ts +++ b/packages/puppeteer-core/src/common/IsolatedWorld.ts @@ -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 | undefined, - selector: string, - options: WaitForSelectorOptions, - bindings = new Map unknown>() - ): Promise | 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> = EvaluateFunc< @@ -487,14 +429,12 @@ export class IsolatedWorld { polling?: 'raf' | 'mutation' | number; timeout?: number; root?: ElementHandle; - bindings?: Map unknown>; } = {}, ...args: Params ): Promise>>> { 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, diff --git a/packages/puppeteer-core/src/common/QueryHandler.ts b/packages/puppeteer-core/src/common/QueryHandler.ts index 52a7410e679..007fe02df07 100644 --- a/packages/puppeteer-core/src/common/QueryHandler.ts +++ b/packages/puppeteer-core/src/common/QueryHandler.ts @@ -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 | 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); } } diff --git a/packages/puppeteer-core/src/common/WaitTask.ts b/packages/puppeteer-core/src/common/WaitTask.ts index 7058a9b869f..882db247f92 100644 --- a/packages/puppeteer-core/src/common/WaitTask.ts +++ b/packages/puppeteer-core/src/common/WaitTask.ts @@ -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 unknown>; polling: 'raf' | 'mutation' | number; root?: ElementHandle; timeout: number; @@ -40,7 +38,6 @@ export interface WaitTaskOptions { */ export class WaitTask { #world: IsolatedWorld; - #bindings: Map unknown>; #polling: 'raf' | 'mutation' | number; #root?: ElementHandle; @@ -60,7 +57,6 @@ export class WaitTask { ...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 { }, 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 { async rerun(): Promise { 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(