Class: EventEmitter

EventEmitter

new mmir.tools.EventEmitter(thisArg, events, enableHooks)

Inspired by tiny-events
MIT License (MIT), Copyright (c) 2014 Björn Brauer
Simple Event Emitter / Manager. Optionally, a list of supported events can be specified: if a list of supported events is given, trying the register a listener for an unsupported event will cause an error.
Name Type Description
thisArg any optional OPTIONAL this context when emitting events: if omitted (i.e undefined), the this context will be the EventEmitter instance. If null the context will be the global context. Any other value will be taken as-is. WARNING: if thisArg is an Array, then events argument must also be given, either as an Array, or false for disabling the supported events restriction
events Array.<string> optional OPTIONAL A list of supported event types/names: if calling EventEmitter.on() with an unsupported type, an error will be thrown. If omitted, any event type/names is allowed.
enableHooks Boolean optional OPTIONAL If true, upon emitting events, existing handler functions on the thisArg with name "on{lower-case event name}" will be triggered, after registered listeners were triggered. NOTE: if enabled, the thisArg argument must be undefined or a valid context (must not be null). NOTE: should not be used in combination with mmir.extensions.EventEmitter#createEventHandlerProperty

Methods

Release all resources of EventEmitter instance: unregister any listeners and unlink any external references (e.g. thisArg if given in constructor).

emit(type, args){boolean}

Emit an event to registered listeners.
Name Type Description
type string the event type that will be emitted
args any optional repeatable OPTIONAL arguments that will be emitted to the listeners
Returns:
Type Description
boolean true if at least one listener was notified

empty(){boolean}

Check if the EventEmitter instance has any listener registered. NOTE: only considers hooks, if hooks are enabled and a list of valid events was specified (see constructor).
Returns:
Type Description
boolean true if no listener is registered

get(type){Array.<function()>|Array.<String>}

Get a list of event types (if no argument is given) or a list of registered listeners for an event type (if event type is given).
Name Type Description
type string optional the event type. If omitted returns a list of event types, or undefined, if there are no listener for any event registered. If an event type is specified, returns a list of listeners, or undefined, if no listeners for the event type or registered.
Returns:
Type Description
Array.<function()> | Array.<String> a list of listeners or a list of event types or undefined, if there are none.

has(type, listener){boolean}

Check if listener is registered for an event.
Name Type Description
type string the event type
listener function optional OPTIONAL The listener to be checked. If omitted, it will be checked, if there is any listener of the event type.
Returns:
Type Description
boolean true if the listener (or any if listener was not specified) is registered for the event type

off(type, listener){boolean}

Unregister a listener for an event.
Name Type Description
type string the event type
listener function optional OPTIONAL The listener to be removed. If omitted, all listeners for the event type will be removed.
Returns:
Type Description
boolean true if the listener was removed

on(type, listener){boolean}

Register a listener for an event type. Will be ignored, if the listener already is registered for that event type. If a list of supported event types is specified for the EventEmitter instance, and type is not a supported type, an error will be thrown.
Name Type Description
type string the event type
listener function this listener that will be registered
Throws:
Error if the event type is not supported by the EventEmitter instance
Returns:
Type Description
boolean true if the listener was registered

once(type, listener){boolean}

Register a listener for an event type to be called one time.
Name Type Description
type string the event type
listener function this listener that will be registered
See:
Throws:
Error if the event type is not supported by the EventEmitter instance
Returns:
Type Description
boolean true if the listener was registered
Verifies that an event type can be used with the EventEmitter instance (see events argument in constructor).
Name Type Description
type string the event type to check
Throws:
Error if the event type is not supported by the EventEmitter instance