• Listens for events on the specified event source and invokes the callback for each event.

    The source parameter can be a plain event source, a signal, or a reactive function that returns the event source.

    on returns a cleanup handle that should be used to stop listening for the event.

    Example:

    import { on } from "@conterra/reactivity-events";

    const view = ...;
    const handle = on(view.clicked, (event) => {
    console.log("Clicked at", event.x, event.y);
    });
    // Later, to clean up:
    handle.destroy();

    The event source parameter can be reactive. In the following example, we will always subscribe on the current view (and unsubscribe from the previous one):

    // model.getCurrentView() is implemented using signals
    const handle = on(() => model.getCurrentView().clicked, (event) => {
    console.log("Clicked at", event.x, event.y);
    });

    NOTE: This function will slightly defer executions of the given callback. In other words, the execution of callbacks does not happen immediately after an event was fired.

    If you need more control, take a look at onSync.

    Type Parameters

    • T

    Parameters

    Returns CleanupHandle