Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify worlds handling #12400

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 14 additions & 26 deletions packages/puppeteer-core/src/cdp/Frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ import type {CdpPage} from './Page.js';
export class CdpFrame extends Frame {
#url = '';
#detached = false;
#client!: CDPSession;
worlds!: IsolatedWorldChart;
#client: CDPSession;

_frameManager: FrameManager;
override _id: string;
_loaderId = '';
_lifecycleEvents = new Set<string>();

override _id: string;
override _parentId?: string;

worlds: IsolatedWorldChart;

constructor(
frameManager: FrameManager,
frameId: string,
Expand All @@ -56,10 +58,16 @@ export class CdpFrame extends Frame {
this._id = frameId;
this._parentId = parentFrameId;
this.#detached = false;
this.#client = client;

this._loaderId = '';

this.updateClient(client);
this.worlds = {
[MAIN_WORLD]: new IsolatedWorld(this, this._frameManager.timeoutSettings),
[PUPPETEER_WORLD]: new IsolatedWorld(
this,
this._frameManager.timeoutSettings
),
};

this.on(FrameEvent.FrameSwappedByActivation, () => {
// Emulate loading process for swapped frames.
Expand All @@ -85,28 +93,8 @@ export class CdpFrame extends Frame {
this._id = id;
}

updateClient(client: CDPSession, keepWorlds = false): void {
updateClient(client: CDPSession): void {
this.#client = client;
if (!keepWorlds) {
// Clear the current contexts on previous world instances.
if (this.worlds) {
this.worlds[MAIN_WORLD].clearContext();
this.worlds[PUPPETEER_WORLD].clearContext();
}
this.worlds = {
[MAIN_WORLD]: new IsolatedWorld(
this,
this._frameManager.timeoutSettings
),
[PUPPETEER_WORLD]: new IsolatedWorld(
this,
this._frameManager.timeoutSettings
),
};
} else {
this.worlds[MAIN_WORLD].frameUpdated();
this.worlds[PUPPETEER_WORLD].frameUpdated();
}
}

override page(): CdpPage {
Expand Down
4 changes: 1 addition & 3 deletions packages/puppeteer-core/src/cdp/FrameManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,8 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
this.#frameNavigatedReceived.add(this.#client._target()._targetId);
this._frameTree.removeFrame(frame);
frame.updateId(this.#client._target()._targetId);
frame.mainRealm().clearContext();
frame.isolatedRealm().clearContext();
this._frameTree.addFrame(frame);
frame.updateClient(client, true);
frame.updateClient(client);
}
this.setupEventListeners(client);
client.once(CDPSessionEvent.Disconnected, () => {
Expand Down
3 changes: 0 additions & 3 deletions packages/puppeteer-core/src/cdp/IsolatedWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ export class IsolatedWorld extends Realm {
) {
super(timeoutSettings);
this.#frameOrWorker = frameOrWorker;
this.frameUpdated();
}

get environment(): CdpFrame | CdpWebWorker {
return this.#frameOrWorker;
}

frameUpdated(): void {}

get client(): CDPSession {
return this.#frameOrWorker.client;
}
Expand Down