Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Register listener to all events #23

Open
rahatarmanahmed opened this issue Mar 24, 2016 · 3 comments
Open

Register listener to all events #23

rahatarmanahmed opened this issue Mar 24, 2016 · 3 comments

Comments

@rahatarmanahmed
Copy link

I figure I could make another package myself or monkeypatch for my own needs, but I wanted to see if anyone was open to the idea of adding this to Emitter.

It would be nice if Emitter had a onAny method that worked the same as on, but listeners registered would be triggered when any event is emitted.

The only issue I can see with this is that listeners could not be called in the order they were subscribed (i.e., all onAny listeners will be invoked before the on listeners). I don't think Emitter makes any guarantees about callback order, but it's understandable if this is too much of a surprise for users of Emitter.

@rahatarmanahmed
Copy link
Author

Oh, another issue I can think of is providing the event name to onAny listeners.

One solution is to make the callback signature for onAny listeners be (event, args...) ->. It's a bit inconsistent, though.

@maxbrunsfeld
Copy link
Contributor

This seems like an unusual use case. Could you solve it using the existing Emitter API, just by calling emit multiple times?

class Person {
  onDidDoAnything (callback) {
    return this.emitter.on('did-anything', callback)
  }

  setName (name) {
    this.name = name
    this.emitter.emit('did-change-name', name)
    this.emitter.emit('did-anything')
  }

  setAge (age) {
    this.age = age
    this.emitter.emit('did-change-age', age)
    this.emitter.emit('did-anything')
  }
}

@rahatarmanahmed
Copy link
Author

rahatarmanahmed commented Aug 8, 2016

It is possible, but at that point I would just monkeypatch emit to trigger the did-anything event automatically to avoid having to manually do it every time I needed it. Adding .onAny only serves to avoid that monkeypatching.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants