Class: EventEmitter

EventEmitter

new mmir.tools.EventEmitter(thisArg, events)

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, the this context will be the EventEmitter instance. 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.

Methods

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.
Returns:
Type Description
boolean true if no listener is registered

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

Get a list of registered listeners for an event type.
Name Type Description
type string the event type
Returns:
Type Description
Array.<function()> a list of listeners or undefined, if there are none for the event type

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