Class PubSub<EVENT_PAYLOAD, EVENT>

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

Type Parameters

Hierarchy (view full)

Constructors

Methods

Constructors

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.

    Returns void

    // 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);