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.
The fix addresses an issue where links generated by the registerPortHandlers function were sometimes incorrect, particularly when right-clicking on them. Here's the breakdown of the changes:
Function Encapsulation: The core change involves encapsulating the logic that builds the link (link = ...) within the event listeners themselves (anchor.addEventListener("click", ...) and anchor.addEventListener("contextmenu", ...)). This is done within a loop iterating over anchor elements using actions.forEach.
Immediate Link Building: By building the link immediately within the event listeners, it ensures that the link generated uses the correct values from the anchor's attributes at the time the click or right-click event occurs.
The original code had a problem due to JavaScript closures. Here's the breakdown of what was happening:
Loop Execution: The loop creating event listeners ran, and the link variable was updated as expected during each iteration.
Closure Created: The anonymous functions created for the click and contextmenu handlers formed closures around the environment in which they were created, including the link variable.
Last Value Used: Since all event handlers were referencing the same link variable, they ended up using the last value it held after the loop finished executing, not the value associated with the specific link that was clicked.
By moving the link creation logic within the event listeners themselves, you ensure that the values of port, protocol, etc. are captured from the correct anchor element at the moment the event is triggered.