EventEmitter

util EventEmitter

EventEmitter implements the classic observer/observable pattern.

Note: this is heavily inspired by http://www.datchley.name/es6-eventemitter/

Constructor

new EventEmitter()

Source:
Example
let observable = new EventEmitter();
let uuid1 = observable.on('change', data => { console.log(data); });
observable.emit("change", { a: 1 });
observable.off("change", uuid1);
observable.emit("change", { a: 1 });

Methods

emit(name, data) → {boolean}

Source:

Emit an event with a given name and associated data.

Parameters:
Name Type Description
name String

the name of the event

data object

the data of the event

Returns:

true if at least one listener has been registered for that event, and false otherwise

Type
boolean

off(name, listener)

Source:

Remove the listener with the given uuid associated to the given event name.

Parameters:
Name Type Description
name String

the name of the event

listener module:util.EventEmitter~Listener

a listener called upon emission of the event

on(name, listener)

Source:

Register a new listener for events with the given name emitted by this instance.

Parameters:
Name Type Description
name String

the name of the event

listener module:util.EventEmitter~Listener

a listener called upon emission of the event

Returns:

string - the unique identifier associated with that (event, listener) pair (useful to remove the listener)

once(name, listener)

Source:

Register a new listener for the given event name, and remove it as soon as the event has been emitted.

Parameters:
Name Type Description
name String

the name of the event

listener module:util.EventEmitter~Listener

a listener called upon emission of the event

Returns:

string - the unique identifier associated with that (event, listener) pair (useful to remove the listener)