Firemitt - v0.1.9
    Preparing search index...

    Class EventHelper

    Helper class for managing custom events across different windows or iframes.

    This class provides static methods to initialize the event system, send encoded messages, and listen for specific event types. It uses the Base64helper class for encoding and decoding messages.

    Index
    • Removes all active message listeners registered by this helper. Called before each new session to prevent stale handlers from a previous session from intercepting events meant for the new one.

      Returns void

    • Initializes the EventHelper with a target window.

      This method sets the target window where the messages will be posted to. Any listeners registered during a previous session are removed first. The calling page's window is captured as the host window for message listeners. When no target is given, nothing is changed and the current session is left untouched.

      Parameters

      • target: TUnsafe<Window>

        The target window to which messages will be sent.

      Returns boolean

      Returns true if the target is successfully set, otherwise false.

    • Sets up a one-time event listener for a specific event type.

      This method listens for messages of the specified event type and executes the provided function the first time such an event occurs. Untrusted events, events that are not valid encoded messages, and (when options.source is not undefined) events coming from any other source are ignored.

      Type Parameters

      • T = unknown

        The expected type of data in the event message.

      Parameters

      • type: EventType

        The type of event to listen for.

      • func: (data?: T, event?: MessageEvent<any>) => void

        The function to execute when the event is received. It receives the event data and the raw message event.

      • Optionaloptions: TListenOptions = {}

        Optional listener options.

      Returns typeof EventHelper

      Returns the EventHelper class for method chaining.

    • Sends a message to the target window.

      This method encodes a message of the specified event type and data, then posts it to the target window. It does nothing when EventHelper.init was never called successfully.

      Type Parameters

      • T extends object = object

        The type of data being sent.

      Parameters

      • type: EventType

        The type of event.

      • Optionaldata: T

        Optional data to be included in the event message.

      • OptionaltargetOrigin: string = "*"

        The origin the target window must have for the message to be delivered. Defaults to "*".

      Returns typeof EventHelper

      Returns the EventHelper class for method chaining.