Mixin: EventEmitterMixin

EventEmitterMixin

API for events handling.

Methods


<static> emit(type [, params])

Call all handlers for the specified event type.

If value of type parameter is an object it will be passed in each handler as is.
If value of type parameter is a string an object of EventData type will be passed in each handler.

Parameters:
Name Type Argument Description
type Object | string

Type (name) of event or event object with type field for which to call handlers.

params any <optional>
<repeatable>

Any values that should be available in handlers. Will be used only when type parameter is string.

Returns:

this.

Type
module:neva~EventEmitter
Example
const emitter = getEmitter();
...
emitter.on('some-event', (event) => {
     console.log('Emitted value: ', event.data);
});
...
emitter.emit('some-event', 'payload')
       .emit('another-event');
...
emitter.emit({type: 'eventName', value: {a: 5}});

<static> hasEventHandler( [type] [, handler] [, context])

Check whether the specified event handler or any event handler is registered.

Parameters:
Name Type Argument Description
type string <optional>

Type (name) of event to check for. If type is not passed it means that existence of any handler for any type should be checked.

handler function <optional>

Handler that should be checked for the given event type. If handler is not passed it means that existence of any handler for the given type should be checked.

context Object <optional>

Context object for the event handler to check alongside.

Returns:

true if handler is registered, otherwise false.

Type
boolean
Example
const emitter = getEmitter();
...
emitter
     .on('event1', handler1)
     .on('event2', obj.handler, obj);
...
emitter.hasEventHandler('event1', handler1);   // true
emitter.hasEventHandler('event2', obj.handler, obj);   // true
emitter.hasEventHandler('event2', handler1);   // false
emitter.hasEventHandler('event2', obj2.handler, obj2);   // false

emitter.hasEventHandler('event1');   // true
emitter.hasEventHandler('event2');   // true
emitter.hasEventHandler('event3');   // false

emitter.hasEventHandler();   // true

<static> off( [type] [, handler] [, context])

Remove the specified event handler or all handlers for given type or for all types.

Parameters:
Name Type Argument Description
type string <optional>

Type (name) of event for which to remove handler(s). If type is not passed then all handlers for all types will be removed.

handler function <optional>

Event handler that should be removed. If handler is not passed then all handlers for given type will be removed.

context Object <optional>

Context object for event handler that should be removed.

Returns:

this.

Type
module:neva~EventEmitter
Example
const emitter = getEmitter();
...
emitter
     .on('event1', handler1)
     .on('event2', obj.handler, obj)
     .on('event3', handler3)
     .on('event1', handler4);
...
// Remove specific handler
emitter.off('event2', obj.handler, obj);
// Remove all handlers for event1
emitter.off('event1');
// Remove all handlers for all events
emitter.off();

<static> on(type, handler [, context] [, settings])

Register a handler for the specified event type(s).

Parameters:
Name Type Argument Description
type string | Array.<string>

Type (name) of event or list of types to listen for.

handler function

Function that should be called in response to specified event.

context Object <optional>

An object that should be used as this when calling the event handler. By default null is used.

settings module:neva~HandlerSettings <optional>

Settings for the event handler.

Returns:

this.

Type
module:neva~EventEmitter
Example
const emitter = getEmitter();
...
emitter
     .on('open', onceHandler, null, {once: true})
     .on('occasion', (event) => {
         ...
     })
     .on('close', obj.handler, obj);
...
emitter.on(['event1', 'event2'], eventHandler);