Skip to content

[feature] change how meta events work #6

@janhancic

Description

@janhancic

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions