EventAggregator

EventAggregator

The EventAggregator provides a way to bring EventEmitters together into a group and to interact with them as a collection (rather than individuals)

Constructor

new EventAggregator(sourcesopt)

Source:

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)

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()

Source:

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>}

Source:

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)

Source:

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)

Source:

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)

Source:

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)

Source:

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)

Source:

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)

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)

Source:

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>