Skip to content

Class: TypedEventEmitter<T>

A strongly-typed Tiny Event Emitter.

Type Parameters

Type ParameterDefault type
T extends EventMap<T>DefaultEventMap

Constructors

Constructor

ts
new TypedEventEmitter<T>(): TypedEventEmitter<T>;

Creates a new TypedEventEmitter instance.

Returns

TypedEventEmitter<T>

Properties

PropertyModifierTypeDescription
epublicEventListenerMapThe internal event listener map for the emitter.

Methods

emit()

ts
emit<K>(event: Key<K, T>, ...args: Args<K, T>): this;

Emit an event.

Type Parameters

Type Parameter
K

Parameters

ParameterTypeDescription
eventKey<K, T>The event name.
...argsArgs<K, T>The arguments to pass to the listeners.

Returns

this

the current instance for chaining


off()

ts
off<K>(event: Key<K, T>, listener?: Listener<K, T>): this;

Unsubscribe from an event.

Type Parameters

Type Parameter
K

Parameters

ParameterTypeDescription
eventKey<K, T>The event name.
listener?Listener<K, T>Optional. The listener function to remove.

Returns

this

the current instance for chaining

Remarks

When no listener is provided, all listeners for the event will be removed, otherwise only the specified listener will be removed.


on()

ts
on<K>(
   event: Key<K, T>,
   listener: Listener<K, T>,
   ctx?: any): this;

Subscribe to an event with a typed listener.

Type Parameters

Type Parameter
K

Parameters

ParameterTypeDescription
eventKey<K, T>The event name.
listenerListener<K, T>The listener function.
ctx?anyThe this context to apply to the listener when it is called

Returns

this

the current instance for chaining


once()

ts
once<K>(
   event: Key<K, T>,
   listener: Listener<K, T>,
   ctx?: any): this;

Subscribe to an event once with a typed listener.

Type Parameters

Type Parameter
K

Parameters

ParameterTypeDescription
eventKey<K, T>The event name.
listenerListener<K, T>The listener function.
ctx?anyThe this context to apply to the listener when it is called

Returns

this

the current instance for chaining