TomTom Maps for JavaScript
    Preparing search index...

    Class ModuleEvents<TConfig, TShownFeatures>

    Lifecycle event module for map modules.

    It offers one API for subscribing to module-level events that aren't tied to user interaction with map features. It supports two event types:

    • config-change: emitted after every change to the module's configuration, which What triggers config-change lists.
    • shown-features: emitted by modules that have a show method, immediately after features are displayed on the map.

    Each on() call returns an unsubscribe function, so you can remove individual handlers without clearing the entire listener list.

    const unsubscribeConfig = trafficFlow.events.on('config-change', (config) => {
    console.log('New config:', config);
    });

    const unsubscribeShown = places.events.on('shown-features', (features) => {
    console.log('Shown:', features);
    });

    // Later:
    unsubscribeConfig();
    unsubscribeShown();

    Type Parameters

    • TConfig

      The module's configuration type.

    • TShownFeatures = never

      What the module hands its shown-features handlers. Use never for modules that have no show method.

    Index
    • Removes all handlers for the given event type at once.

      To remove a single handler, call the unsubscribe function that on returns instead, which is also how a component cleans up on unmount: off also removes the handlers other code put on the module, such as a plugin's.

      Parameters

      • type: "config-change" | ShownFeaturesType<TShownFeatures>

        The event type whose handlers should all be cleared. A module takes 'shown-features' only if it has a show method.

      Returns void

      // Clear all config-change listeners in one call
      trafficFlow.events.off('config-change');
    • Subscribes to config-change events.

      The handler receives the module's full current configuration right after every change to it, which What triggers config-change lists.

      Parameters

      • type: "config-change"

        'config-change'

      • handler: (config: TConfig | undefined) => void

        Receives the module's config, or undefined when the config is cleared.

      Returns () => void

      An unsubscribe function. Call it to remove only this handler, leaving the other handlers for the same event type registered.

      const unsubscribe = trafficFlow.events.on('config-change', (config) => {
      toggle.checked = config?.visible ?? false;
      });

      // Later:
      unsubscribe();
    • Subscribes to shown-features events.

      The handler is called right after a module's show() method, or its equivalent, updates the map with new data. Only modules that have a show method offer it.

      Parameters

      • type: ShownFeaturesType<TShownFeatures>

        'shown-features'

      • handler: (features: TShownFeatures) => void

        Receives what the module shows: the data passed to show(), or, from a module that shows several kinds, an object naming which, such as PlacesModule's { places } or { connections }.

      Returns () => void

      An unsubscribe function. Call it to remove only this handler, leaving the other handlers for the same event type registered.

      const unsubscribe = places.events.on('shown-features', (shown) => {
      if ('places' in shown) fitMapToResults(shown.places);
      });

      // Later:
      unsubscribe();