mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: simplify worlds handling (#12400)
This commit is contained in:
parent
91e9503624
commit
3eab6b5381
@ -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 {
|
||||
|
@ -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, () => {
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user