advanced-post-message
Version:
Inspired from post robot, this module is designed to provide a simple interface for cross domain communication.
68 lines • 2.51 kB
TypeScript
import { EventManagerOptions, EventManagerOnOptions, EventManagerSendOptions, RequestHandler } from "./eventManager.types";
/**
* Manages the events and message communication between different windows or iframes.
*/
export declare class AdvancedPostMessage {
private requestMessageHandlers;
private responseMessageHandlers;
private postMessage;
private logger;
private config;
constructor(channelId: string, options?: Partial<EventManagerOptions>);
/**
* Handle an incoming post message event
* @param event The post message event containing details of the request
* @returns A promise that resolves when the response is received
*/
private handleIncomingMessage;
/**
* Send an event to the target window
* @param type The type of event to send
* @param payload The payload to send with the event
*
* @example
* const eventManager = new EventManager("channel-id");
*
* const output = eventManager.send("my-event", { foo: "bar" });
* console.log(output) // { foo: "bar1" }
*/
send<ReturnType = undefined>(type: string, payload?: any, options?: Partial<EventManagerSendOptions>): Promise<ReturnType>;
/**
* Register an event handler for a specific event type
* @param type The type of event to listen for
* @param handler The handler to call when the event is received
* @returns An object with an unregister method to unregister the event
*
* @example
* const eventManager = new EventManager("channel-id");
*
* const unregister = eventManager.on("my-event", (event) => {
* console.log("event received", event.data);
* return { foo: "bar1" };
* });
*
* unregister();
*/
on<Payload = unknown, ReturnType = any>(type: string, handler: RequestHandler<Payload, ReturnType>, options?: Partial<EventManagerOnOptions>): {
unregister: () => void;
};
/**
* Unregister an event handler for a specific event type
* @param type The type of event to unregister
*/
private unregisterEvent;
/**
* Updates the configuration options for the event manager.
* @param config - The partial configuration options to update.
*/
updateConfig(config: Partial<EventManagerOptions> & {
channelId?: string;
}): void;
/**
* Destroy the event manager
*/
destroy(config?: {
soft?: boolean;
}): void;
}
//# sourceMappingURL=eventManager.d.ts.map