-
Notifications
You must be signed in to change notification settings - Fork 9.6k
core(fr): add session.onAnyProtocolMessage listener #11995
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
Conversation
| for (const method of delegateMethods) { | ||
| describe(`.${method}`, () => { | ||
| it('delegates to puppeteer', async () => { | ||
| const puppeteerFn = puppeteerSession[method] = jest.fn(); |
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.
is this change doing something?
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.
no just a prettier artifact
connorjclark
left a comment
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.
LGTM
wondering if changing on/once to accept a regex would be better api
| */ | ||
| 'use strict'; | ||
|
|
||
| const MONKEYPATCHED_EMIT = Symbol('monkeypatch'); |
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.
SessionEmitMonkeypatch or w.e. casing what-have-you.
I think some other method is desired due to the signature differences (possibly unavoidable for typescript explosion constraints? Crdp events already pushed the envelope before). You need to know what event happened for any function that can be invoked for more than 1 event type. I'm open to other names other than |
hmm right, the callback does need I did the easy part of the type change: somehow this work, idk why |
That still doesn't seem like a usable type signature though, or is that what you meant by the easy part? :) Taking a step back, I'm not really convinced that a regex-based method with a polymorphic signature is an easier API to understand and use than |
I was imagining usages of the "regexp" api would do the same.
ah, yea, ok so basically the code would end up being the same. nvm on the regexp.
|
SGTM 👍 |
|
Since holding off for release I played around with a few more ideas, WDYT about this @connorjclark? diff --git a/lighthouse-core/fraggle-rock/gather/session.js b/lighthouse-core/fragg
le-rock/gather/session.js
index dd15f9535..db3b27b6c 100644
--- a/lighthouse-core/fraggle-rock/gather/session.js
+++ b/lighthouse-core/fraggle-rock/gather/session.js
@@ -26,6 +26,11 @@ class ProtocolSession {
};
// @ts-expect-error - It's monkeypatching 🤷<U+200D>♂️.
session.emit[SessionEmitMonkeypatch] = true;
+
+ /** @type {LH.Gatherer.FRProtocolSession['on']} @param {Array<*>} args */
+ this.on = (...args) => args.length === 1 ?
+ session.on('*', args[0]) :
+ session.on(args[0], args[1]);
}
/**
diff --git a/types/gatherer.d.ts b/types/gatherer.d.ts
index 9fc0dd197..4d81705a8 100644
--- a/types/gatherer.d.ts
+++ b/types/gatherer.d.ts
@@ -17,6 +17,7 @@ declare global {
getNextProtocolTimeout(): number;
setNextProtocolTimeout(ms: number): void;
on<TEvent extends keyof LH.CrdpEvents>(event: TEvent, callback: (...args: LH.CrdpEvents[TEvent]) => void): void;
+ on(callback: (args: LH.Protocol.RawEventMessage) => void): void;
once<TEvent extends keyof LH.CrdpEvents>(event: TEvent, callback: (...args: LH.CrdpEvents[TEvent]) => void): void;
onAnyProtocolMessage(callback: (payload: LH.Protocol.RawEventMessage) => void): void
off<TEvent extends keyof LH.CrdpEvents>(event: TEvent, callback: (...args: LH.CrdpEvents[TEvent]) => void): void;Usage // Listen to a specific event.
session.on('Page.frameNavigated', event => console.log(event));
// Listen to *all* events.
session.on(({method, params}) => console.log(method, params)); |
5589240 to
6312aaa
Compare
|
I'll cast my vote for
Isn't this going to be called in like three places? Do we need a shorthand for it? |
|
Any other voters out there? The arguments are above, LMK by tomorrow at 10am CDT https://fast-poll.com/poll/4c080449 |
|
Alright the voters have spoken! Uncontested win for |
This reverts commit 6312aaa.
|
that's what I voted for doe |
😑... bro, then why 🎉 my proposal for what I thought your request was!? time on life I'll never get back 😆 |
|
i'm an irrational voter sry |
Summary
Paves the way for devtools log artifact on the new FR driver/session system. This monkeypatches our puppeteer session so we can listen for any protocol event. It's on the agenda to discuss at next sync with their team if they'd be open a PR for an official API, but it's relatively lightweight (and enables backcompat) in the meantime.
Related Issues/PRs
ref #11313