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 {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 {assert} from '../util/assert.js';
import {DisposableStack} from '../util/disposable.js';
@ -229,19 +229,13 @@ export class JSCoverage {
this.#scriptURLs.clear();
this.#scriptSources.clear();
this.#subscriptions = new DisposableStack();
this.#subscriptions.use(
new EventSubscription(
this.#client,
'Debugger.scriptParsed',
this.#onScriptParsed.bind(this)
)
const clientEmitter = this.#subscriptions.use(
new EventEmitter(this.#client)
);
this.#subscriptions.use(
new EventSubscription(
this.#client,
'Runtime.executionContextsCleared',
this.#onExecutionContextsCleared.bind(this)
)
clientEmitter.on('Debugger.scriptParsed', this.#onScriptParsed.bind(this));
clientEmitter.on(
'Runtime.executionContextsCleared',
this.#onExecutionContextsCleared.bind(this)
);
await Promise.all([
this.#client.send('Profiler.enable'),
@ -355,20 +349,15 @@ export class CSSCoverage {
this.#stylesheetURLs.clear();
this.#stylesheetSources.clear();
this.#eventListeners = new DisposableStack();
this.#eventListeners.use(
new EventSubscription(
this.#client,
'CSS.styleSheetAdded',
this.#onStyleSheet.bind(this)
)
const clientEmitter = this.#eventListeners.use(
new EventEmitter(this.#client)
);
this.#eventListeners.use(
new EventSubscription(
this.#client,
'Runtime.executionContextsCleared',
this.#onExecutionContextsCleared.bind(this)
)
clientEmitter.on('CSS.styleSheetAdded', this.#onStyleSheet.bind(this));
clientEmitter.on(
'Runtime.executionContextsCleared',
this.#onExecutionContextsCleared.bind(this)
);
await Promise.all([
this.#client.send('DOM.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 {HTTPResponse} from '../api/HTTPResponse.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 {assert} from '../util/assert.js';
import {Deferred} from '../util/Deferred.js';
@ -103,70 +103,43 @@ export class LifecycleWatcher {
this.#frame = frame;
this.#timeout = timeout;
this.#subscriptions.use(
// Revert if TODO #1 is done
new EventSubscription(
frame._frameManager,
FrameManagerEvent.LifecycleEvent,
this.#checkLifecycleComplete.bind(this)
)
const frameManagerEmitter = this.#subscriptions.use(
new EventEmitter(frame._frameManager)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameNavigatedWithinDocument,
this.#navigatedWithinDocument.bind(this)
)
frameManagerEmitter.on(
FrameManagerEvent.LifecycleEvent,
this.#checkLifecycleComplete.bind(this)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameNavigated,
this.#navigated.bind(this)
)
const frameEmitter = this.#subscriptions.use(new EventEmitter(frame));
frameEmitter.on(
FrameEvent.FrameNavigatedWithinDocument,
this.#navigatedWithinDocument.bind(this)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameSwapped,
this.#frameSwapped.bind(this)
)
frameEmitter.on(FrameEvent.FrameNavigated, this.#navigated.bind(this));
frameEmitter.on(FrameEvent.FrameSwapped, this.#frameSwapped.bind(this));
frameEmitter.on(
FrameEvent.FrameSwappedByActivation,
this.#frameSwapped.bind(this)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameSwappedByActivation,
this.#frameSwapped.bind(this)
)
frameEmitter.on(FrameEvent.FrameDetached, this.#onFrameDetached.bind(this));
const networkManagerEmitter = this.#subscriptions.use(
new EventEmitter(networkManager)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameDetached,
this.#onFrameDetached.bind(this)
)
networkManagerEmitter.on(
NetworkManagerEvent.Request,
this.#onRequest.bind(this)
);
this.#subscriptions.use(
new EventSubscription(
networkManager,
NetworkManagerEvent.Request,
this.#onRequest.bind(this)
)
networkManagerEmitter.on(
NetworkManagerEvent.Response,
this.#onResponse.bind(this)
);
this.#subscriptions.use(
new EventSubscription(
networkManager,
NetworkManagerEvent.Response,
this.#onResponse.bind(this)
)
);
this.#subscriptions.use(
new EventSubscription(
networkManager,
NetworkManagerEvent.RequestFailed,
this.#onRequestFailed.bind(this)
)
networkManagerEmitter.on(
NetworkManagerEvent.RequestFailed,
this.#onRequestFailed.bind(this)
);
this.#terminationDeferred = Deferred.create<Error>({
timeout: this.#timeout,
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 type {Frame} from '../api/Frame.js';
import type {Credentials} from '../api/Page.js';
import {EventEmitter, EventSubscription} from '../common/EventEmitter.js';
import {EventEmitter} from '../common/EventEmitter.js';
import {
NetworkManagerEvent,
type NetworkManagerEvents,
@ -100,14 +100,14 @@ export class NetworkManager extends EventEmitter<NetworkManagerEvents> {
}
const subscriptions = new DisposableStack();
this.#clients.set(client, subscriptions);
const clientEmitter = subscriptions.use(new EventEmitter(client));
for (const [event, handler] of this.#handlers) {
subscriptions.use(
// TODO: Remove any here.
new EventSubscription(client, event, (arg: any) => {
return handler.bind(this)(client, arg);
})
);
clientEmitter.on(event, (arg: any) => {
return handler.bind(this)(client, arg);
});
}
await Promise.all([
this.#ignoreHTTPSErrors
? client.send('Security.setIgnoreCertificateErrors', {