Skip to content

Add event as second parameter to listeners? #125

@ronen

Description

@ronen

Hi, it would be convenient (for me :) if the listener for on and once were passed the event, i.e. have the signature be listener(data, event). This would:

  • make it possible to for the listener to disambiguate the event when used with multiple events on([...events], listener)
  • similarly be convenient to use the same listener in multiple separate calls to on or once with different events
  • be non-breaking / backwards-compatible (unless there's some subtlety that I'm not thinking of?)

It looks like it'd be a fairly small change to the code plus a few extra tests.

An awkwardness is that the listener parameter order would be the opposite of onAny(listener), which could be confusing. One way around that could be to define a symbol such as Emittery.Any so that on(Emmittery.Any, listener) would be equivalent to onAny(listener), except using the signature of on listeners. Or instead of a symbol, could support on('*', listener) as a special case of possible future wildcard/namespace support (#31, #80) — but that would be a breaking change since '*' is currently a valid ordinary event name.

There are workarounds of course — include the event name in the data, or write the generic listener and curry the event name into event-specific listeners — but they're a little more cumbersome.

What do you think? If you like the idea I could put together a PR when I get a chance.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions