-
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(tsc): add type checking to use of CRDP events #4886
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,10 @@ const DEFAULT_NETWORK_QUIET_THRESHOLD = 5000; | |
// Controls how long to wait between longtasks before determining the CPU is idle, off by default | ||
const DEFAULT_CPU_QUIET_THRESHOLD = 0; | ||
|
||
/** | ||
* @typedef {LH.StrictEventEmitter<LH.CrdpEvents>} CrdpEventEmitter | ||
*/ | ||
|
||
class Driver { | ||
static get MAX_WAIT_FOR_FULLY_LOADED() { | ||
return 45 * 1000; | ||
|
@@ -39,6 +43,10 @@ class Driver { | |
*/ | ||
constructor(connection) { | ||
this._traceCategories = Driver.traceCategories; | ||
/** | ||
* An event emitter that enforces mapping between Crdp event names and payload types. | ||
* @type {CrdpEventEmitter} | ||
*/ | ||
this._eventEmitter = new EventEmitter(); | ||
this._connection = connection; | ||
// currently only used by WPT where just Page and Network are needed | ||
|
@@ -63,7 +71,7 @@ class Driver { | |
*/ | ||
this._monitoredUrl = null; | ||
|
||
connection.on('notification', event => { | ||
connection.on('protocolevent', event => { | ||
this._devtoolsLog.record(event); | ||
if (this._networkStatusMonitor) { | ||
this._networkStatusMonitor.dispatch(event.method, event.params); | ||
|
@@ -124,49 +132,6 @@ class Driver { | |
return this._connection.wsEndpoint(); | ||
} | ||
|
||
/** | ||
* Bind listeners for protocol events | ||
* @param {string} eventName | ||
* @param {(event: any) => void} cb | ||
*/ | ||
on(eventName, cb) { | ||
if (this._eventEmitter === null) { | ||
throw new Error('connect() must be called before attempting to listen to events.'); | ||
} | ||
|
||
// log event listeners being bound | ||
log.formatProtocol('listen for event =>', {method: eventName}, 'verbose'); | ||
this._eventEmitter.on(eventName, cb); | ||
} | ||
|
||
/** | ||
* Bind a one-time listener for protocol events. Listener is removed once it | ||
* has been called. | ||
* @param {string} eventName | ||
* @param {(event: any) => void} cb | ||
*/ | ||
once(eventName, cb) { | ||
if (this._eventEmitter === null) { | ||
throw new Error('connect() must be called before attempting to listen to events.'); | ||
} | ||
// log event listeners being bound | ||
log.formatProtocol('listen once for event =>', {method: eventName}, 'verbose'); | ||
this._eventEmitter.once(eventName, cb); | ||
} | ||
|
||
/** | ||
* Unbind event listeners | ||
* @param {string} eventName | ||
* @param {(event: any) => void} cb | ||
*/ | ||
off(eventName, cb) { | ||
if (this._eventEmitter === null) { | ||
throw new Error('connect() must be called before attempting to remove an event listener.'); | ||
} | ||
|
||
this._eventEmitter.removeListener(eventName, cb); | ||
} | ||
|
||
/** | ||
* Debounce enabling or disabling domains to prevent driver users from | ||
* stomping on each other. Maintains an internal count of the times a domain | ||
|
@@ -495,7 +460,7 @@ class Driver { | |
const checkForQuietExpression = `(${pageFunctions.checkTimeSinceLastLongTask.toString()})()`; | ||
/** | ||
* @param {Driver} driver | ||
* @param {(value: void) => void} resolve | ||
* @param {() => void} resolve | ||
*/ | ||
function checkForQuiet(driver, resolve) { | ||
if (cancelled) return; | ||
|
@@ -893,7 +858,7 @@ class Driver { | |
} | ||
|
||
/** | ||
* @param {{additionalTraceCategories: string=}=} settings | ||
* @param {{additionalTraceCategories?: string}=} settings | ||
* @return {Promise<void>} | ||
*/ | ||
beginTrace(settings) { | ||
|
@@ -946,8 +911,8 @@ class Driver { | |
endTrace() { | ||
return new Promise((resolve, reject) => { | ||
// When the tracing has ended this will fire with a stream handle. | ||
this.once('Tracing.tracingComplete', streamHandle => { | ||
this._readTraceFromStream(streamHandle) | ||
this.once('Tracing.tracingComplete', completeEvent => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note that these are now type checked (not |
||
this._readTraceFromStream(completeEvent) | ||
.then(traceContents => resolve(traceContents), reject); | ||
}); | ||
|
||
|
@@ -957,15 +922,15 @@ class Driver { | |
} | ||
|
||
/** | ||
* @param {LH.Crdp.Tracing.TracingCompleteEvent} streamHandle | ||
* @param {LH.Crdp.Tracing.TracingCompleteEvent} traceCompleteEvent | ||
*/ | ||
_readTraceFromStream(streamHandle) { | ||
_readTraceFromStream(traceCompleteEvent) { | ||
return new Promise((resolve, reject) => { | ||
let isEOF = false; | ||
const parser = new TraceParser(); | ||
|
||
const readArguments = { | ||
handle: streamHandle.stream, | ||
handle: traceCompleteEvent.stream, | ||
}; | ||
|
||
/** | ||
|
@@ -1204,4 +1169,45 @@ class Driver { | |
} | ||
} | ||
|
||
// Declared outside class body because function expressions can be typed via more expressive @type | ||
/** | ||
* Bind listeners for protocol events. | ||
* @type {CrdpEventEmitter['on']} | ||
*/ | ||
Driver.prototype.on = function on(eventName, cb) { | ||
if (this._eventEmitter === null) { | ||
throw new Error('connect() must be called before attempting to listen to events.'); | ||
} | ||
|
||
// log event listeners being bound | ||
log.formatProtocol('listen for event =>', {method: eventName}, 'verbose'); | ||
this._eventEmitter.on(eventName, cb); | ||
}; | ||
|
||
/** | ||
* Bind a one-time listener for protocol events. Listener is removed once it | ||
* has been called. | ||
* @type {CrdpEventEmitter['once']} | ||
*/ | ||
Driver.prototype.once = function once(eventName, cb) { | ||
if (this._eventEmitter === null) { | ||
throw new Error('connect() must be called before attempting to listen to events.'); | ||
} | ||
// log event listeners being bound | ||
log.formatProtocol('listen once for event =>', {method: eventName}, 'verbose'); | ||
this._eventEmitter.once(eventName, cb); | ||
}; | ||
|
||
/** | ||
* Unbind event listener. | ||
* @type {CrdpEventEmitter['removeListener']} | ||
*/ | ||
Driver.prototype.off = function off(eventName, cb) { | ||
if (this._eventEmitter === null) { | ||
throw new Error('connect() must be called before attempting to remove an event listener.'); | ||
} | ||
|
||
this._eventEmitter.removeListener(eventName, cb); | ||
}; | ||
|
||
module.exports = Driver; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
discussed this with @paulirish earlier. Since this event emitter can only emit protocol events (and
Driver
is the only receiver), it makes sense to make the single event it handles be something more descriptive than "notification" :)