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 { export class CdpFrame extends Frame {
#url = ''; #url = '';
#detached = false; #detached = false;
#client!: CDPSession; #client: CDPSession;
worlds!: IsolatedWorldChart;
_frameManager: FrameManager; _frameManager: FrameManager;
override _id: string;
_loaderId = ''; _loaderId = '';
_lifecycleEvents = new Set<string>(); _lifecycleEvents = new Set<string>();
override _id: string;
override _parentId?: string; override _parentId?: string;
worlds: IsolatedWorldChart;
constructor( constructor(
frameManager: FrameManager, frameManager: FrameManager,
frameId: string, frameId: string,
@ -56,10 +58,16 @@ export class CdpFrame extends Frame {
this._id = frameId; this._id = frameId;
this._parentId = parentFrameId; this._parentId = parentFrameId;
this.#detached = false; this.#detached = false;
this.#client = client;
this._loaderId = ''; this._loaderId = '';
this.worlds = {
this.updateClient(client); [MAIN_WORLD]: new IsolatedWorld(this, this._frameManager.timeoutSettings),
[PUPPETEER_WORLD]: new IsolatedWorld(
this,
this._frameManager.timeoutSettings
),
};
this.on(FrameEvent.FrameSwappedByActivation, () => { this.on(FrameEvent.FrameSwappedByActivation, () => {
// Emulate loading process for swapped frames. // Emulate loading process for swapped frames.
@ -85,28 +93,8 @@ export class CdpFrame extends Frame {
this._id = id; this._id = id;
} }
updateClient(client: CDPSession, keepWorlds = false): void { updateClient(client: CDPSession): void {
this.#client = client; 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 { override page(): CdpPage {

View File

@ -134,10 +134,8 @@ export class FrameManager extends EventEmitter<FrameManagerEvents> {
this.#frameNavigatedReceived.add(this.#client._target()._targetId); this.#frameNavigatedReceived.add(this.#client._target()._targetId);
this._frameTree.removeFrame(frame); this._frameTree.removeFrame(frame);
frame.updateId(this.#client._target()._targetId); frame.updateId(this.#client._target()._targetId);
frame.mainRealm().clearContext();
frame.isolatedRealm().clearContext();
this._frameTree.addFrame(frame); this._frameTree.addFrame(frame);
frame.updateClient(client, true); frame.updateClient(client);
} }
this.setupEventListeners(client); this.setupEventListeners(client);
client.once(CDPSessionEvent.Disconnected, () => { client.once(CDPSessionEvent.Disconnected, () => {

View File

@ -54,15 +54,12 @@ export class IsolatedWorld extends Realm {
) { ) {
super(timeoutSettings); super(timeoutSettings);
this.#frameOrWorker = frameOrWorker; this.#frameOrWorker = frameOrWorker;
this.frameUpdated();
} }
get environment(): CdpFrame | CdpWebWorker { get environment(): CdpFrame | CdpWebWorker {
return this.#frameOrWorker; return this.#frameOrWorker;
} }
frameUpdated(): void {}
get client(): CDPSession { get client(): CDPSession {
return this.#frameOrWorker.client; return this.#frameOrWorker.client;
} }