Skip to content

Latest commit

 

History

History
82 lines (57 loc) · 1.92 KB

api.md

File metadata and controls

82 lines (57 loc) · 1.92 KB

SuperEventEmitter API

on( name: string, fn: Function, ctx?: Object )

  • name - a string value representing the name of event
  • fn - action which will be called when event will be triggered
  • ctx - object will be context of triggered action

Example:

instance.on('foo', function (payload) {
    console.log(payload, this);
});

instance.on('foo', function (payload) {
    console.log(payload, this);
}, instance);

Special behavior:

  • when name = all (or *) register handler will be fired on any event
instance.on('all', function (name, payload) {
    // will be fired, when emit any of event type
    // - name - event name, in this example will be equal: "something"
    // - payload - data which are sended, in this example will be equal: { foo: 1 }
});

instance.emit('something', { foo: 1 });

once( name: string, fn: Function, ctx?: Object )

The same as on but, after triggered event, destroy all listeners

Example:

instance.once('foo', function (payload) {
    console.log(payload, this);
});

instance.once('foo', function (payload) {
    console.log(payload, this);
}, instance);

off( name?: string, fn?: Function )

  • name - a string value representing the name of event
  • fn - action which will be removed from listeners

Example:

instance.off();
instance.off('foo');
instance.off('foo', fooHandler);

emit( name: string, payload?: Object )

  • name - a string value representing the name of event
  • payload - will be passed as first argument in called actions

Example:

instance.emit('foo');
instance.emit('foo', { name: 'bar' });

Backward compatibility

For compatibility with any other APIs I was added some aliases:

  • on => addEventListener, addListener, bind
  • off => removeEventListener, removeListener, unbind
  • emit => dispatchEventListener, dispatchListener, trigger