DEV Community

Discussion on: Review. Event dispatcher based on the CustomEvent interface

 
shystruk profile image
Vasyl Stokolosa

You may have multiple listeners, but when you want to remove them you will remove only the last.

TARGET.removeEventListener(eventName, EVENTS[eventName]);

Good point here. Should think about that.

Thread Thread
 
theoutlander profile image
Nick Karnik • Edited

Don't just remove the last added. The user needs complete control.

Usually, this is how I would expect it to be used:

let listener = () => {...}
obj.addEventListener('event', listener)
obj.removeEventListener('event', listener)

Another variation is:

let listenerHandler = obj.addEventListener('event', () => {...})
obj.removeEventListener('event', listenerHandler)

Simply calling this should remove all listeners:

obj.removeEventListener('event')
Thread Thread
 
shystruk profile image
Vasyl Stokolosa

removeEventListener required a handler
developer.mozilla.org/en-US/docs/W...
so, you have to call remove for all listeners and pass the handler function