refactor: remove obsolete wait task binding code (#9679)
This commit is contained in:
parent
2b3cf3ace9
commit
cfb60d01a8
@ -18,7 +18,6 @@ import {Protocol} from 'devtools-protocol';
|
|||||||
import {JSHandle} from '../api/JSHandle.js';
|
import {JSHandle} from '../api/JSHandle.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||||
import {isErrorLike} from '../util/ErrorLike.js';
|
|
||||||
import {CDPSession} from './Connection.js';
|
import {CDPSession} from './Connection.js';
|
||||||
import {ExecutionContext} from './ExecutionContext.js';
|
import {ExecutionContext} from './ExecutionContext.js';
|
||||||
import {Frame} from './Frame.js';
|
import {Frame} from './Frame.js';
|
||||||
@ -40,8 +39,6 @@ import {TaskManager, WaitTask} from './WaitTask.js';
|
|||||||
|
|
||||||
import type {ElementHandle} from '../api/ElementHandle.js';
|
import type {ElementHandle} from '../api/ElementHandle.js';
|
||||||
import {Binding} from './Binding.js';
|
import {Binding} from './Binding.js';
|
||||||
import {LazyArg} from './LazyArg.js';
|
|
||||||
import {stringifyFunction} from '../util/Function.js';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* @public
|
||||||
@ -421,61 +418,6 @@ export class IsolatedWorld {
|
|||||||
await binding?.run(context, seq, args, isTrivial);
|
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<
|
waitForFunction<
|
||||||
Params extends unknown[],
|
Params extends unknown[],
|
||||||
Func extends EvaluateFunc<InnerLazyParams<Params>> = EvaluateFunc<
|
Func extends EvaluateFunc<InnerLazyParams<Params>> = EvaluateFunc<
|
||||||
@ -487,14 +429,12 @@ export class IsolatedWorld {
|
|||||||
polling?: 'raf' | 'mutation' | number;
|
polling?: 'raf' | 'mutation' | number;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
root?: ElementHandle<Node>;
|
root?: ElementHandle<Node>;
|
||||||
bindings?: Map<string, (...args: never[]) => unknown>;
|
|
||||||
} = {},
|
} = {},
|
||||||
...args: Params
|
...args: Params
|
||||||
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
): Promise<HandleFor<Awaited<ReturnType<Func>>>> {
|
||||||
const {
|
const {
|
||||||
polling = 'raf',
|
polling = 'raf',
|
||||||
timeout = this.#timeoutSettings.timeout(),
|
timeout = this.#timeoutSettings.timeout(),
|
||||||
bindings,
|
|
||||||
root,
|
root,
|
||||||
} = options;
|
} = options;
|
||||||
if (typeof polling === 'number' && polling < 0) {
|
if (typeof polling === 'number' && polling < 0) {
|
||||||
@ -503,7 +443,6 @@ export class IsolatedWorld {
|
|||||||
const waitTask = new WaitTask(
|
const waitTask = new WaitTask(
|
||||||
this,
|
this,
|
||||||
{
|
{
|
||||||
bindings,
|
|
||||||
polling,
|
polling,
|
||||||
root,
|
root,
|
||||||
timeout,
|
timeout,
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
import {ElementHandle} from '../api/ElementHandle.js';
|
import {ElementHandle} from '../api/ElementHandle.js';
|
||||||
import type PuppeteerUtil from '../injected/injected.js';
|
import type PuppeteerUtil from '../injected/injected.js';
|
||||||
import {assert} from '../util/assert.js';
|
import {assert} from '../util/assert.js';
|
||||||
|
import {isErrorLike} from '../util/ErrorLike.js';
|
||||||
import {interpolateFunction, stringifyFunction} from '../util/Function.js';
|
import {interpolateFunction, stringifyFunction} from '../util/Function.js';
|
||||||
import type {Frame} from './Frame.js';
|
import type {Frame} from './Frame.js';
|
||||||
import {transposeIterableHandle} from './HandleIterator.js';
|
import {transposeIterableHandle} from './HandleIterator.js';
|
||||||
@ -146,6 +147,9 @@ export class QueryHandler {
|
|||||||
/**
|
/**
|
||||||
* Waits until a single node appears for a given selector and
|
* Waits until a single node appears for a given selector and
|
||||||
* {@link ElementHandle}.
|
* {@link ElementHandle}.
|
||||||
|
*
|
||||||
|
* This will always query the handle in the Puppeteer world and migrate the
|
||||||
|
* result to the main world.
|
||||||
*/
|
*/
|
||||||
static async waitFor(
|
static async waitFor(
|
||||||
elementOrFrame: ElementHandle<Node> | Frame,
|
elementOrFrame: ElementHandle<Node> | Frame,
|
||||||
@ -160,19 +164,51 @@ export class QueryHandler {
|
|||||||
frame = elementOrFrame.frame;
|
frame = elementOrFrame.frame;
|
||||||
element = await frame.worlds[PUPPETEER_WORLD].adoptHandle(elementOrFrame);
|
element = await frame.worlds[PUPPETEER_WORLD].adoptHandle(elementOrFrame);
|
||||||
}
|
}
|
||||||
const result = await frame.worlds[PUPPETEER_WORLD]._waitForSelectorInPage(
|
|
||||||
this._querySelector,
|
const {visible = false, hidden = false, timeout} = options;
|
||||||
element,
|
|
||||||
selector,
|
try {
|
||||||
options
|
const handle = await frame.worlds[PUPPETEER_WORLD].waitForFunction(
|
||||||
);
|
async (PuppeteerUtil, query, selector, root, visible) => {
|
||||||
if (element) {
|
const querySelector = PuppeteerUtil.createFunction(
|
||||||
await element.dispose();
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,6 @@ import {JSHandle} from '../api/JSHandle.js';
|
|||||||
import type {Poller} from '../injected/Poller.js';
|
import type {Poller} from '../injected/Poller.js';
|
||||||
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
import {createDeferredPromise} from '../util/DeferredPromise.js';
|
||||||
import {stringifyFunction} from '../util/Function.js';
|
import {stringifyFunction} from '../util/Function.js';
|
||||||
import {Binding} from './Binding.js';
|
|
||||||
import {TimeoutError} from './Errors.js';
|
import {TimeoutError} from './Errors.js';
|
||||||
import {IsolatedWorld} from './IsolatedWorld.js';
|
import {IsolatedWorld} from './IsolatedWorld.js';
|
||||||
import {LazyArg} from './LazyArg.js';
|
import {LazyArg} from './LazyArg.js';
|
||||||
@ -29,7 +28,6 @@ import {HandleFor} from './types.js';
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export interface WaitTaskOptions {
|
export interface WaitTaskOptions {
|
||||||
bindings?: Map<string, (...args: never[]) => unknown>;
|
|
||||||
polling: 'raf' | 'mutation' | number;
|
polling: 'raf' | 'mutation' | number;
|
||||||
root?: ElementHandle<Node>;
|
root?: ElementHandle<Node>;
|
||||||
timeout: number;
|
timeout: number;
|
||||||
@ -40,7 +38,6 @@ export interface WaitTaskOptions {
|
|||||||
*/
|
*/
|
||||||
export class WaitTask<T = unknown> {
|
export class WaitTask<T = unknown> {
|
||||||
#world: IsolatedWorld;
|
#world: IsolatedWorld;
|
||||||
#bindings: Map<string, (...args: never[]) => unknown>;
|
|
||||||
#polling: 'raf' | 'mutation' | number;
|
#polling: 'raf' | 'mutation' | number;
|
||||||
#root?: ElementHandle<Node>;
|
#root?: ElementHandle<Node>;
|
||||||
|
|
||||||
@ -60,7 +57,6 @@ export class WaitTask<T = unknown> {
|
|||||||
...args: unknown[]
|
...args: unknown[]
|
||||||
) {
|
) {
|
||||||
this.#world = world;
|
this.#world = world;
|
||||||
this.#bindings = options.bindings ?? new Map();
|
|
||||||
this.#polling = options.polling;
|
this.#polling = options.polling;
|
||||||
this.#root = options.root;
|
this.#root = options.root;
|
||||||
|
|
||||||
@ -84,15 +80,6 @@ export class WaitTask<T = unknown> {
|
|||||||
}, options.timeout);
|
}, 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();
|
this.rerun();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,15 +89,6 @@ export class WaitTask<T = unknown> {
|
|||||||
|
|
||||||
async rerun(): Promise<void> {
|
async rerun(): Promise<void> {
|
||||||
try {
|
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) {
|
switch (this.#polling) {
|
||||||
case 'raf':
|
case 'raf':
|
||||||
this.#poller = await this.#world.evaluateHandle(
|
this.#poller = await this.#world.evaluateHandle(
|
||||||
|
Loading…
Reference in New Issue
Block a user