refactor: remove EventSubscription (#12277)

This commit is contained in:
Nikolay Vitkov 2024-04-19 12:24:40 +02:00 committed by GitHub
parent 9a17ec3b2a
commit 29e33df2a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 89 deletions

View File

@ -7,7 +7,7 @@
import type {Protocol} from 'devtools-protocol'; import type {Protocol} from 'devtools-protocol';
import type {CDPSession} from '../api/CDPSession.js'; import type {CDPSession} from '../api/CDPSession.js';
import {EventSubscription} from '../common/EventEmitter.js'; import {EventEmitter} from '../common/EventEmitter.js';
import {debugError, PuppeteerURL} from '../common/util.js'; import {debugError, PuppeteerURL} from '../common/util.js';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
import {DisposableStack} from '../util/disposable.js'; import {DisposableStack} from '../util/disposable.js';
@ -229,19 +229,13 @@ export class JSCoverage {
this.#scriptURLs.clear(); this.#scriptURLs.clear();
this.#scriptSources.clear(); this.#scriptSources.clear();
this.#subscriptions = new DisposableStack(); this.#subscriptions = new DisposableStack();
this.#subscriptions.use( const clientEmitter = this.#subscriptions.use(
new EventSubscription( new EventEmitter(this.#client)
this.#client,
'Debugger.scriptParsed',
this.#onScriptParsed.bind(this)
)
); );
this.#subscriptions.use( clientEmitter.on('Debugger.scriptParsed', this.#onScriptParsed.bind(this));
new EventSubscription( clientEmitter.on(
this.#client,
'Runtime.executionContextsCleared', 'Runtime.executionContextsCleared',
this.#onExecutionContextsCleared.bind(this) this.#onExecutionContextsCleared.bind(this)
)
); );
await Promise.all([ await Promise.all([
this.#client.send('Profiler.enable'), this.#client.send('Profiler.enable'),
@ -355,20 +349,15 @@ export class CSSCoverage {
this.#stylesheetURLs.clear(); this.#stylesheetURLs.clear();
this.#stylesheetSources.clear(); this.#stylesheetSources.clear();
this.#eventListeners = new DisposableStack(); this.#eventListeners = new DisposableStack();
this.#eventListeners.use( const clientEmitter = this.#eventListeners.use(
new EventSubscription( new EventEmitter(this.#client)
this.#client,
'CSS.styleSheetAdded',
this.#onStyleSheet.bind(this)
)
); );
this.#eventListeners.use( clientEmitter.on('CSS.styleSheetAdded', this.#onStyleSheet.bind(this));
new EventSubscription( clientEmitter.on(
this.#client,
'Runtime.executionContextsCleared', 'Runtime.executionContextsCleared',
this.#onExecutionContextsCleared.bind(this) this.#onExecutionContextsCleared.bind(this)
)
); );
await Promise.all([ await Promise.all([
this.#client.send('DOM.enable'), this.#client.send('DOM.enable'),
this.#client.send('CSS.enable'), this.#client.send('CSS.enable'),

View File

@ -10,7 +10,7 @@ import {type Frame, FrameEvent} from '../api/Frame.js';
import type {HTTPRequest} from '../api/HTTPRequest.js'; import type {HTTPRequest} from '../api/HTTPRequest.js';
import type {HTTPResponse} from '../api/HTTPResponse.js'; import type {HTTPResponse} from '../api/HTTPResponse.js';
import type {TimeoutError} from '../common/Errors.js'; import type {TimeoutError} from '../common/Errors.js';
import {EventSubscription} from '../common/EventEmitter.js'; import {EventEmitter} from '../common/EventEmitter.js';
import {NetworkManagerEvent} from '../common/NetworkManagerEvents.js'; import {NetworkManagerEvent} from '../common/NetworkManagerEvents.js';
import {assert} from '../util/assert.js'; import {assert} from '../util/assert.js';
import {Deferred} from '../util/Deferred.js'; import {Deferred} from '../util/Deferred.js';
@ -103,70 +103,43 @@ export class LifecycleWatcher {
this.#frame = frame; this.#frame = frame;
this.#timeout = timeout; this.#timeout = timeout;
this.#subscriptions.use( const frameManagerEmitter = this.#subscriptions.use(
// Revert if TODO #1 is done new EventEmitter(frame._frameManager)
new EventSubscription( );
frame._frameManager, frameManagerEmitter.on(
FrameManagerEvent.LifecycleEvent, FrameManagerEvent.LifecycleEvent,
this.#checkLifecycleComplete.bind(this) this.#checkLifecycleComplete.bind(this)
)
); );
this.#subscriptions.use(
new EventSubscription( const frameEmitter = this.#subscriptions.use(new EventEmitter(frame));
frame, frameEmitter.on(
FrameEvent.FrameNavigatedWithinDocument, FrameEvent.FrameNavigatedWithinDocument,
this.#navigatedWithinDocument.bind(this) this.#navigatedWithinDocument.bind(this)
)
); );
this.#subscriptions.use( frameEmitter.on(FrameEvent.FrameNavigated, this.#navigated.bind(this));
new EventSubscription( frameEmitter.on(FrameEvent.FrameSwapped, this.#frameSwapped.bind(this));
frame, frameEmitter.on(
FrameEvent.FrameNavigated,
this.#navigated.bind(this)
)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameSwapped,
this.#frameSwapped.bind(this)
)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameSwappedByActivation, FrameEvent.FrameSwappedByActivation,
this.#frameSwapped.bind(this) this.#frameSwapped.bind(this)
)
); );
this.#subscriptions.use( frameEmitter.on(FrameEvent.FrameDetached, this.#onFrameDetached.bind(this));
new EventSubscription(
frame, const networkManagerEmitter = this.#subscriptions.use(
FrameEvent.FrameDetached, new EventEmitter(networkManager)
this.#onFrameDetached.bind(this)
)
); );
this.#subscriptions.use( networkManagerEmitter.on(
new EventSubscription(
networkManager,
NetworkManagerEvent.Request, NetworkManagerEvent.Request,
this.#onRequest.bind(this) this.#onRequest.bind(this)
)
); );
this.#subscriptions.use( networkManagerEmitter.on(
new EventSubscription(
networkManager,
NetworkManagerEvent.Response, NetworkManagerEvent.Response,
this.#onResponse.bind(this) this.#onResponse.bind(this)
)
); );
this.#subscriptions.use( networkManagerEmitter.on(
new EventSubscription(
networkManager,
NetworkManagerEvent.RequestFailed, NetworkManagerEvent.RequestFailed,
this.#onRequestFailed.bind(this) this.#onRequestFailed.bind(this)
)
); );
this.#terminationDeferred = Deferred.create<Error>({ this.#terminationDeferred = Deferred.create<Error>({
timeout: this.#timeout, timeout: this.#timeout,
message: `Navigation timeout of ${this.#timeout} ms exceeded`, message: `Navigation timeout of ${this.#timeout} ms exceeded`,

View File

@ -9,7 +9,7 @@ import type {Protocol} from 'devtools-protocol';
import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js'; import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js';
import type {Frame} from '../api/Frame.js'; import type {Frame} from '../api/Frame.js';
import type {Credentials} from '../api/Page.js'; import type {Credentials} from '../api/Page.js';
import {EventEmitter, EventSubscription} from '../common/EventEmitter.js'; import {EventEmitter} from '../common/EventEmitter.js';
import { import {
NetworkManagerEvent, NetworkManagerEvent,
type NetworkManagerEvents, type NetworkManagerEvents,
@ -100,14 +100,14 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
} }
const subscriptions = new DisposableStack(); const subscriptions = new DisposableStack();
this.#clients.set(client, subscriptions); this.#clients.set(client, subscriptions);
const clientEmitter = subscriptions.use(new EventEmitter(client));
for (const [event, handler] of this.#handlers) { for (const [event, handler] of this.#handlers) {
subscriptions.use( clientEmitter.on(event, (arg: any) => {
// TODO: Remove any here.
new EventSubscription(client, event, (arg: any) => {
return handler.bind(this)(client, arg); return handler.bind(this)(client, arg);
}) });
);
} }
await Promise.all([ await Promise.all([
this.#ignoreHTTPSErrors this.#ignoreHTTPSErrors
? client.send('Security.setIgnoreCertificateErrors', { ? client.send('Security.setIgnoreCertificateErrors', {