Generic PubSub class implementing the Publish-Subscribe pattern for event handling.

Type Parameters

  • T

    The type of the event payload.

Hierarchy (View Summary)

  • PubSub<T>
    • __INTERNAL__MapLibreOverlay

Implements

  • IControl

Constructors

Properties

Accessors

Methods

Constructors

  • Type Parameters

    • T

    Parameters

    • origin: undefined | Position
    • Optionaloptions: Partial<
          {
              __EXPERIMENTAL_shadows?: ShadowsOptions;
              antialias?: boolean;
              attribution?: AttributionControlOptions;
              backgroundAlpha?: number;
              backgroundColor?: string;
              bearing?: number;
              center?: Position;
              environment?: EnvMapOptions;
              gl?: WebGLRenderingContext;
              imagePlacementOptions?: ImagePlacementOptions;
              map?: Map$1;
              mode?: "standalone"
              | "outdoors-interleaved"
              | "outdoors-overlay";
              naturalBearing?: number;
              occlusionEnabled?: boolean;
              onWebGLContextCreationError?: (event: Event) => void;
              onWebGLContextLost?: (event: Event) => void;
              onWebGLContextRestored?: (event: Event) => void;
              onWebGLRendererError?: (error: Error) => void;
              outdoorView: {
                  enabled: boolean;
                  headers: { "x-mappedin-tiles-key": string };
                  lowDpi?: boolean;
                  style?: string;
              };
              outlines?: boolean
              | OutlinesOptions;
              pitch?: number;
              transformImageRequest?: TransformImageRequest;
              useCollisionWorker?: boolean;
              useStandaloneCamera?: boolean;
              useWorkers?: boolean;
              watermark?: WatermarkOptions;
              zoomLevel?: number;
          },
      >

    Returns __INTERNAL__MapLibreOverlay<T>

Properties

core: Core
el: HTMLDivElement
layer?: CustomLayerInterface
map?: Map$1
origin?: Position

Accessors

  • get signal(): AbortSignal

    Returns the AbortSignal for this PubSub instance. Use this signal with APIs that support cancellation (fetch, addEventListener, etc.) When the PubSub is destroyed, the signal will be aborted and all listeners using it will be automatically removed.

    Returns AbortSignal

    // Automatically cleaned up when PubSub is destroyed
    pubsub.addEventListener(window, 'resize', handler, { signal: pubsub.signal });

Methods

  • Unsubscribe a function previously subscribed with on

    Type Parameters

    • EVENT_NAME extends string | number | symbol

    Parameters

    • eventName: EVENT_NAME

      An event name to which the provided function was previously subscribed.

    • fn: (
          payload: T[EVENT_NAME] extends { data: null }
              ? any[any]["data"]
              : T[EVENT_NAME],
      ) => void

      A function that was previously passed to on. The function must have the same reference as the function that was subscribed.

    Returns void

    // Unsubscribe from the 'click' event
    const handler = (event) => {
    console.log('Map was clicked', event);
    };
    map.off('click', handler);
  • Subscribe a function to an event.

    Type Parameters

    • EVENT_NAME extends string | number | symbol

    Parameters

    • eventName: EVENT_NAME

      An event name which, when fired, will call the provided function.

    • fn: (
          payload: T[EVENT_NAME] extends { data: null }
              ? any[any]["data"]
              : T[EVENT_NAME],
      ) => void

      A callback that gets called when the corresponding event is fired. The callback will get passed an argument with a type that's one of event payloads.

    • Optionaloptions: { signal?: AbortSignal }

      Optional options object. If a signal is provided, the subscription will be automatically cleaned up when that signal is aborted.

    Returns () => void

    A cleanup function that unsubscribes the event listener when called.

    // Subscribe to the 'click' event
    const handler = (event) => {
    const { coordinate } = event;
    const { latitude, longitude } = coordinate;
    console.log(`Map was clicked at ${latitude}, ${longitude}`);
    };
    map.on('click', handler);
  • Subscribe a function to be called when the signal is aborted.

    Type Parameters

    • T extends (...args: any[]) => void

    Parameters

    • fn: T

      The function to call when the signal is aborted.

    Returns void

  • Register a control on the map and give it a chance to register event listeners and resources. This method is called by Map#addControl internally.

    Parameters

    • map: Map$1

      the Map this control will be added to

    Returns HTMLDivElement

    The control's container element. This should be created by the control and returned by onAdd without being attached to the DOM: the map will insert the control's element into the DOM as necessary.

  • Unregister a control on the map and give it a chance to detach event listeners and resources. This method is called by Map#removeControl internally.

    Returns void