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 {
|
reject(id: number, message: string, originalMessage?: string): void {
|
||||||
const callback = this.#callbacks.get(id);
|
const callback = this.#callbacks.get(id);
|
||||||
if (!callback) {
|
if (!callback) {
|
||||||
|
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
import {ChildProcess} from 'child_process';
|
import {ChildProcess} from 'child_process';
|
||||||
|
|
||||||
|
import * as Bidi from 'chromium-bidi/lib/cjs/protocol/protocol.js';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Browser as BrowserBase,
|
Browser as BrowserBase,
|
||||||
BrowserCloseCallback,
|
BrowserCloseCallback,
|
||||||
@ -30,14 +32,16 @@ import {Connection} from './Connection.js';
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
export class Browser extends BrowserBase {
|
export class Browser extends BrowserBase {
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
static async create(opts: Options): Promise<Browser> {
|
static async create(opts: Options): Promise<Browser> {
|
||||||
// TODO: await until the connection is established.
|
// TODO: await until the connection is established.
|
||||||
try {
|
try {
|
||||||
await opts.connection.send('session.new', {});
|
await opts.connection.send('session.new', {});
|
||||||
} catch {}
|
} catch {}
|
||||||
|
await opts.connection.send('session.subscribe', {
|
||||||
|
events: [
|
||||||
|
'browsingContext.contextCreated',
|
||||||
|
] as Bidi.Session.SubscribeParametersEvent[],
|
||||||
|
});
|
||||||
return new Browser(opts);
|
return new Browser(opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,9 +49,6 @@ export class Browser extends BrowserBase {
|
|||||||
#closeCallback?: BrowserCloseCallback;
|
#closeCallback?: BrowserCloseCallback;
|
||||||
#connection: Connection;
|
#connection: Connection;
|
||||||
|
|
||||||
/**
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
constructor(opts: Options) {
|
constructor(opts: Options) {
|
||||||
super();
|
super();
|
||||||
this.#process = opts.process;
|
this.#process = opts.process;
|
||||||
|
@ -140,30 +140,37 @@ export class Connection extends EventEmitter {
|
|||||||
object.message
|
object.message
|
||||||
);
|
);
|
||||||
} else {
|
} 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);
|
this.#callbacks.resolve(object.id, object);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let context: Context | undefined;
|
this.#handleSpecialEvents(object);
|
||||||
if ('context' in object.params) {
|
this.#maybeEmitOnContext(object);
|
||||||
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.emit(object.method, object.params);
|
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 {
|
#onClose(): void {
|
||||||
if (this.#closed) {
|
if (this.#closed) {
|
||||||
return;
|
return;
|
||||||
|
@ -107,12 +107,6 @@
|
|||||||
"parameters": ["cdp", "firefox"],
|
"parameters": ["cdp", "firefox"],
|
||||||
"expectations": ["SKIP"]
|
"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",
|
"testIdPattern": "[evaluation.spec] Evaluation specs Frame.evaluate should execute after cross-site navigation",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -671,6 +665,12 @@
|
|||||||
"parameters": ["cdp", "firefox"],
|
"parameters": ["cdp", "firefox"],
|
||||||
"expectations": ["FAIL"]
|
"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",
|
"testIdPattern": "[emulation.spec] Emulation Page.viewport should support landscape emulation",
|
||||||
"platforms": ["darwin", "linux", "win32"],
|
"platforms": ["darwin", "linux", "win32"],
|
||||||
@ -1835,6 +1835,12 @@
|
|||||||
"parameters": ["cdp", "firefox"],
|
"parameters": ["cdp", "firefox"],
|
||||||
"expectations": ["FAIL"]
|
"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\"",
|
"testIdPattern": "[requestinterception-experimental.spec] request interception \"after each\" hook in \"request interception\"",
|
||||||
"platforms": ["win32"],
|
"platforms": ["win32"],
|
||||||
|
Loading…
Reference in New Issue
Block a user