mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: consolidate user context creation (#11880)
This commit is contained in:
parent
59c4daa5ee
commit
c75dbf4f36
@ -64,11 +64,6 @@ export class Browser extends EventEmitter<{
|
||||
// keep-sorted start
|
||||
this.session = session;
|
||||
// keep-sorted end
|
||||
|
||||
this.#userContexts.set(
|
||||
UserContext.DEFAULT,
|
||||
UserContext.create(this, UserContext.DEFAULT)
|
||||
);
|
||||
}
|
||||
|
||||
async #initialize() {
|
||||
@ -95,13 +90,7 @@ export class Browser extends EventEmitter<{
|
||||
} = await this.session.send('browser.getUserContexts', {});
|
||||
|
||||
for (const context of userContexts) {
|
||||
if (context.userContext === UserContext.DEFAULT) {
|
||||
continue;
|
||||
}
|
||||
this.#userContexts.set(
|
||||
context.userContext,
|
||||
UserContext.create(this, context.userContext)
|
||||
);
|
||||
this.#createUserContext(context.userContext);
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,16 +105,13 @@ export class Browser extends EventEmitter<{
|
||||
sessionEmitter.on('browsingContext.contextCreated', info => {
|
||||
contextIds.add(info.context);
|
||||
});
|
||||
sessionEmitter.on('browsingContext.contextDestroyed', info => {
|
||||
contextIds.delete(info.context);
|
||||
});
|
||||
const {result} = await this.session.send('browsingContext.getTree', {});
|
||||
contexts = result.contexts;
|
||||
}
|
||||
|
||||
// Simulating events so contexts are created naturally.
|
||||
for (const info of contexts) {
|
||||
if (contextIds.has(info.context)) {
|
||||
if (!contextIds.has(info.context)) {
|
||||
this.session.emit('browsingContext.contextCreated', info);
|
||||
}
|
||||
if (info.children) {
|
||||
@ -134,6 +120,22 @@ export class Browser extends EventEmitter<{
|
||||
}
|
||||
}
|
||||
|
||||
#createUserContext(id: string) {
|
||||
const userContext = UserContext.create(this, id);
|
||||
this.#userContexts.set(userContext.id, userContext);
|
||||
|
||||
const userContextEmitter = this.#disposables.use(
|
||||
new EventEmitter(userContext)
|
||||
);
|
||||
userContextEmitter.once('closed', () => {
|
||||
userContextEmitter.removeAllListeners();
|
||||
|
||||
this.#userContexts.delete(userContext.id);
|
||||
});
|
||||
|
||||
return userContext;
|
||||
}
|
||||
|
||||
// keep-sorted start block=yes
|
||||
get closed(): boolean {
|
||||
return this.#closed;
|
||||
@ -210,20 +212,7 @@ export class Browser extends EventEmitter<{
|
||||
const {
|
||||
result: {userContext: context},
|
||||
} = await this.session.send('browser.createUserContext', {});
|
||||
|
||||
const userContext = UserContext.create(this, context);
|
||||
this.#userContexts.set(userContext.id, userContext);
|
||||
|
||||
const userContextEmitter = this.#disposables.use(
|
||||
new EventEmitter(userContext)
|
||||
);
|
||||
userContextEmitter.once('closed', () => {
|
||||
userContextEmitter.removeAllListeners();
|
||||
|
||||
this.#userContexts.delete(context);
|
||||
});
|
||||
|
||||
return userContext;
|
||||
return this.#createUserContext(context);
|
||||
}
|
||||
|
||||
[disposeSymbol](): void {
|
||||
|
Loading…
Reference in New Issue
Block a user