Skip to content
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

Preventing duplicate event listener registrations #554

Merged
merged 7 commits into from
May 15, 2023

Conversation

seungdeng17
Copy link
Contributor

@seungdeng17 seungdeng17 commented May 11, 2023

I encountered an issue where event listeners were being registered multiple times.

I discovered that when calling scrollTo-related functions in the animate-scroll module, event listeners were being registered through the subscribe method.

There was no logic to remove registered events or check for duplicates, resulting in event listeners being registered multiple times.

I have thought of the simplest solution: preventing duplicate registrations by using the name of the listener function passed as a parameter to the addPassiveEventListener function as a key.

It is important to note that you should pass a named function with a unique name as the listener parameter, rather than an anonymous function.

Fix for #451


  • As-is
before

  • To-be
after

@fisshy
Copy link
Owner

fisshy commented May 12, 2023

Thanks for your PR!

Why do we need to enforce a named function?

if (!listener.name) throw new Error('Listener must be a named function.');

Using this will break functionality for current users, we should probably just console.warn instead?

@seungdeng17
Copy link
Contributor Author

This is because we prevent duplicate event listener registration by using the name of the named function as a key.

if (!listener.name) throw new Error('Listener must be a named function.');

This code isn't necessary, but was added as a defensive measure. It doesn't seem to cause any issues in operation. If you are concerned, replacing it with the console.warn you suggested is a good idea.

I will add a commit 😀

@fisshy
Copy link
Owner

fisshy commented May 12, 2023

Maybe we should also fallback on eventName if listener.name is empty?

@seungdeng17
Copy link
Contributor Author

Using the eventName as a key seems like a reasonable alternative when an anonymous function is passed.

What do you think about this approach?

export const addPassiveEventListener = (target, eventName, listener) => {
  let listenerName = listener.name;
  if (!listenerName) {
    listenerName = eventName + new Date().getTime();
    console.warn('Listener must be a named function.');
  }
  ...
}

Since eventNames are usually not unique, I considered appending the return value from the getTime method.

By doing this, the same behavior as before is assured even if this PR is merged.

@fisshy
Copy link
Owner

fisshy commented May 15, 2023

Do we really need to append more events with getTime?

Couldn't we just replace the old event with the new, given same name?

Also removeEventListener wouldnt work with getTime

@seungdeng17
Copy link
Contributor Author

I've written it as defensively as possible.

If it's sufficient to replace an existing event with a new one, it's fine to just use eventName.

@fisshy
Copy link
Owner

fisshy commented May 15, 2023

Looks good to me!

@fisshy fisshy merged commit 236ddf0 into fisshy:master May 15, 2023
@nsunga
Copy link

nsunga commented Nov 7, 2023

@seungdeng17 hello 👋

Sorry to bother - this is also my first time digging into the source code for this package

But ever since upgrading to this commit, every time scrollTo is called in scroller.js, I keep getting the console.warn noise
console.warn('Listener must be a named function.');

Do you have any pointers to stop these console warns? I'll start digging into the call stack but I figured I'd ask here in this change if you know the correct way around this

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.

4 participants