Skip to main content

Listenable Interface

An object which allows the registration of listeners so that subscribers can be notified when an event happens.

Sealed

This type is "sealed," meaning that code outside of the library defining it should not implement or extend it. Future versions of this type may add members or make typing of readonly members more specific.

Signature

/** @sealed */
export interface Listenable<TListeners extends object>

Type Parameters

ParameterConstraintDescription
TListenersobject

Methods

MethodReturn TypeDescription
off(eventName, listener)voidDeregister an event listener.
on(eventName, listener)OffRegister an event listener.

Method Details

off

Deregister an event listener.

Signature

off<K extends keyof Listeners<TListeners>>(eventName: K, listener: TListeners[K]): void;
Type Parameters
ParameterConstraintDescription
Kkeyof Listeners<TListeners>

Remarks

If listener is not currently registered, this method will have no effect.

Listeners may also be deregistered by calling the deregistration function returned when they are registered.

Parameters

ParameterTypeDescription
eventNameKThe name of the event.
listenerTListeners[K]The listener function to remove from the current set of event listeners.

on

Register an event listener.

Signature

on<K extends keyof Listeners<TListeners>>(eventName: K, listener: TListeners[K]): Off;
Type Parameters
ParameterConstraintDescription
Kkeyof Listeners<TListeners>

Remarks

Registering the exact same listener object for the same event more than once will throw an error. If registering the same listener for the same event multiple times is desired, consider using a wrapper function for the second subscription.

Parameters

ParameterTypeDescription
eventNameKThe name of the event.
listenerTListeners[K]The listener function to run when the event is fired.

Returns

A function which will deregister the listener when called. Calling the deregistration function more than once will have no effect.

Listeners may also be deregistered by passing the listener to off().

Return type: Off