import { Protoframe, ProtoframeMessageType, ProtoframeMessageResponse, ProtoframeMessageBody, ProtoframeDescriptor } from './types'; interface Connector { /** Destroy the connector and all resources/listeners being held. */ destroy(): void; } interface AbstractProtoframeSubscriber
extends Connector {
/**
* Handle a message that was sent with the [[ProtoframePublisher.tell]]
* function.
*
* @param type The message type being handled
* @param handler The handler function for the message
*/
handleTell & undefined>(type: T, handler: (body: ProtoframeMessageBody ) => void): void;
}
interface AbstractProtoframePublisher extends Connector {
/**
* Send a message to a receiving connector. This is a fire-and-forget emitter
* that does not accept a response. If you want a request-response workflow,
* use [[ProtoframePubsub.ask]].
*
* @param type The message type being sent
* @param body The body of the message to send
*/
tell & undefined>(type: T, body: ProtoframeMessageBody ): void;
}
interface AbstractProtoframePubsub extends AbstractProtoframeSubscriber , AbstractProtoframePublisher {
/**
* Send an "ask" message to the receiving connector. On the other end, a
* connector would have invoked `handleAsk` in order to receive this message
* and issue a response.
*
* The promise returned by this call will resolve when a response as been
* received from the target connector, or if the timeout has been exceeded
*
* @param type The message type being asked
* @param body The body of the ask message
* @param timeout How long to wait for a response before the resulting promise
* is rejected with a timeout error.
*/
ask , R extends ProtoframeMessageResponse & Record & Record ) => Promise implements AbstractProtoframeSubscriber {
private readonly protocol;
private readonly thisWindow;
constructor(protocol: ProtoframeDescriptor , thisWindow?: Window);
private listeners;
handleTell & undefined>(type: T, handler: (body: ProtoframeMessageBody ) => void): void;
destroy(): void;
}
export declare class ProtoframePublisher implements AbstractProtoframePublisher {
private readonly protocol;
private readonly targetWindow;
private readonly targetOrigin;
/**
* We are a "parent" page that is embedding an iframe, and we wish to connect
* to that iframe in order to publish messages.
*
* @param protocol The protocol this connector will communicate with
* @param iframe The target iframe HTML element we are connecting to
* @param targetOrigin The target scheme and host we expect the receiver to be
*/
static parent (protocol: ProtoframeDescriptor , iframe: HTMLIFrameElement, targetOrigin?: string): ProtoframePublisher ;
/**
* We are an "iframe" page that will be embedded, and we wish to connect to a
* parent page in order to publish messages.
*
* @param protocol The protocol this connector will communicate with
* @param targetOrigin The target scheme and host we expect the receiver to be
* @param targetWindow The window of the parent frame. This should normally be
* the `window.parent`
*/
static iframe (protocol: ProtoframeDescriptor , targetOrigin?: string, targetWindow?: Window): ProtoframePublisher ;
private listeners;
constructor(protocol: ProtoframeDescriptor , targetWindow: Window, targetOrigin?: string);
tell implements AbstractProtoframePubsub {
private readonly protocol;
private readonly targetWindow;
private readonly thisWindow;
private readonly targetOrigin;
/**
* Connect to the target configured in the supplied pubsub connector by
* sending ping requests over and over until we get a response.
*
* @param pubsub The pubsub connector to wait until is "connected" to its
* target
* @param retries How many times to retry and ping the target. By default,
* this will retry 50 times (thus waiting 25 seconds total)
* @param timeout How long to wait for a response from the target before
* retrying. By default the timeout is 500ms (thus waiting 25 seconds total)
*/
static connect (pubsub: ProtoframePubsub , retries?: number, timeout?: number): Promise (protocol: ProtoframeDescriptor , iframe: HTMLIFrameElement, targetOrigin?: string, thisWindow?: Window): ProtoframePubsub ;
/**
* We are an "iframe" page that will be embedded, and we wish to connect to a
* parent page for communication.
*
* @param protocol The protocol this connector will communicate with
* @param targetOrigin The target scheme and host we expect the receiver to be
* @param thisWindow The window of the current iframe. This should normally be
* the current `window`
* @param targetWindow The window of the parent frame. This should normally be
* the `window.parent`
*/
static iframe (protocol: ProtoframeDescriptor , targetOrigin?: string, { thisWindow, targetWindow, }?: {
thisWindow?: Window;
targetWindow?: Window;
}): ProtoframePubsub ;
private systemProtocol;
private listeners;
constructor(protocol: ProtoframeDescriptor , targetWindow: Window, thisWindow?: Window, targetOrigin?: string);
/**
* Send a 'ping' request to check if there is a listener open at the target
* window. If this times out, then it means no listener was available *at the
* time the ping request was sent*. Since requests are not buffered, then this
* should be retried if we're waiting for some target iframe to start up and
* load its assets. See `ProtoframePubsub.connect` as an implementation of
* this functionality.
*
* @param timeout How long to wait for the reply before resulting in an error
*/
ping({ timeout }: {
timeout?: number;
}): Promise & undefined>(type: T, handler: (body: ProtoframeMessageBody ) => void): void;
tell