Skip to main content
Version: Version 5

Class: EventEmitter<T>

EventEmitter is a class designed for managing and dispatching events.

Extended by​

Type Parameters​

T​

T extends Record<keyof T, (…args) => void>

Interface which describes the event names and their associated callback types.

Constructors​

Constructor​

new EventEmitter<T>(): EventEmitter<T>

Returns​

EventEmitter<T>

Methods​

addEventListener()​

addEventListener<K>(type, listener): void

Register a function that will be called whenever the specified event is delivered.

Type Parameters​

K​

K extends string | number | symbol

A generic type representing the key of the event type.

Parameters​

type​

K

String representing the event type to listen for.

listener​

(…e) => void

The function that will be executed when an event of the specified type occurs.

Returns​

void


dispatchEvent()​

dispatchEvent<K>(type, args): void

Dispatches an event to all registered listeners.

Type Parameters​

K​

K extends string | number | symbol

A generic type representing the key of the event type.

Parameters​

type​

K

String representing the event type to dispatch.

args​

Parameters<T[K]>

The data associated with the event.

Returns​

void


removeAllListeners()​

removeAllListeners<K>(type): void

Remove all listeners for a given event.

Type Parameters​

K​

K extends string | number | symbol

A generic type representing the key of the event type.

Parameters​

type​

K

String representing the event for which to remove all listeners.

Returns​

void


removeEventListener()​

removeEventListener<K>(type, listener): void

Removes an event listener previously registered with addEventListener.

Type Parameters​

K​

K extends string | number | symbol

A generic type representing the key of the event type.

Parameters​

type​

K

String representing the event for which to remove an event listener.

listener​

(…e) => void

The event listener function of the event handler to remove from the event target.

Returns​

void