-
Notifications
You must be signed in to change notification settings - Fork 481
Closed
Labels
Description
In the initial release of mitt a caller can register one handler multiple times for an event. For some use cases, though, duplicate registrations can be problematic. At the moment, mitt does not provide any APIs to avoid duplicate registration.
I see two straight forward ways of allowing consumers to avoid duplicate registration:
- Extend
on()to take a dedupe parameter. When truthy, mitt would not register the handler if it's already present onall. - Create a
has()method that returns a Boolean indicating if a given handler has been registered for an event. Callers concerned about duplicate handlers can manually check before callingon().
Between the two I feel like 1 provide a more ergonomic API and would likely help keep byte count down.
Prior art
- Element.addEventListener silently drops duplicate listener registrations.
- EventEmitter.on states "[m]ultiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times."