-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
core: make target manager accessible on driver #14122
Conversation
const cdpSession = await this._page.target().createCDPSession(); | ||
this._targetManager = new TargetManager(cdpSession); | ||
await this._targetManager.enable(); | ||
this.defaultSession = this._targetManager.rootSession(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because TargetManager
is in charge of creating FR ProtocolSession
s from CDPSession
s, and we want to eventually make those sessions available for gatherers to access directly, Driver now gets its driver.defaultSession
from targetManager instead of making it itself. This is a little weird, but otherwise we have two different session objects pointing to the same thing. This seemed conceptually simpler (driver setup is a little more involved but it means the set of all active sessions includes defaultSession
) but open to ideas.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big fan of avoiding the double session object! (I was seeing that at the CDP traffic level. so weird.)
* 'Domain.method2Name': {method: 'Domain.method2Name', params: EventPayload2}, | ||
* } | ||
*/ | ||
type RawEventMessageRecord = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an unchanged move from below, but it was previously scoped to the file and we need it exported now
this._networkMonitor.on('protocolmessage', this._onProtocolMessage); | ||
this._networkMonitor.enable(); | ||
driver.targetManager.on('protocolevent', this._onProtocolMessage); | ||
await driver.defaultSession.sendCommand('Page.enable'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we have so many Page.enable
s in LH that this has to be unnecessary, but this matches the behavior of the this._networkMonitor.enable()
call
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(first pass)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lots of great improvements here. thanks for taking this on.
const cdpSession = await this._page.target().createCDPSession(); | ||
this._targetManager = new TargetManager(cdpSession); | ||
await this._targetManager.enable(); | ||
this.defaultSession = this._targetManager.rootSession(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Big fan of avoiding the double session object! (I was seeing that at the CDP traffic level. so weird.)
const cdpSession = await this._page.target().createCDPSession(); | ||
this._targetManager = new TargetManager(cdpSession); | ||
await this._targetManager.enable(); | ||
this.defaultSession = this._targetManager.rootSession(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd love to s/defaultSession/rootSession/g but fine with it happening in a followup.
|
||
await session.sendCommand('Target.setAutoAttach', { | ||
// We want to receive information about network requests from iframes, so enable the Network domain. | ||
await newSession.sendCommand('Network.enable'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you think we should also tweak this to do a promise.all style? 4bc0b05
(im still surprised this is fine without it)
also.. i wonder if the finally
case hits because of a protocol_timeout on one of these?
i know you saw some patterns in pptr/playwright which felt more confident around this stuff. dunno if that's relevant.
targetManager: { | ||
rootSession(): FRProtocolSession; | ||
on(event: 'protocolevent', callback: (payload: Protocol.RawEventMessage) => void): void | ||
off(event: 'protocolevent', callback: (payload: Protocol.RawEventMessage) => void): void | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could give this its own interface. Then the target manager and legacy shim will implement that interface.
Might help with some of the event type TS stuff you did in target-manager.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm hoping that it'll stay nice and simple until LH 11, and then needing an interface won't even be a question with legacy gone, it'll just be itself.
const FRGatherer = require('../../fraggle-rock/gather/base-gatherer.js'); | ||
|
||
/** @typedef {import('../driver/target-manager.js')} TargetManager */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this used?
it's not, deleted :)
b24fc22
to
39b9d64
Compare
Addressed feedback and unit tests updated. Test updates are in separate commits to make it slightly easier to review. Let's add a mock simplification pass to our project TODOs, that was way harder (and more than 2x the lines changed) than refactoring the actual code :) |
@@ -31,58 +31,6 @@ describe('ProtocolSession', () => { | |||
session = new ProtocolSession(puppeteerSession); | |||
}); | |||
|
|||
describe('ProtocolSession', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tests in here moved to target-manager-test.js
This seems to close out the linked issue (I'm inferring from this quoted text that there is more to do, but can't tell what). |
I think mostly, but I believe @paulirish has a few more things adjacent to this, like settling on some naming conventions across files, using regular event emitters where possible, etc. We could discuss in #14078 and/or next sync. |
Should help significantly with #14078. Smoke tests should all pass, but waiting on feedback before touching the unit test mocks :)
This PR elevates
TargetManager
to a top-level property onDriver
. The intention is that TargetManagerCDPSession
s in FRsession
s), which should make the execution flow easier to understand and make future changes simpler.Driver
does, which includes across passes. It's basically a live view of all the sessions Lighthouse knows aboutsessionId
included)The legacy driver basically does these things already, so it was easy to expose a shim on it and keep
NetworkMonitor
(and its users) on a unified implementation. It's not pretty architecturally, but it is simple (yay) and should only be around until LH 11.Since
TargetManager
tracks all sessions and their targetInfo, in the future it could be used for things like, "give me all the iframe sessions" so you can send commands orevaluate()
in them easily, but nothing needs that right now and it's harder to include legacy support, so we can wait.re: @paulirish's #14120 (comment), AFAICT all sessions now have IDs, presumably because pptr is keeping the
sessionId
-less session to itself. I'd like to verify that and move us to a place wheresessionId
is required in the all-protocol-events type:lighthouse/types/protocol.d.ts
Lines 68 to 69 in 1d6ee0e
so code will hopefully be forced to contend with it. Meanwhile in this PR,
session
drops thesessionId
so it's likeCDPSession
. If you're subscribing to a single session's events, there's no need for a session ID. We can follow up and make thesession
event handling simpler, but I didn't want the changes in this PR interleaved with that change.