fix(revert): use LazyArg for puppeteer utilities (#9590)

Reverts puppeteer/puppeteer#9575
This commit is contained in:
Alex Rudenko 2023-01-26 09:50:06 +01:00 committed by GitHub
parent 496658f029
commit 6edd996768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 52 deletions

View File

@ -34,7 +34,6 @@ import {Page} from '../api/Page.js';
import {getQueryHandlerAndSelector} from './QueryHandler.js'; import {getQueryHandlerAndSelector} from './QueryHandler.js';
import {EvaluateFunc, HandleFor, NodeFor} from './types.js'; import {EvaluateFunc, HandleFor, NodeFor} from './types.js';
import {importFS} from './util.js'; import {importFS} from './util.js';
import {LazyArg} from './LazyArg.js';
/** /**
* @public * @public
@ -805,9 +804,8 @@ export class Frame {
type = type ?? 'text/javascript'; type = type ?? 'text/javascript';
const puppeteerWorld = this.worlds[PUPPETEER_WORLD];
return this.worlds[MAIN_WORLD].transferHandle( return this.worlds[MAIN_WORLD].transferHandle(
await puppeteerWorld.evaluateHandle( await this.worlds[PUPPETEER_WORLD].evaluateHandle(
async ({createDeferredPromise}, {url, id, type, content}) => { async ({createDeferredPromise}, {url, id, type, content}) => {
const promise = createDeferredPromise<void>(); const promise = createDeferredPromise<void>();
const script = document.createElement('script'); const script = document.createElement('script');
@ -841,9 +839,7 @@ export class Frame {
await promise; await promise;
return script; return script;
}, },
LazyArg.create(() => { await this.worlds[PUPPETEER_WORLD].puppeteerUtil,
return puppeteerWorld.puppeteerUtil;
}),
{...options, type, content} {...options, type, content}
) )
); );
@ -891,9 +887,8 @@ export class Frame {
options.content = content; options.content = content;
} }
const puppeteerWorld = this.worlds[PUPPETEER_WORLD];
return this.worlds[MAIN_WORLD].transferHandle( return this.worlds[MAIN_WORLD].transferHandle(
await puppeteerWorld.evaluateHandle( await this.worlds[PUPPETEER_WORLD].evaluateHandle(
async ({createDeferredPromise}, {url, content}) => { async ({createDeferredPromise}, {url, content}) => {
const promise = createDeferredPromise<void>(); const promise = createDeferredPromise<void>();
let element: HTMLStyleElement | HTMLLinkElement; let element: HTMLStyleElement | HTMLLinkElement;
@ -928,9 +923,7 @@ export class Frame {
await promise; await promise;
return element; return element;
}, },
LazyArg.create(() => { await this.worlds[PUPPETEER_WORLD].puppeteerUtil,
return puppeteerWorld.puppeteerUtil;
}),
options options
) )
); );

View File

@ -28,7 +28,7 @@ import {JSHandle} from './JSHandle.js';
import {LazyArg} from './LazyArg.js'; import {LazyArg} from './LazyArg.js';
import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js'; import {LifecycleWatcher, PuppeteerLifeCycleEvent} from './LifecycleWatcher.js';
import {TimeoutSettings} from './TimeoutSettings.js'; import {TimeoutSettings} from './TimeoutSettings.js';
import {EvaluateFunc, HandleFor, NodeFor} from './types.js'; import {EvaluateFunc, HandleFor, InnerLazyParams, NodeFor} from './types.js';
import {createJSHandle, debugError, pageBindingInitString} from './util.js'; import {createJSHandle, debugError, pageBindingInitString} from './util.js';
import {TaskManager, WaitTask} from './WaitTask.js'; import {TaskManager, WaitTask} from './WaitTask.js';
import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js'; import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
@ -138,11 +138,8 @@ export class IsolatedWorld {
} }
clearContext(): void { clearContext(): void {
// Only create a new promise if the old one was resolved.
if (this.#puppeteerUtil.resolved()) {
this.#puppeteerUtil = createDeferredPromise();
}
this.#document = undefined; this.#document = undefined;
this.#puppeteerUtil = createDeferredPromise();
this.#context = createDeferredPromise(); this.#context = createDeferredPromise();
} }
@ -520,8 +517,13 @@ export class IsolatedWorld {
root, root,
timeout, timeout,
}, },
LazyArg.create(() => { new LazyArg(async () => {
return this.puppeteerUtil; try {
// In case CDP fails.
return await this.puppeteerUtil;
} catch {
return undefined;
}
}), }),
queryOne.toString(), queryOne.toString(),
selector, selector,
@ -545,7 +547,9 @@ export class IsolatedWorld {
waitForFunction< waitForFunction<
Params extends unknown[], Params extends unknown[],
Func extends EvaluateFunc<Params> = EvaluateFunc<Params> Func extends EvaluateFunc<InnerLazyParams<Params>> = EvaluateFunc<
InnerLazyParams<Params>
>
>( >(
pageFunction: Func | string, pageFunction: Func | string,
options: { options: {

View File

@ -18,14 +18,8 @@
* @internal * @internal
*/ */
export class LazyArg<T> { export class LazyArg<T> {
static create = <T>(callback: () => Promise<T>): T => {
// We do type coercion here because we don't want to introduce LazyArgs to
// the type system.
return new LazyArg(callback) as unknown as T;
};
#get: () => Promise<T>; #get: () => Promise<T>;
private constructor(get: () => Promise<T>) { constructor(get: () => Promise<T>) {
this.#get = get; this.#get = get;
} }

View File

@ -15,13 +15,11 @@
*/ */
import PuppeteerUtil from '../injected/injected.js'; import PuppeteerUtil from '../injected/injected.js';
import {assert} from '../util/assert.js';
import {ariaHandler} from './AriaQueryHandler.js'; import {ariaHandler} from './AriaQueryHandler.js';
import {ElementHandle} from './ElementHandle.js'; import {ElementHandle} from './ElementHandle.js';
import {Frame} from './Frame.js'; import {Frame} from './Frame.js';
import {WaitForSelectorOptions} from './IsolatedWorld.js'; import {WaitForSelectorOptions} from './IsolatedWorld.js';
import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js'; import {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
import {LazyArg} from './LazyArg.js';
/** /**
* @public * @public
@ -104,11 +102,7 @@ function createPuppeteerQueryHandler(
const jsHandle = await element.evaluateHandle( const jsHandle = await element.evaluateHandle(
queryOne, queryOne,
selector, selector,
LazyArg.create(() => { await element.executionContext()._world!.puppeteerUtil
const world = element.executionContext()._world;
assert(world);
return world.puppeteerUtil;
})
); );
const elementHandle = jsHandle.asElement(); const elementHandle = jsHandle.asElement();
if (elementHandle) { if (elementHandle) {
@ -154,11 +148,7 @@ function createPuppeteerQueryHandler(
const jsHandle = await element.evaluateHandle( const jsHandle = await element.evaluateHandle(
queryAll, queryAll,
selector, selector,
LazyArg.create(() => { await element.executionContext()._world!.puppeteerUtil
const world = element.executionContext()._world;
assert(world);
return world.puppeteerUtil;
})
); );
const properties = await jsHandle.getProperties(); const properties = await jsHandle.getProperties();
await jsHandle.dispose(); await jsHandle.dispose();

View File

@ -20,7 +20,6 @@ import {ElementHandle} from './ElementHandle.js';
import {TimeoutError} from './Errors.js'; import {TimeoutError} from './Errors.js';
import {IsolatedWorld} from './IsolatedWorld.js'; import {IsolatedWorld} from './IsolatedWorld.js';
import {JSHandle} from './JSHandle.js'; import {JSHandle} from './JSHandle.js';
import {LazyArg} from './LazyArg.js';
import {HandleFor} from './types.js'; import {HandleFor} from './types.js';
/** /**
@ -115,9 +114,7 @@ export class WaitTask<T = unknown> {
return fun(...args) as Promise<T>; return fun(...args) as Promise<T>;
}); });
}, },
LazyArg.create(() => { await this.#world.puppeteerUtil,
return this.#world.puppeteerUtil;
}),
this.#fn, this.#fn,
...this.#args ...this.#args
); );
@ -130,9 +127,7 @@ export class WaitTask<T = unknown> {
return fun(...args) as Promise<T>; return fun(...args) as Promise<T>;
}, root || document); }, root || document);
}, },
LazyArg.create(() => { await this.#world.puppeteerUtil,
return this.#world.puppeteerUtil;
}),
this.#root, this.#root,
this.#fn, this.#fn,
...this.#args ...this.#args
@ -146,9 +141,7 @@ export class WaitTask<T = unknown> {
return fun(...args) as Promise<T>; return fun(...args) as Promise<T>;
}, ms); }, ms);
}, },
LazyArg.create(() => { await this.#world.puppeteerUtil,
return this.#world.puppeteerUtil;
}),
this.#polling, this.#polling,
this.#fn, this.#fn,
...this.#args ...this.#args

View File

@ -16,6 +16,7 @@
import {JSHandle} from './JSHandle.js'; import {JSHandle} from './JSHandle.js';
import {ElementHandle} from './ElementHandle.js'; import {ElementHandle} from './ElementHandle.js';
import {LazyArg} from './LazyArg.js';
/** /**
* @public * @public
@ -37,6 +38,18 @@ export type HandleOr<T> = HandleFor<T> | JSHandle<T> | T;
*/ */
export type FlattenHandle<T> = T extends HandleOr<infer U> ? U : never; export type FlattenHandle<T> = T extends HandleOr<infer U> ? U : never;
/**
* @internal
*/
export type FlattenLazyArg<T> = T extends LazyArg<infer U> ? U : T;
/**
* @internal
*/
export type InnerLazyParams<T extends unknown[]> = {
[K in keyof T]: FlattenLazyArg<T[K]>;
};
/** /**
* @public * @public
*/ */

View File

@ -16,7 +16,6 @@
import expect from 'expect'; import expect from 'expect';
import {PUPPETEER_WORLD} from 'puppeteer-core/internal/common/IsolatedWorlds.js'; import {PUPPETEER_WORLD} from 'puppeteer-core/internal/common/IsolatedWorlds.js';
import {LazyArg} from 'puppeteer-core/internal/common/LazyArg.js';
import { import {
getTestState, getTestState,
setupTestBrowserHooks, setupTestBrowserHooks,
@ -46,9 +45,7 @@ describe('PuppeteerUtil tests', function () {
({createFunction}, fnString) => { ({createFunction}, fnString) => {
return createFunction(fnString)(4); return createFunction(fnString)(4);
}, },
LazyArg.create(() => { await world.puppeteerUtil,
return world.puppeteerUtil;
}),
(() => { (() => {
return 4; return 4;
}).toString() }).toString()