Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove EventSubscription #12277

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 14 additions & 25 deletions packages/puppeteer-core/src/cdp/Coverage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down
87 changes: 30 additions & 57 deletions packages/puppeteer-core/src/cdp/LifecycleWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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)
)
);
this.#subscriptions.use(
new EventSubscription(
frame,
FrameEvent.FrameSwapped,
this.#frameSwapped.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.FrameSwappedByActivation,
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.FrameDetached,
this.#onFrameDetached.bind(this)
)
frameEmitter.on(FrameEvent.FrameDetached, this.#onFrameDetached.bind(this));

const networkManagerEmitter = this.#subscriptions.use(
new EventEmitter(networkManager)
);
this.#subscriptions.use(
new EventSubscription(
networkManager,
NetworkManagerEvent.Request,
this.#onRequest.bind(this)
)
networkManagerEmitter.on(
NetworkManagerEvent.Request,
this.#onRequest.bind(this)
);
this.#subscriptions.use(
new EventSubscription(
networkManager,
NetworkManagerEvent.Response,
this.#onResponse.bind(this)
)
networkManagerEmitter.on(
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`,
Expand Down
14 changes: 7 additions & 7 deletions packages/puppeteer-core/src/cdp/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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', {
Expand Down