refactor: move createCdpHandle to IsolatedWorld (#12397)

This commit is contained in:
Alex Rudenko 2024-05-06 13:22:11 +02:00 committed by GitHub
parent 857051212c
commit 5ce4f12960
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 23 additions and 23 deletions

View File

@ -268,7 +268,7 @@ export class ExecutionContext {
return returnByValue
? valueFromRemoteObject(remoteObject)
: createCdpHandle(this._world, remoteObject);
: this._world.createCdpHandle(remoteObject);
}
const functionDeclaration = stringifyFunction(pageFunction);
@ -305,7 +305,7 @@ export class ExecutionContext {
}
return returnByValue
? valueFromRemoteObject(remoteObject)
: createCdpHandle(this._world, remoteObject);
: this._world.createCdpHandle(remoteObject);
async function convertArgument(
this: ExecutionContext,
@ -377,16 +377,3 @@ const rewriteError = (error: Error): Protocol.Runtime.EvaluateResponse => {
}
throw error;
};
/**
* @internal
*/
export function createCdpHandle(
realm: IsolatedWorld,
remoteObject: Protocol.Runtime.RemoteObject
): JSHandle | ElementHandle<Node> {
if (remoteObject.subtype === 'node') {
return new CdpElementHandle(realm, remoteObject);
}
return new CdpJSHandle(realm, remoteObject);
}

View File

@ -7,6 +7,7 @@
import type {Protocol} from 'devtools-protocol';
import type {CDPSession} from '../api/CDPSession.js';
import type {ElementHandle} from '../api/ElementHandle.js';
import type {JSHandle} from '../api/JSHandle.js';
import {Realm} from '../api/Realm.js';
import type {TimeoutSettings} from '../common/TimeoutSettings.js';
@ -17,9 +18,11 @@ import {disposeSymbol} from '../util/disposable.js';
import {Mutex} from '../util/Mutex.js';
import type {Binding} from './Binding.js';
import {ExecutionContext, createCdpHandle} from './ExecutionContext.js';
import {CdpElementHandle} from './ElementHandle.js';
import {ExecutionContext} from './ExecutionContext.js';
import type {CdpFrame} from './Frame.js';
import type {MAIN_WORLD, PUPPETEER_WORLD} from './IsolatedWorlds.js';
import {CdpJSHandle} from './JSHandle.js';
import {addPageBinding} from './utils.js';
import type {CdpWebWorker} from './WebWorker.js';
@ -231,7 +234,7 @@ export class IsolatedWorld extends Realm {
backendNodeId: backendNodeId,
executionContextId: executionContext._contextId,
});
return createCdpHandle(this, object) as JSHandle<Node>;
return this.createCdpHandle(object) as JSHandle<Node>;
}
async adoptHandle<T extends JSHandle<Node>>(handle: T): Promise<T> {
@ -266,6 +269,18 @@ export class IsolatedWorld extends Realm {
return newHandle;
}
/**
* @internal
*/
createCdpHandle(
remoteObject: Protocol.Runtime.RemoteObject
): JSHandle | ElementHandle<Node> {
if (remoteObject.subtype === 'node') {
return new CdpElementHandle(this, remoteObject);
}
return new CdpJSHandle(this, remoteObject);
}
[disposeSymbol](): void {
super[disposeSymbol]();
this.client.off('Runtime.bindingCalled', this.#onBindingCalled);

View File

@ -64,7 +64,6 @@ import {Coverage} from './Coverage.js';
import type {DeviceRequestPrompt} from './DeviceRequestPrompt.js';
import {CdpDialog} from './Dialog.js';
import {EmulationManager} from './EmulationManager.js';
import {createCdpHandle} from './ExecutionContext.js';
import {FirefoxTargetManager} from './FirefoxTargetManager.js';
import type {CdpFrame} from './Frame.js';
import {FrameManager} from './FrameManager.js';
@ -576,10 +575,9 @@ export class CdpPage extends Page {
prototypeObjectId: prototypeHandle.id,
}
);
return createCdpHandle(
this.mainFrame().mainRealm(),
response.objects
) as HandleFor<Prototype[]>;
return this.mainFrame()
.mainRealm()
.createCdpHandle(response.objects) as HandleFor<Prototype[]>;
}
override async cookies(...urls: string[]): Promise<Cookie[]> {
@ -814,7 +812,7 @@ export class CdpPage extends Page {
return;
}
const values = event.args.map(arg => {
return createCdpHandle(context._world, arg);
return context._world.createCdpHandle(arg);
});
this.#addConsoleMessage(
convertConsoleMessageLevel(event.type),