export type handler = (payload: any, event: MessageEvent) => any; export type TinyIframeEventsConfig = { /** * - The target iframe element to post messages to. Required if instantiated in the parent window. */ targetIframe?: HTMLIFrameElement; /** * - The target origin to restrict messages to. Defaults to `window.location.origin`. */ targetOrigin?: string; /** * - Custom internal name used to validate standard routing messages. */ secretEventName?: string; /** * - Custom internal name used for the initial MessageChannel handshake. */ handshakeEventName?: string; /** * - Custom internal name used for the ready event trigger. */ readyEventName?: string; }; export type IframeEventBase = { /** * - Dynamic key based on secretEventName to validate the message. */ secretIndicator?: boolean; /** * - The name of the custom event route. */ eventName: string; /** * - The data being sent (can be any type). */ payload: any; /** * - Indicates the sender: 'iframe' or 'parent'. */ direction: 'iframe' | 'parent'; }; export type TinyIframeEventsMap = Record<`win:${string}`, (payload: any, event: MessageEvent) => void> & Record void>; /** * @callback handler * A function to handle incoming event payloads. * @param {any} payload - The data sent by the emitter. * @param {MessageEvent} event - Metadata about the message. */ /** * @typedef {object} TinyIframeEventsConfig * Configuration object for initializing TinyIframeEvents. * @property {HTMLIFrameElement} [targetIframe] - The target iframe element to post messages to. Required if instantiated in the parent window. * @property {string} [targetOrigin] - The target origin to restrict messages to. Defaults to `window.location.origin`. * @property {string} [secretEventName] - Custom internal name used to validate standard routing messages. * @property {string} [handshakeEventName] - Custom internal name used for the initial MessageChannel handshake. * @property {string} [readyEventName] - Custom internal name used for the ready event trigger. */ /** * @typedef {object} IframeEventBase * Internal message structure for routed communication. * @property {boolean} [secretIndicator] - Dynamic key based on secretEventName to validate the message. * @property {string} eventName - The name of the custom event route. * @property {any} payload - The data being sent (can be any type). * @property {'iframe' | 'parent'} direction - Indicates the sender: 'iframe' or 'parent'. */ /** * Event map for {@link TinyIframeEvents}. * All routed events are prefixed with `win:` followed by the event name. * @typedef {Record<`win:${string}`, (payload: any, event: MessageEvent) => void> & Record void>} TinyIframeEventsMap */ /** @type {new (config?: TinyIframeEventsConfig) => import('../../../jsdoc/EventEmitter.mjs').ICustomEventEmitter} */ declare const TypedEventEmitter: new (config?: TinyIframeEventsConfig) => import('../../../jsdoc/EventEmitter.mjs').ICustomEventEmitter; /** * A highly secure and flexible event routing system for structured communication * between a parent window and its iframe using `MessageChannel`. * * This class abstracts the complexity of cross-origin communication by establishing * a direct, un-interceptable port connection after a secure initial handshake. * * Features: * - Secure direct pipeline via `MessageChannel`. * - Customizable internal event names to avoid collisions. * - Symmetrical usage for both parent and iframe. * - Queue management for messages sent before the connection is established. * - Auto-reconnects and resets ports if the target iframe is reloaded. */ declare class TinyIframeEvents extends TypedEventEmitter { #private; _boundWindowMessage: (event: MessageEvent) => void; _boundPortMessage: (event: MessageEvent) => void; get targetWindow(): Window; get targetIframeElement(): HTMLIFrameElement | undefined; get targetOrigin(): string; get selfType(): "iframe" | "parent"; get ready(): boolean; /** * Executes the provided callback when the secure MessageChannel connection is fully established. * If the connection is already ready, the callback is executed immediately. * * @param {() => void} handler - The callback function to execute. */ onReady(handler: () => void): void; /** * Creates a new TinyIframeEvents instance to manage secure communication. * Automatically establishes a MessageChannel connection between contexts. * * @param {TinyIframeEventsConfig} config - Configuration object. */ constructor({ targetIframe, targetOrigin, secretEventName, handshakeEventName, readyEventName, }?: TinyIframeEventsConfig); /** * Gets the internal secret iframe event name used for validation. * @returns {string} */ get secretEventName(): string; /** * Sets the internal secret iframe event name. * @param {string} name * @throws {TypeError} If the value is not a string. */ set secretEventName(name: string); /** * Gets the internal handshake event name used for establishing the MessageChannel. * @returns {string} */ get handshakeEventName(): string; /** * Sets the internal handshake event name. * @param {string} name * @throws {TypeError} If the value is not a string. */ set handshakeEventName(name: string); /** * Gets the internal ready event name used when the connection is fully established. * @returns {string} */ get readyEventName(): string; /** * Sets the internal ready event name. * @param {string} name * @throws {TypeError} If the value is not a string. */ set readyEventName(name: string); /** * Sends an event to the target window through the secure MessageChannel. * * @param {string} eventName - A unique name identifying the event. * @param {*} payload - The data to send with the event. Can be any serializable value. * @throws {TypeError} If `eventName` is not a string. * @throws {Error} If instance has been destroyed. */ winEmit(eventName: string, payload: any): void; /** * Checks if the communication instance has been destroyed. * * @returns {boolean} True if destroyed, false otherwise. */ isDestroyed(): boolean; /** * Unsubscribes all registered event listeners, closes the message port, and cleans up references. * Call this when the instance is no longer needed to prevent memory leaks. */ destroy(): void; } export default TinyIframeEvents; //# sourceMappingURL=TinyIframeEvents.d.mts.map