sindresorhus/emittery

Support multiple events in `on()` #1

sindresorhus posted onGitHub

I considered adding support for multiple events in on(), but wanted to wait to see if this is something people actually need.

Relevant Node.js issue: https://github.com/nodejs/node/issues/15697

// @silverwind


@sindresorhus :wave: we've implemented support for multiple events in Probot's "event emitter", it's definitely been useful. We just test for an Array as the eventName then call .on() for each item.

posted by JasonEtco over 7 years ago

I'm actually a fan of space-separated event names like in jQuery' .on, but I guess Array is fine too.

posted by silverwind over 7 years ago

I'd prefer to be explicit and use arrays containing strings. #12 is a problem since technically, at the moment, event names can be arrays.

posted by novemberborn over 7 years ago

It would be useful to change the way to pass the event name so that the listener signature does not change if it starts to listener to multiple events.

it could use the context but it rules out the use of arrow function:

interface Event {
  name: string;
}

class Emittery {
  on(name: string|string[], listener: (this: Event, data: any) => any): () => void {}
  onAny(listener: (this: Event, data: any) => any): () => void {}
}

Or the data and the event could be sent together:

interface Event {
  name: string;
  data: any;
}

class Emittery {
  on(name: string|string[], listener: (ev: Event) => any): () => void {}
  onAny(listener: (ev: Event) => any): () => void {}
  // Would behave like `Promise.race` but would unsubscribe the waiting events
  once(name: string|string[]): Promise<Event>
}

Alternatively, there could be an extra method:

class Emittery {
  on(name: string, listener: (data: any) => any): () => void {}
  onMulti(name: string[], listener: (name: string, data: any) => any): () => void {}
  onAny(listener: (name: string, data: any) => any): () => void {}
}
posted by dinoboff over 7 years ago

@novemberborn Any opinion on the above? I'm leaning towards an additional method.

posted by sindresorhus about 7 years ago

I'm leaning towards an additional method.

๐Ÿ‘

posted by novemberborn about 7 years ago

An other option, closer to OP, would be to switch the argument order for multiple events:

class Emittery {
  on(name: string|string[], listener: (data: any, name: string) => any): () => void {}
  onAny(listener: (data: any, name: string) => any): () => void {}

  // maybe
  once(name: string|string[]): Promise<[any, string]>
}
posted by dinoboff about 7 years ago

@issuehunt has funded $40.00 to this issue. See it on IssueHunt

posted by IssueHuntBot about 6 years ago

@sindresorhus has rewarded $36.00 to @lukks. See it on IssueHunt

  • :moneybag: Total deposit: $40.00
  • :tada: Repository reward(0%): $0.00
  • :wrench: Service fee(10%): $4.00
posted by issuehunt-app[bot] almost 5 years ago

Fund this Issue

$40.00
Rewarded

Rewarded pull request

Other pull requests

Recent activities

lukks was rewarded by sindresorhus for sindresorhus/emittery# 1
almost 5 years ago
lukks submitted an output to  sindresorhus/ emittery# 1
about 5 years ago