API for events handling.
Methods
-
<static> emit(type [, params])
-
Call all handlers for the specified event type.
If value of
typeparameter is an object it will be passed in each handler as is.
If value oftypeparameter is a string an object of EventData type will be passed in each handler.Parameters:
Name Type Argument Description typeObject | string Type (name) of event or event object with
typefield for which to call handlers.paramsany <optional>
<repeatable>
Any values that should be available in handlers. Will be used only when
typeparameter is string.Returns:
this.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 typestring <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.
handlerfunction <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.
contextObject <optional>
Context object for the event handler to check alongside.
Returns:
trueif handler is registered, otherwisefalse.- 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 typestring <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.
handlerfunction <optional>
Event handler that should be removed. If handler is not passed then all handlers for given type will be removed.
contextObject <optional>
Context object for event handler that should be removed.
Returns:
this.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 typestring | Array.<string> Type (name) of event or list of types to listen for.
handlerfunction Function that should be called in response to specified event.
contextObject <optional>
An object that should be used as
thiswhen calling the event handler. By defaultnullis used.settingsmodule:neva~HandlerSettings <optional>
Settings for the event handler.
Returns:
this.Example
const emitter = getEmitter(); ... emitter .on('open', onceHandler, null, {once: true}) .on('occasion', (event) => { ... }) .on('close', obj.handler, obj); ... emitter.on(['event1', 'event2'], eventHandler);