Go to repository

Class EventEmitter<L>

The EventEmitter class is a minimal observer pattern class definition. Instances of this class can be created directly, by new EventEmitter() or it can be used as a base class for custom definitions that require support for this pattern in order to allow subscription to custom events.

The EventEmitter accepts as a type as a EventSignature, that is, a set of event names together with the expected parameters that the observer will be called with when the event occurs.

Type Parameters

Constructors

Type Parameters

Returns EventEmitter<L>

Properties

Private
eventObservers: Map<keyof L, Set<L[keyof L]>> = ...

A map of event to observers for the full time observers.

Private
eventOnceObservers: Map<keyof L, Set<L[keyof L]>> = ...

A map of event to observers for the one time observers.

Methods

Remove all observers to a given event. That is, no observers will be called when the event is emitted. Wether the observers were one time observers or full time observers is irrelevant, they are removed anyhow.

Event if all observers are removed, an observer may subscribe again to the event afterwards.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event that the observers will be removed from.

Returns this

The event emitter.

Private

Add a particular observer to an event in the given map. If the event does not exists in the map, create it. If it does, add only the new observer. If the observer is already present for that event in the given map, do nothing.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event to add the observer to.

  • observer: L[U]

    The observer to add.

  • map: Map<U, Set<L[U]>>

    The map to register the event and observer.

Returns void

Private

Remove all entries for an event in the given map. That is, remove the event itself.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event to remove.

  • map: Map<U, Set<L[U]>>

    The map to remove the event from.

Returns void

Emit a particular event, calling all the observers that were registered to such event in the process, wether they were one time subscribers or full time subscribers.

After the emission, one time subscribers are removed as subscribers to the event, as they should not be called again.

Type Parameters

  • U extends string | number | symbol

Parameters

Returns void

Private

Return a set for the given event. If the event exists in the given map, return the set of that event. If not, return a new set.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event to obtain the set for.

  • map: Map<U, Set<L[U]>>

    The map from where to grab the set from.

Returns Set<L[U]>

A set for the given event, a present one, or a new one.

Remove an observer from a particular event of this event emitter. That is make the observer not to be called on future emits. If the observer is registered as a one time observer or as a full time observer is not relevant, it's removed anyhow. If an observer that is not registered is given, nothing happens, and no error is thrown.

Note that the observer is removed only to the specified event, and such it may still be registered for other events.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event that the observer will be removed from.

  • observer: L[U]

    The observer that will be removed when the event occurs.

Returns this

The event emitter.

Register a observer to a particular event of this event emitter, as a full time observer, that is, it will be called on the next emit of the event, and on all the following emits of such event.

If the observer is already present as a full time observer, it will remain as a full time observer. If it's a one time observer, it will be transformed to a full time observer.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event that the observer will be registered to.

  • observer: L[U]

    The observer that will be called when the event occurs.

Returns this

The event emitter.

Register a observer to a particular event of this event emitter, as a one time observer, that is, it will be called on the next emit of the event, but not on the following emits of such event.

If the observer is already present as a one time observer, it will remain as a one time observer. If it's a full time observer, it will be transformed to a one time observer.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event that the observer will be registered to.

  • observer: L[U]

    The observer that will be called when the event occurs.

Returns this

The event emitter.

Private

Remove a particular observer from an event in the given map. If the observer does not exists in the event for the map, or the event is not present in the map, do nothing.

Type Parameters

  • U extends string | number | symbol

Parameters

  • event: U

    The event to remove the observer from.

  • observer: L[U]

    The observer to be removed.

  • map: Map<U, Set<L[U]>>

    The map to unregister the event and observer.

Returns void