From 3ffafc67d5120a1958d07c30d71ef80b75836e17 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Wed, 30 Aug 2023 12:25:07 +0200 Subject: [PATCH] refactor: event maps and docs warnings (#10800) --- packages/puppeteer-core/src/common/Browser.ts | 2 +- packages/puppeteer-core/src/common/Page.ts | 112 ++++-------------- .../puppeteer-core/src/common/WebWorker.ts | 7 +- packages/puppeteer-core/src/common/common.ts | 1 + 4 files changed, 27 insertions(+), 95 deletions(-) diff --git a/packages/puppeteer-core/src/common/Browser.ts b/packages/puppeteer-core/src/common/Browser.ts index 478e03dd..7cb84490 100644 --- a/packages/puppeteer-core/src/common/Browser.ts +++ b/packages/puppeteer-core/src/common/Browser.ts @@ -610,7 +610,7 @@ export class CDPBrowserContext extends BrowserContext { * * @returns Promise which resolves to an array of all open pages. * Non visible pages, such as `"background_page"`, will not be listed here. - * You can find them using {@link CDPTarget.page | the target page}. + * You can find them using {@link Target.page | the target page}. */ override async pages(): Promise { const pages = await Promise.all( diff --git a/packages/puppeteer-core/src/common/Page.ts b/packages/puppeteer-core/src/common/Page.ts index ba94fac4..54cfbb07 100644 --- a/packages/puppeteer-core/src/common/Page.ts +++ b/packages/puppeteer-core/src/common/Page.ts @@ -152,54 +152,38 @@ export class CDPPage extends Page { #frameManagerHandlers = new Map>([ [ FrameManagerEmittedEvents.FrameAttached, - event => { - return this.emit(PageEmittedEvents.FrameAttached, event); - }, + this.emit.bind(this, PageEmittedEvents.FrameAttached), ], [ FrameManagerEmittedEvents.FrameDetached, - event => { - return this.emit(PageEmittedEvents.FrameDetached, event); - }, + this.emit.bind(this, PageEmittedEvents.FrameDetached), ], [ FrameManagerEmittedEvents.FrameNavigated, - event => { - return this.emit(PageEmittedEvents.FrameNavigated, event); - }, + this.emit.bind(this, PageEmittedEvents.FrameNavigated), ], ]); #networkManagerHandlers = new Map>([ [ NetworkManagerEmittedEvents.Request, - event => { - return this.emit(PageEmittedEvents.Request, event); - }, + this.emit.bind(this, PageEmittedEvents.Request), ], [ NetworkManagerEmittedEvents.RequestServedFromCache, - event => { - return this.emit(PageEmittedEvents.RequestServedFromCache, event); - }, + this.emit.bind(this, PageEmittedEvents.RequestServedFromCache), ], [ NetworkManagerEmittedEvents.Response, - event => { - return this.emit(PageEmittedEvents.Response, event); - }, + this.emit.bind(this, PageEmittedEvents.Response), ], [ NetworkManagerEmittedEvents.RequestFailed, - event => { - return this.emit(PageEmittedEvents.RequestFailed, event); - }, + this.emit.bind(this, PageEmittedEvents.RequestFailed), ], [ NetworkManagerEmittedEvents.RequestFinished, - event => { - return this.emit(PageEmittedEvents.RequestFinished, event); - }, + this.emit.bind(this, PageEmittedEvents.RequestFinished), ], ]); @@ -214,70 +198,17 @@ export class CDPPage extends Page { ], [ 'Page.domContentEventFired', - () => { - return this.emit(PageEmittedEvents.DOMContentLoaded); - }, - ], - [ - 'Page.loadEventFired', - () => { - return this.emit(PageEmittedEvents.Load); - }, - ], - [ - 'Page.loadEventFired', - () => { - return this.emit(PageEmittedEvents.Load); - }, - ], - [ - 'Runtime.consoleAPICalled', - event => { - return this.#onConsoleAPI(event); - }, - ], - [ - 'Runtime.bindingCalled', - event => { - return this.#onBindingCalled(event); - }, - ], - [ - 'Page.javascriptDialogOpening', - event => { - return this.#onDialog(event); - }, - ], - [ - 'Runtime.exceptionThrown', - exception => { - return this.#handleException(exception.exceptionDetails); - }, - ], - [ - 'Inspector.targetCrashed', - () => { - return this.#onTargetCrashed(); - }, - ], - [ - 'Performance.metrics', - event => { - return this.#emitMetrics(event); - }, - ], - [ - 'Log.entryAdded', - event => { - return this.#onLogEntryAdded(event); - }, - ], - [ - 'Page.fileChooserOpened', - event => { - return this.#onFileChooser(event); - }, + this.emit.bind(this, PageEmittedEvents.DOMContentLoaded), ], + ['Page.loadEventFired', this.emit.bind(this, PageEmittedEvents.Load)], + ['Runtime.consoleAPICalled', this.#onConsoleAPI.bind(this)], + ['Runtime.bindingCalled', this.#onBindingCalled.bind(this)], + ['Page.javascriptDialogOpening', this.#onDialog.bind(this)], + ['Runtime.exceptionThrown', this.#handleException.bind(this)], + ['Inspector.targetCrashed', this.#onTargetCrashed.bind(this)], + ['Performance.metrics', this.#emitMetrics.bind(this)], + ['Log.entryAdded', this.#onLogEntryAdded.bind(this)], + ['Page.fileChooserOpened', this.#onFileChooser.bind(this)], ]); /** @@ -802,8 +733,11 @@ export class CDPPage extends Page { return result; } - #handleException(exceptionDetails: Protocol.Runtime.ExceptionDetails): void { - this.emit(PageEmittedEvents.PageError, createClientError(exceptionDetails)); + #handleException(exception: Protocol.Runtime.ExceptionThrownEvent): void { + this.emit( + PageEmittedEvents.PageError, + createClientError(exception.exceptionDetails) + ); } async #onConsoleAPI( diff --git a/packages/puppeteer-core/src/common/WebWorker.ts b/packages/puppeteer-core/src/common/WebWorker.ts index ba7a9627..cda9df91 100644 --- a/packages/puppeteer-core/src/common/WebWorker.ts +++ b/packages/puppeteer-core/src/common/WebWorker.ts @@ -38,7 +38,7 @@ export type ConsoleAPICalledCallback = ( * @internal */ export type ExceptionThrownCallback = ( - details: Protocol.Runtime.ExceptionDetails + event: Protocol.Runtime.ExceptionThrownEvent ) => void; /** @@ -69,7 +69,6 @@ export type ExceptionThrownCallback = ( */ export class WebWorker extends EventEmitter { #executionContext = Deferred.create(); - #client: CDPSession; #url: string; @@ -104,9 +103,7 @@ export class WebWorker extends EventEmitter { debugError(err); } }); - this.#client.on('Runtime.exceptionThrown', exception => { - return exceptionThrown(exception.exceptionDetails); - }); + this.#client.on('Runtime.exceptionThrown', exceptionThrown); // This might fail if the target is closed before we receive all execution contexts. this.#client.send('Runtime.enable').catch(debugError); diff --git a/packages/puppeteer-core/src/common/common.ts b/packages/puppeteer-core/src/common/common.ts index 3cdabf99..643773fb 100644 --- a/packages/puppeteer-core/src/common/common.ts +++ b/packages/puppeteer-core/src/common/common.ts @@ -16,6 +16,7 @@ export * from './Accessibility.js'; export * from './AriaQueryHandler.js'; +export * from './Binding.js'; export * from './Browser.js'; export * from './BrowserConnector.js'; export * from './BrowserWebSocketTransport.js';