Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] change how meta events work #6

Open
janhancic opened this issue Oct 8, 2014 · 4 comments
Open

[feature] change how meta events work #6

janhancic opened this issue Oct 8, 2014 · 4 comments

Comments

@janhancic
Copy link

There is a new pattern that is being used across different projects now, and we realised that it could be made simpler.

It's a form of sticky events, but not really. An example:

// this will send a new listener for "status-changed" event the current status
this.on(emitr.meta.AddListenerEvent, function(addEvent) {
    if (addEvent.event === 'status-changed') {
        addEvent.listener.call(addEvent.context, this._status);
    }
}.bind(this));

So after discussing with @dchambers and @bit-shifter we decided to add a new method to emitr: .onMetaEvent(metaEvent, eventName, callback, ctx). And deprecate the current .on(metaEvent, ... (which will internally invoke the new method for backwards compatibility).

If eventName is null emitr instance will be told of every new listener, otherwise it will only be told of the event it is interested in.

callback will have the following signature: callback(eventName, sendToListenerFunc).

sendToListenerFunc will be a function that will take care of invoking the listener with the correct context and so on.

From a user's perspective it will look like this:

function MyClass() {
  this._status = 42;
  this.onMetaEvent(emitr.meta.AddListenerEvent, 'status-changed', function(sendToListenerFunc) {
    sendToListenerFunc(this._status);
  }, this);
}
Emitr.mixInto(MyClass);

I'm just capturing what we discussed here, I'll make a PR as soon as I have some time :)

@dchambers
Copy link
Contributor

Hey @janhancic, was there ever any progress on this?

@janhancic
Copy link
Author

No :( I haven't had the time ...

@dchambers
Copy link
Contributor

Okay, no probs.

@improbable-adam
Copy link

this seems like a specific instance of a more general concept - filtering event listeners based on some of their parameters. This functionality would be useful in normal events too, not just metaevents.

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

No branches or pull requests

3 participants