-
Notifications
You must be signed in to change notification settings - Fork 0
EventEmitter
Instances of this class can collect listeners and emit events. An instance is either directly instantiated by calling new EventEmitter or by inheritance from other class. If other classes inherit this class, they must call the EventEmitter constructor.
If the EventEmitter is called without as function (without new operator), it'll return new instance of EventEmitter.
var require('lee').EventEmitter;
var emitter = new EventEmitter;
var emitter2 = new EventEmitter();
var emitter3 = EventEmitter();
function MyEmitter() {
EventEmitter.call(this);
}
MyEmitter.prototype = Object.create(EventEmitter.prototype);
var customEmitter = new MyEmitter();| Method(s) | Description |
|---|---|
| [[on | EventEmitter.addEventListener]], [[addListener |
| [[once | EventEmitter.once]] |
| [[off | EventEmitter.removeEventListener]], [[removeListener |
| [[hasListener | EventEmitter.hasListener]] |
| [[getSortedListenerList | EventEmitter.getSortedListenerList]] |
| [[getGroupedListenerList | EventEmitter.getGroupedListenerList]] |
| [[getListenerDependencyList | EventEmitter.getListenerDependencyList]] |
| [[emit | EventEmitter.emitParallel]], [[emitParallel |
| [[emitSeries | EventEmitter.emitSeries]] |
EventEmitter support three types of listeners.
- Listener added as dependency.
- Listener with self-remove (called once).
- Normal listener.
A listener is a function. Adding function twice have no effect unless it is promoted.
A listener can be promoted after they have been added. However listeners cannot be demoted. Here is the order of promotion:
Let have following example:
var emitter = new EventEmitter();
var f = function() {}, g = function() {};
emitter.on("my-event", f, [g]); //1
emitter.once("my-event", g); //2Both functions f and g will be added as listeners for "my-event", but after (1) f is added explicitly, while g is added as dependency of f. Removing f will also remove g (unless other listener functions for this event depend on g).
After (2) however g is promoted to a listener with self-removal. If won't be removed if f is removed.
Let have following example:
var emitter = new EventEmitter();
var f = function() {};
emitter.once("my-event", f); //1
emitter.on("my-event", f); //2After (1) f is added as listener with self-removal. However after (2) f is promoted to normal listener and it won't be removed after the call.
Normal listeners stays until they are removed or EventEmitter instance has been garbage collected.
