Skip to content

Commit

Permalink
refactor: remove things
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed May 7, 2024
1 parent 18eaae9 commit 7ce1b07
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 51 deletions.
50 changes: 27 additions & 23 deletions packages/puppeteer-core/src/cdp/ExecutionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ export class ExecutionContext
}>
implements Disposable
{
_client: CDPSession;
_world: IsolatedWorld;
_contextId: number;
_contextName?: string;
#client: CDPSession;
#world: IsolatedWorld;
#id: number;
#name?: string;

readonly #disposables = new DisposableStack();

Expand All @@ -85,16 +85,16 @@ export class ExecutionContext
world: IsolatedWorld
) {
super();
this._client = client;
this._world = world;
this._contextId = contextPayload.id;
this.#client = client;
this.#world = world;
this.#id = contextPayload.id;
if (contextPayload.name) {
this._contextName = contextPayload.name;
this.#name = contextPayload.name;
}
const clientEmitter = this.#disposables.use(new EventEmitter(this._client));
const clientEmitter = this.#disposables.use(new EventEmitter(this.#client));
clientEmitter.on('Runtime.bindingCalled', this.#onBindingCalled.bind(this));
clientEmitter.on('Runtime.executionContextDestroyed', async event => {
if (event.executionContextId === this._contextId) {
if (event.executionContextId === this.#id) {
this[disposeSymbol]();
}
});
Expand All @@ -120,16 +120,16 @@ export class ExecutionContext

using _ = await this.#mutex.acquire();
try {
await this._client.send(
await this.#client.send(
'Runtime.addBinding',
this._contextName
this.#name
? {
name: binding.name,
executionContextName: this._contextName,
executionContextName: this.#name,
}
: {
name: binding.name,
executionContextId: this._contextId,
executionContextId: this.#id,
}
);

Expand Down Expand Up @@ -177,7 +177,7 @@ export class ExecutionContext
}

try {
if (event.executionContextId !== this._contextId) {
if (event.executionContextId !== this.#id) {
return;
}

Expand All @@ -188,8 +188,12 @@ export class ExecutionContext
}
}

get id(): number {
return this.#id;
}

#onConsoleAPI(event: Protocol.Runtime.ConsoleAPICalledEvent): void {
if (event.executionContextId !== this._contextId) {
if (event.executionContextId !== this.#id) {
return;
}
this.emit('consoleapicalled', event);
Expand Down Expand Up @@ -369,13 +373,13 @@ export class ExecutionContext
);

if (isString(pageFunction)) {
const contextId = this._contextId;
const contextId = this.#id;
const expression = pageFunction;
const expressionWithSourceUrl = SOURCE_URL_REGEX.test(expression)
? expression
: `${expression}\n${sourceUrlComment}\n`;

const {exceptionDetails, result: remoteObject} = await this._client
const {exceptionDetails, result: remoteObject} = await this.#client
.send('Runtime.evaluate', {
expression: expressionWithSourceUrl,
contextId,
Expand All @@ -391,7 +395,7 @@ export class ExecutionContext

return returnByValue
? valueFromRemoteObject(remoteObject)
: this._world.createCdpHandle(remoteObject);
: this.#world.createCdpHandle(remoteObject);
}

const functionDeclaration = stringifyFunction(pageFunction);
Expand All @@ -402,9 +406,9 @@ export class ExecutionContext
: `${functionDeclaration}\n${sourceUrlComment}\n`;
let callFunctionOnPromise;
try {
callFunctionOnPromise = this._client.send('Runtime.callFunctionOn', {
callFunctionOnPromise = this.#client.send('Runtime.callFunctionOn', {
functionDeclaration: functionDeclarationWithSourceUrl,
executionContextId: this._contextId,
executionContextId: this.#id,
arguments: args.length
? await Promise.all(args.map(convertArgument.bind(this)))
: [],
Expand All @@ -428,7 +432,7 @@ export class ExecutionContext
}
return returnByValue
? valueFromRemoteObject(remoteObject)
: this._world.createCdpHandle(remoteObject);
: this.#world.createCdpHandle(remoteObject);

async function convertArgument(
this: ExecutionContext,
Expand Down Expand Up @@ -458,7 +462,7 @@ export class ExecutionContext
? arg
: null;
if (objectHandle) {
if (objectHandle.realm !== this._world) {
if (objectHandle.realm !== this.#world) {
throw new Error(
'JSHandles can be evaluated only in the context they were created!'
);
Expand Down
27 changes: 0 additions & 27 deletions packages/puppeteer-core/src/cdp/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
#page: CdpPage;
#networkManager: NetworkManager;
#timeoutSettings: TimeoutSettings;
#contextIdToContext = new Map<string, ExecutionContext>();
#isolatedWorlds = new Set<string>();
#client: CDPSession;

Expand Down Expand Up @@ -223,22 +222,6 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
}
}

executionContextById(
contextId: number,
session: CDPSession = this.#client
): ExecutionContext {
const context = this.getExecutionContextById(contextId, session);
assert(context, 'INTERNAL ERROR: missing context with id = ' + contextId);
return context;
}

getExecutionContextById(
contextId: number,
session: CDPSession = this.#client
): ExecutionContext | undefined {
return this.#contextIdToContext.get(`${session.id()}:${contextId}`);
}

page(): CdpPage {
return this.#page;
}
Expand Down Expand Up @@ -491,16 +474,6 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
world
);
world.setContext(context);
const key = `${session.id()}:${contextPayload.id}`;
this.#contextIdToContext.set(key, context);
context.once('disposed', () => {
const key = `${session.id()}:${contextPayload.id}`;
const context = this.#contextIdToContext.get(key);
if (!context) {
return;
}
this.#contextIdToContext.delete(key);
});
}

#removeFramesRecursively(frame: CdpFrame): void {
Expand Down
2 changes: 1 addition & 1 deletion packages/puppeteer-core/src/cdp/IsolatedWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class IsolatedWorld extends Realm {
}
const {object} = await this.client.send('DOM.resolveNode', {
backendNodeId: backendNodeId,
executionContextId: context._contextId,
executionContextId: context.id,
});
return this.createCdpHandle(object) as JSHandle<Node>;
}
Expand Down

0 comments on commit 7ce1b07

Please sign in to comment.