Skip to main content

Listenable Interface

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

Signature

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

Type Parameters

Parameter Constraint Description
TListeners object

Methods

Method Return Type Description
off(eventName, listener) void Deregister an event listener.
on(eventName, listener) Off Register 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
Parameter Constraint Description
K keyof 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

Parameter Type Description
eventName K The name of the event.
listener TListeners[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
Parameter Constraint Description
K keyof 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

Parameter Type Description
eventName K The name of the event.
listener TListeners[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