Dedupe event listeners
See original GitHub issueCurrently, each instead of Perimeter
will register a mousemove
and resize
listener on window
. While I don’t recommend having tons of these on a single page, it may be better for performance to register a single listener and iterate over each registered instance. So we need to:
- Determine if its more performant to use a single listener (maybe browsers can parallelize multiple listeners better?)
- Find a universal way for each instance to register with a global listener. The easiest solution may be to track a property on the global
window
but other suggestions welcome
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (11 by maintainers)
Top Results From Across the Web
Removing duplicate event listeners - javascript
I made a function that will add an event listener to a button but if the function runs again I want the event...
Read more >How to prevent duplicate events - kiko.io
I'm working on a new web app that contains a sliding out panel with some additional information on the selected element.
Read more >Javascript addEventListener will not duplicate named functions
Let's take the classic use of the anonymous function as an event listener. If we bind the same content multiple times to the...
Read more >Drop most custom event listeners · Issue #5992
Reasons: deduplication logic is making things difficult; a simple selector observer is usually much easier to reason about. Related: MAIN AJAX ...
Read more >Deduplicate events to reduce noise
Deduplication is the process of identifying repeated events and combining them into alerts. This reduces operational noise by limiting the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I agree that it’s quite a bit of moving parts if you want to make it conditional and let people pass their own register & remove listeners functions. (I like the customizability aspect but I’m not sure if it’s necessary, we should just come up with the 90 percentile best solution and go with that.)
In my proposed solution we could handle the “global vs non-global” problem very simply by the way:
and then if we wanted to go super deep we could still do the eventManager as a potential prop to (think history in React-Router)
EDIT: Also first we should experiment and prove that it’s actually beneficial to have 1 global listener rather than a bunch of listeners. (I’m confident it is, but need to test what impact it makes anyways.)
Feels like a good place to use a MouseMoveProvider component wrapping the application (like the Provider from react-redux) and providing a subscription method to move events on the context -> the Perimeter component on componentWillMount subscribes to the MouseMoveProvider via the exposed context method and unsubscribes on componentWillUnmount
I feel like that should work?
Sorry for the terrible formatting, I’m on my phone. Happy to submit a PR once I get to work that implements this.