mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: do not dispose window realm when the context is not destroyed (#11856)
This commit is contained in:
parent
c6ac9d505e
commit
87b4e401e0
@ -33,6 +33,8 @@ export type EvaluateOptions = Omit<
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export abstract class Realm extends EventEmitter<{
|
export abstract class Realm extends EventEmitter<{
|
||||||
|
/** Emitted whenever the realm has updated. */
|
||||||
|
updated: Realm;
|
||||||
/** Emitted when the realm is destroyed. */
|
/** Emitted when the realm is destroyed. */
|
||||||
destroyed: {reason: string};
|
destroyed: {reason: string};
|
||||||
/** Emitted when a dedicated worker is created in the realm. */
|
/** Emitted when a dedicated worker is created in the realm. */
|
||||||
@ -55,22 +57,12 @@ export abstract class Realm extends EventEmitter<{
|
|||||||
// keep-sorted end
|
// keep-sorted end
|
||||||
}
|
}
|
||||||
|
|
||||||
protected initialize(): void {
|
|
||||||
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
|
|
||||||
sessionEmitter.on('script.realmDestroyed', info => {
|
|
||||||
if (info.realm !== this.id) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.dispose('Realm already destroyed.');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// keep-sorted start block=yes
|
// keep-sorted start block=yes
|
||||||
get disposed(): boolean {
|
get disposed(): boolean {
|
||||||
return this.#reason !== undefined;
|
return this.#reason !== undefined;
|
||||||
}
|
}
|
||||||
protected abstract get session(): Session;
|
protected abstract get session(): Session;
|
||||||
protected get target(): Bidi.Script.Target {
|
get target(): Bidi.Script.Target {
|
||||||
return {realm: this.id};
|
return {realm: this.id};
|
||||||
}
|
}
|
||||||
// keep-sorted end
|
// keep-sorted end
|
||||||
@ -144,7 +136,7 @@ export abstract class Realm extends EventEmitter<{
|
|||||||
export class WindowRealm extends Realm {
|
export class WindowRealm extends Realm {
|
||||||
static from(context: BrowsingContext, sandbox?: string): WindowRealm {
|
static from(context: BrowsingContext, sandbox?: string): WindowRealm {
|
||||||
const realm = new WindowRealm(context, sandbox);
|
const realm = new WindowRealm(context, sandbox);
|
||||||
realm.initialize();
|
realm.#initialize();
|
||||||
return realm;
|
return realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -169,16 +161,26 @@ export class WindowRealm extends Realm {
|
|||||||
// keep-sorted end
|
// keep-sorted end
|
||||||
}
|
}
|
||||||
|
|
||||||
override initialize(): void {
|
#initialize(): void {
|
||||||
super.initialize();
|
const browsingContextEmitter = this.disposables.use(
|
||||||
|
new EventEmitter(this.browsingContext)
|
||||||
|
);
|
||||||
|
browsingContextEmitter.on('closed', ({reason}) => {
|
||||||
|
this.dispose(reason);
|
||||||
|
});
|
||||||
|
|
||||||
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
|
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
|
||||||
sessionEmitter.on('script.realmCreated', info => {
|
sessionEmitter.on('script.realmCreated', info => {
|
||||||
if (info.type !== 'window') {
|
if (
|
||||||
|
info.type !== 'window' ||
|
||||||
|
info.context !== this.browsingContext.id ||
|
||||||
|
info.sandbox !== this.sandbox
|
||||||
|
) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
(this as any).id = info.realm;
|
(this as any).id = info.realm;
|
||||||
(this as any).origin = info.origin;
|
(this as any).origin = info.origin;
|
||||||
|
this.emit('updated', this);
|
||||||
});
|
});
|
||||||
sessionEmitter.on('script.realmCreated', info => {
|
sessionEmitter.on('script.realmCreated', info => {
|
||||||
if (info.type !== 'dedicated-worker') {
|
if (info.type !== 'dedicated-worker') {
|
||||||
@ -244,7 +246,7 @@ export class DedicatedWorkerRealm extends Realm {
|
|||||||
origin: string
|
origin: string
|
||||||
): DedicatedWorkerRealm {
|
): DedicatedWorkerRealm {
|
||||||
const realm = new DedicatedWorkerRealm(owner, id, origin);
|
const realm = new DedicatedWorkerRealm(owner, id, origin);
|
||||||
realm.initialize();
|
realm.#initialize();
|
||||||
return realm;
|
return realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -262,10 +264,14 @@ export class DedicatedWorkerRealm extends Realm {
|
|||||||
this.owners = new Set([owner]);
|
this.owners = new Set([owner]);
|
||||||
}
|
}
|
||||||
|
|
||||||
override initialize(): void {
|
#initialize(): void {
|
||||||
super.initialize();
|
|
||||||
|
|
||||||
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
|
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
|
||||||
|
sessionEmitter.on('script.realmDestroyed', info => {
|
||||||
|
if (info.realm !== this.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.dispose('Realm already destroyed.');
|
||||||
|
});
|
||||||
sessionEmitter.on('script.realmCreated', info => {
|
sessionEmitter.on('script.realmCreated', info => {
|
||||||
if (info.type !== 'dedicated-worker') {
|
if (info.type !== 'dedicated-worker') {
|
||||||
return;
|
return;
|
||||||
@ -302,7 +308,7 @@ export class SharedWorkerRealm extends Realm {
|
|||||||
origin: string
|
origin: string
|
||||||
): SharedWorkerRealm {
|
): SharedWorkerRealm {
|
||||||
const realm = new SharedWorkerRealm(owners, id, origin);
|
const realm = new SharedWorkerRealm(owners, id, origin);
|
||||||
realm.initialize();
|
realm.#initialize();
|
||||||
return realm;
|
return realm;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -320,10 +326,14 @@ export class SharedWorkerRealm extends Realm {
|
|||||||
this.owners = new Set(owners);
|
this.owners = new Set(owners);
|
||||||
}
|
}
|
||||||
|
|
||||||
override initialize(): void {
|
#initialize(): void {
|
||||||
super.initialize();
|
|
||||||
|
|
||||||
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
|
const sessionEmitter = this.disposables.use(new EventEmitter(this.session));
|
||||||
|
sessionEmitter.on('script.realmDestroyed', info => {
|
||||||
|
if (info.realm !== this.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.dispose('Realm already destroyed.');
|
||||||
|
});
|
||||||
sessionEmitter.on('script.realmCreated', info => {
|
sessionEmitter.on('script.realmCreated', info => {
|
||||||
if (info.type !== 'dedicated-worker') {
|
if (info.type !== 'dedicated-worker') {
|
||||||
return;
|
return;
|
||||||
|
Loading…
Reference in New Issue
Block a user