mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
chore: use events rather than commands (#9904)
This commit is contained in:
parent
6c018acf5d
commit
48e7bdd75e
@ -137,10 +137,6 @@ export class CallbackRegistry {
|
||||
});
|
||||
}
|
||||
|
||||
getCallback(id: number): Callback | undefined {
|
||||
return this.#callbacks.get(id);
|
||||
}
|
||||
|
||||
reject(id: number, message: string, originalMessage?: string): void {
|
||||
const callback = this.#callbacks.get(id);
|
||||
if (!callback) {
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
import {ChildProcess} from 'child_process';
|
||||
|
||||
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
|
||||
|
||||
import {
|
||||
Browser as BrowserBase,
|
||||
BrowserCloseCallback,
|
||||
@ -30,14 +32,16 @@ import {Connection} from './Connection.js';
|
||||
* @internal
|
||||
*/
|
||||
export class Browser extends BrowserBase {
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
static async create(opts: Options): Promise<Browser> {
|
||||
// TODO: await until the connection is established.
|
||||
try {
|
||||
await opts.connection.send('session.new', {});
|
||||
} catch {}
|
||||
await opts.connection.send('session.subscribe', {
|
||||
events: [
|
||||
'browsingContext.contextCreated',
|
||||
] as Bidi.Session.SubscribeParametersEvent[],
|
||||
});
|
||||
return new Browser(opts);
|
||||
}
|
||||
|
||||
@ -45,9 +49,6 @@ export class Browser extends BrowserBase {
|
||||
#closeCallback?: BrowserCloseCallback;
|
||||
#connection: Connection;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
constructor(opts: Options) {
|
||||
super();
|
||||
this.#process = opts.process;
|
||||
|
@ -140,30 +140,37 @@ export class Connection extends EventEmitter {
|
||||
object.message
|
||||
);
|
||||
} else {
|
||||
if (
|
||||
this.#callbacks.getCallback(object.id)?.label ===
|
||||
'browsingContext.create'
|
||||
) {
|
||||
this.#contexts.set(
|
||||
object.result.context,
|
||||
new Context(this, object.result)
|
||||
);
|
||||
}
|
||||
this.#callbacks.resolve(object.id, object);
|
||||
}
|
||||
} else {
|
||||
let context: Context | undefined;
|
||||
if ('context' in object.params) {
|
||||
context = this.#contexts.get(object.params.context);
|
||||
} else if ('source' in object.params && !!object.params.source.context) {
|
||||
context = this.#contexts.get(object.params.source.context);
|
||||
}
|
||||
context?.emit(object.method, object.params);
|
||||
|
||||
this.#handleSpecialEvents(object);
|
||||
this.#maybeEmitOnContext(object);
|
||||
this.emit(object.method, object.params);
|
||||
}
|
||||
}
|
||||
|
||||
#maybeEmitOnContext(event: Bidi.Message.EventMessage) {
|
||||
let context: Context | undefined;
|
||||
// Context specific events
|
||||
if ('context' in event.params) {
|
||||
context = this.#contexts.get(event.params.context);
|
||||
// `log.entryAdded` specific context
|
||||
} else if ('source' in event.params && !!event.params.source.context) {
|
||||
context = this.#contexts.get(event.params.source.context);
|
||||
}
|
||||
context?.emit(event.method, event.params);
|
||||
}
|
||||
|
||||
#handleSpecialEvents(event: Bidi.Message.EventMessage) {
|
||||
switch (event.method) {
|
||||
case 'browsingContext.contextCreated':
|
||||
this.#contexts.set(
|
||||
event.params.context,
|
||||
new Context(this, event.params)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#onClose(): void {
|
||||
if (this.#closed) {
|
||||
return;
|
||||
|
@ -107,12 +107,6 @@
|
||||
"parameters": ["cdp", "firefox"],
|
||||
"expectations": ["SKIP"]
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[emulation.spec] Emulation Page.viewport should detect touch when applying viewport with touches",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
"parameters": ["firefox"],
|
||||
"expectations": ["PASS", "TIMEOUT"]
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[evaluation.spec] Evaluation specs Frame.evaluate should execute after cross-site navigation",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
@ -671,6 +665,12 @@
|
||||
"parameters": ["cdp", "firefox"],
|
||||
"expectations": ["FAIL"]
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[emulation.spec] Emulation Page.viewport should detect touch when applying viewport with touches",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
"parameters": ["cdp", "firefox"],
|
||||
"expectations": ["PASS", "TIMEOUT"]
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[emulation.spec] Emulation Page.viewport should support landscape emulation",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
@ -1835,6 +1835,12 @@
|
||||
"parameters": ["cdp", "firefox"],
|
||||
"expectations": ["FAIL"]
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[queryhandler.spec] Query handler tests P selectors should work with deep combinators",
|
||||
"platforms": ["darwin", "linux", "win32"],
|
||||
"parameters": ["cdp", "firefox"],
|
||||
"expectations": ["FAIL"]
|
||||
},
|
||||
{
|
||||
"testIdPattern": "[requestinterception-experimental.spec] request interception \"after each\" hook in \"request interception\"",
|
||||
"platforms": ["win32"],
|
||||
|
Loading…
Reference in New Issue
Block a user