new EventEmitter()
EventEmitter implements the classic observer/observable pattern.
Note: this is heavily inspired by http://www.datchley.name/es6-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}
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 |
- Source:
Returns:
true if at least one listener has been registered for that event, and false otherwise
- Type
- boolean
off(name, listener)
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 |
- Source:
on(name, listener)
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 |
- Source:
Returns:
string - the unique identifier associated with that (event, listener) pair (useful to remove the listener)
once(name, listener)
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 |
- Source:
Returns:
string - the unique identifier associated with that (event, listener) pair (useful to remove the listener)
Type Definitions
Listener(data)
Listener called when this instance emits an event for which it is registered.
Parameters:
Name | Type | Description |
---|---|---|
data |
object | the data passed to the listener |
- Source: