You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Error Handling:
The removeCallback method throws an error if the callback ID is not found. This could be handled more gracefully, perhaps by returning a boolean indicating success or failure, rather than throwing an error which might need to be caught by the caller.
Memory Management:
There is no apparent mechanism to prevent the #listener map from growing indefinitely as callbacks are added and not removed. This could potentially lead to memory leaks if not managed properly.
Improve the performance of the removeCallback method by storing a mapping of callback IDs to their respective event types
In the removeCallback method, instead of iterating over all event types to find the callback ID, you can store a mapping of callback IDs to their respective event types. This will improve the performance of the removeCallback method.
-let hasId = false-for (const [, callbacks] of this.#listener) {- if (callbacks.has(id)) {- callbacks.delete(id)- hasId = true- }-}--if (!hasId) {+const eventType = this.#callbackIdToEventType.get(id)+if (!eventType) {
throw Error(`Callback with id ${id} not found`)
}
+this.#listener.get(eventType).delete(id)+this.#callbackIdToEventType.delete(id)
Apply this suggestion
Suggestion importance[1-10]: 8
Why: The suggestion correctly identifies a performance issue in the removeCallback method and provides a valid solution to optimize the callback management by mapping IDs to event types, which can significantly reduce the time complexity of the method.
8
Possible issue
Add a check in the addCallback method to ensure the eventType is valid before adding the callback
In the addCallback method, add a check to ensure that the eventType is valid before adding the callback. This will prevent potential issues if an invalid event type is passed.
+if (!this.#listener.has(eventType)) {+ throw new Error(`Invalid event type: ${eventType}`)+}
const id = ++this.#callbackId
const eventCallbackMap = this.#listener.get(eventType)
eventCallbackMap.set(id, callback)
return id
Apply this suggestion
Suggestion importance[1-10]: 7
Why: This suggestion correctly addresses a potential issue where an invalid eventType could be passed to the addCallback method. Adding a check enhances the robustness of the method.
7
Robustness
Add error handling in the invokeCallbacks method to catch exceptions thrown by callback functions
In the invokeCallbacks method, add error handling to catch any exceptions thrown by the callback functions to prevent the entire process from failing.
Why: Adding error handling in the invokeCallbacks method is a good practice to ensure that exceptions in callbacks do not interrupt the overall execution flow, thus improving the robustness of the event handling system.
7
Best practice
Remove the redundant await keyword before the return statement in the subscribeAndHandleEvent method
In the subscribeAndHandleEvent method, remove the redundant await keyword before the return statement as it is not necessary.
Why: The suggestion to remove the redundant await is correct as it simplifies the code without affecting functionality. However, it is a minor improvement in terms of code cleanliness and does not impact performance or functionality significantly.
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.
User description
Thanks for contributing to Selenium!
A PR well described will help maintainers to quickly review and merge it
Before submitting your PR, please check our contributing guidelines.
Avoid large PRs, help reviewers by making them as simple and short as possible.
Description
Precursor to adding methods for Dom Mutations handling.
Motivation and Context
Adding a way to add callbacks and remove the callbacks for script module related events.
Types of changes
Checklist
PR Type
Enhancement
Description
ScriptEvent
constants to define event types (MESSAGE
,REALM_CREATED
,REALM_DESTROYED
).addCallback
andremoveCallback
methods to manage event callbacks.invokeCallbacks
method to trigger all registered callbacks for a specific event.onMessage
,onRealmCreated
, andonRealmDestroyed
methods to useScriptEvent
constants.subscribeAndHandleEvent
to register callbacks and handle WebSocket messages.Changes walkthrough 📝
scriptManager.js
Add callback management and event handling in ScriptManager
javascript/node/selenium-webdriver/bidi/scriptManager.js
ScriptEvent
constants for event types.invokeCallbacks
to trigger callbacks.ScriptEvent
constants.