Skip to content

Deduping handler registration #6

@dotproto

Description

@dotproto

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:

  1. Extend on() to take a dedupe parameter. When truthy, mitt would not register the handler if it's already present on all.
  2. 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 calling on().

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."

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions