Skip to content

Commit

Permalink
refactor: simplify worlds handling (#12400)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrKoN committed May 6, 2024
1 parent 91e9503 commit 3eab6b5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 deletions.
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

0 comments on commit 3eab6b5

Please sign in to comment.