refactor: simplify worlds handling (#12400)

This commit is contained in:
Alex Rudenko 2024-05-06 17:19:23 +02:00 committed by GitHub
parent 91e9503624
commit 3eab6b5381
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 32 deletions

View File

@ -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,
@ -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.
@ -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 {

View File

@ -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, () => {

View File

@ -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;
}