Skip to content

Conversation

@lyzadanger
Copy link
Contributor

I ran into a confusion today while converting some hooks over to more consistently use listener-collection. Some worked, but a focus-event-related hook did not. I realized it was because listener-collection wasn't passing the options parameter (when provided) to removeEventListener. This matters if you have an event that was registered with options (specifically capture: true) as they don't get removed if you don't pass them options!

Converted it to TS while I was in there.

The tests added to the tests module will fail without the change made to the module.

It's necessary to pass the same `options` to `removeEventListener` as
`addEventListener`, or the listener will not be removed.
@lyzadanger lyzadanger requested a review from robertknight March 9, 2023 18:41
@codecov
Copy link

codecov bot commented Mar 9, 2023

Codecov Report

Merging #910 (761cb50) into main (a3186bb) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #910   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           60        60           
  Lines          747       747           
  Branches       271       271           
=========================================
  Hits           747       747           
Impacted Files Coverage Δ
src/util/listener-collection.ts 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Contributor

@robertknight robertknight left a comment

Choose a reason for hiding this comment

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

LGTM. Once our browser support target passes Safari >= 15 we can simplify this code by using the signal option as a way to disconnect listeners, instead of having to remember all the addEventListener arguments and pass them to removeEventListener.

Copy link
Contributor

@robertknight robertknight left a comment

Choose a reason for hiding this comment

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

LGTM. As noted in Slack, you could get away with abbreviations for the generics if you wanted to make the signature shorter, but I think this is readable as-is.

eventType: Type,
listener: (event: EventType<ListenerTarget, Type>) => void,
options?: AddEventListenerOptions
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be helpful for readers to indicate explicitly that the return type is a symbol.

@lyzadanger lyzadanger merged commit 3ab3010 into main Mar 10, 2023
@lyzadanger lyzadanger deleted the update-listener-collection branch March 10, 2023 13:55
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.

3 participants