Base class which provides default on/emit functions to child classes.

Events are specified by providing a type or interface as a type parameter. The type parameter should list the supported event names and the event value type (or void if there is no value).

interface MyEvents {
numberChanged: number; // event handlers will receive a number value
print: void; // event handlers will receive no value
}

class MyClass extends Evented<MyEvents> {
// ...
}

const object = new MyClass();
object.on("numberChanged", (value) => {
// value is a number
});
interface Evented<Events> {
    emit<Name extends string>(
        eventName: Name,
        ...value: Params<Events[Name]>,
    ): void;
    on<Name extends string>(
        eventName: Name,
        callback: EventCallback<Events[Name]>,
    ): EventHandle;
    on(
        eventName: "*",
        callback: EventCallback<Events[EventNames<Events>]>,
    ): EventHandle;
    on(
        eventName: string | string[],
        callback: EventCallback<unknown>,
    ): EventHandle;
}

Type Parameters

  • Events

Hierarchy (View Summary)

Methods

Methods

  • Emits an event. All associated event listeners registered for the given event name will be invoked asynchronously.

    Type Parameters

    • Name extends string

    Parameters

    • eventName: Name

      The name of the event.

    • ...value: Params<Events[Name]>

      The event value. Passed to all event listeners. Note: no value can be passed if the event type is void.

    Returns void

  • Register event listener for specific events.

    Type Parameters

    • Name extends string

    Parameters

    • eventName: Name

      name of the event

    • callback: EventCallback<Events[Name]>

      the event handler callback will be invoked when the event has been emitted

    Returns EventHandle

    a handle to unregister from the event

  • Register event listener for any event.

    Parameters

    Returns EventHandle

    a handle to unregister from the events

  • Register event listener for the events listed in eventName. Comma separated event names are possible, for example "eventA,eventB".

    Parameters

    • eventName: string | string[]

      the event name or list of names

    • callback: EventCallback<unknown>

      the event handler callback

    Returns EventHandle

    a handle to unregister from the event(s)