Skip to content

websockets: added removeEventListener, modified existing related code and added a test - #6237

Open
Saad259 wants to merge 1 commit into
grafana:masterfrom
Saad259:feature/websockets-remove-event-listener
Open

websockets: added removeEventListener, modified existing related code and added a test#6237
Saad259 wants to merge 1 commit into
grafana:masterfrom
Saad259:feature/websockets-remove-event-listener

Conversation

@Saad259

@Saad259 Saad259 commented Aug 2, 2026

Copy link
Copy Markdown

What?

This PR adds a removeEventListener to the k6/websockets module. To support this, addEventListener's handler parameter was changed from an auto-converted Go closure (func(sobek.Value) (sobek.Value, error)) to a raw sobek.Value. This preserves the original JS function reference, which is needed to identify and remove a specific listener later (because a plain Go closure has no way to be compared back to the JS function it was created from). Each registered listener is now stored as a sobek.Value/callable pair (listenerEntry), and removeEventListener matches on the incoming handler using sobek.Value.SameAs().

Why?

k6/websockets is missing the counterpart to addEventListener. This is useful in scenarios with many active listeners, where removing listeners that are no longer needed matters.

Checklist

  • I have performed a self-review of my code.
  • I have commented on my code, particularly in hard-to-understand areas.
  • I have added tests for my changes.
  • I have run linter and tests locally (make check) and all pass.

Checklist: Documentation (only for k6 maintainers and if relevant)

Please do not merge this PR until the following items are filled out.

  • I have added the correct milestone and labels to the PR.
  • I have updated the release notes: link
  • I have updated or added an issue to the k6-documentation: grafana/k6-docs#NUMBER if applicable
  • I have updated or added an issue to the TypeScript definitions: grafana/k6-DefinitelyTyped#NUMBER if applicable

Related PR(s)/Issue(s)

Closes #5877
Related: #4823 #4844

@Saad259
Saad259 requested a review from a team as a code owner August 2, 2026 10:03
@Saad259
Saad259 requested review from ankur22 and janHildebrandt98 and removed request for a team August 2, 2026 10:03
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

Signed commits report

1 of 1 commit between master and feature/websockets-remove-event-listener could not be fully verified:

Commit Author Reason Message
59d79fde Saad259 unsigned websockets: added removeEventListener, modified existing related code and added a test

This repository requires all commits to be signed. See GitHub docs on commit signature verification.

@cla-assistant

cla-assistant Bot commented Aug 2, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

assert.NoError(t, err)
}

func TestRemoveEventListener(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add cases for removeEventListener with a non-function/null handler, and for removing a listener that was registered twice via addEventListener? Both are silent no-ops right now and neither is covered here.

}

func (w *webSocket) addEventListener(event string, handler func(sobek.Value) (sobek.Value, error)) {
func (w *webSocket) addEventListener(event string, handler sobek.Value) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
The spec actually says to dedup here:

If eventTarget’s event listener list does not contain an event listener whose type is listener’s type, callback is listener’s callback, and capture is listener’s capture, then append listener to eventTarget’s event listener list.

Now that you introduced comparability, could you also add the dedup? This would also get rid of this somewhat strange behavior that one remove() call could wipe multiple listeners at once.

@ankur22 wdyt? IMO we could do this in this PR but on paper it would be a breaking change and somewhat out of scope.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@janHildebrandt98 True true. I'll definitely add those test cases and I'd be happy to also work on dedup here itself (or under a new issue for it) if @ankur22 is happy with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Websockets removeEventListener

2 participants