Mappedin JS - v6.14.0
    Preparing search index...

    Interface PubSub<EVENT_PAYLOAD, EVENT>

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

    interface PubSub<
        EVENT_PAYLOAD,
        EVENT extends keyof EVENT_PAYLOAD = keyof EVENT_PAYLOAD,
    > {
        get signal(): AbortSignal;
        off<EVENT_NAME extends string | number | symbol>(
            eventName: EVENT_NAME,
            fn: (
                payload: EVENT_PAYLOAD[EVENT_NAME] extends { data: null }
                    ? any[any]["data"]
                    : EVENT_PAYLOAD[EVENT_NAME],
            ) => void,
        ): void;
        on<EVENT_NAME extends string | number | symbol>(
            eventName: EVENT_NAME,
            fn: (
                payload: EVENT_PAYLOAD[EVENT_NAME] extends { data: null }
                    ? any[any]["data"]
                    : EVENT_PAYLOAD[EVENT_NAME],
            ) => void,
            options?: { signal?: AbortSignal },
        ): () => void;
        onAbort<T extends (...args: any[]) => void>(fn: T): void;
    }

    Type Parameters

    Hierarchy (View Summary)

    Index

    Accessors

    Methods

    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: EVENT_PAYLOAD[EVENT_NAME] extends { data: null }
                ? any[any]["data"]
                : EVENT_PAYLOAD[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: EVENT_PAYLOAD[EVENT_NAME] extends { data: null }
                ? any[any]["data"]
                : EVENT_PAYLOAD[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