Constructor
new EventAggregator(sourcesopt)
When instantiating an aggregator, you can pass it a single EventEmitter, a collection of Emitters or nothing.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
sources |
Array.<EventEmitter> | EventEmitter |
<optional> |
[]
|
Methods
addSource(source)
Add a source source to the group.
Any listeners that are currently defined will be automatically added to this source.
Parameters:
Name | Type | Description |
---|---|---|
source |
EventEmitter |
destroy()
Calling this method removes all listeners from all sources. It should be called to avoid memory leaks when you're done with the aggregator.
eventsListenedTo() → {Array.<string>}
Provides a list of the events that the aggregator is listening for.
NB: This doesn't mean that there are necessarily any handlers attached to these events.
Returns:
- Type
- Array.<string>
onAll(eventName, listener)
Once all of the sources in this aggregator have emitted a eventName
event, the associated
listener
is triggered.
Like EventEmitter#on
, this will continue to fire until it is explicitly removed.
The listener
will receive an array of the arguments from each of the events that were emitted
from the aggregated sources. The array is in the order in which the sources were added to
this aggregator.
If a source has emitted an event multiple times, the listener will get the arguments from the first event.
Once the listener has been triggered, this aggregator is reset for this event.
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | |
listener |
function |
onAny(eventName, listener)
If any of the sources in this aggregator emits a eventName
event, trigger the associated
listener
.
Like EventEmitter#on
, this will continue to fire until it is explicitly removed.
The listener
will receive a reference to the emitter that emitted the event and an array
of the arguments that the event included.
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | |
listener |
function |
onceAll(eventName, listener)
Once all of the sources in this aggregator have emitted a eventName
event, the associated
listener
is triggered.
Like EventEmitter#once
, the listener will be removed for this event.
The listener
will receive an array of the arguments from each of the events that were emitted
from the aggregated sources. The array is in the order in which the sources were added to
aggregator.
If a source has emitted events multiple times, the listener will get the arguments from the first event.
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | |
listener |
function |
onceAny(eventName, listener)
If any of the sources in this aggregator emits a eventName
event, trigger the associated
listener
.
Like EventEmitter#once
, the listener will be removed once it has been triggered.
The listener
will receive a reference to the source that emitted the event and an array
of the arguments that the event included.
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | |
listener |
function |
removeListener(eventName, listenerToRemove)
Removes a listener from the aggregator (just like EventEmitter#removeListener)
Parameters:
Name | Type | Description |
---|---|---|
eventName |
string | The name of the event from which we're removing a listener |
listenerToRemove |
function | The listener that we wish to remove |
removeSource(source)
Removers a source from the group.
Any listeners attached to this source are also removed.
If any listeners are attached via onAll or onceAll, then removing this source may result in those listeners being fired. This will happen if the source that is being removed is the only one in the group that hadn't emitted an event.
Put another way, if all of the sources except this one had fired, then removing this one
fires the onAll
and onceAll
listeners.
Parameters:
Name | Type | Description |
---|---|---|
source |
EventEmitter |
reset(eventNameopt)
If there are listeners that are waiting for all of the sources in this aggregation to emit, then calling this method will reset the aggregator so that it's as if none of them have emitted.
If an eventName
is provided, this is limited to listeners for that event. Otherwise, calling
this resets all of the listeners.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
eventName |
string |
<optional> |