Class IframeParentIO

This design relies on built-in MessageChannel, and requires a pairing process to establish the port. The PORT_INIT_SIGNAL is designed to be initiated by the child frame, parent window will wait for the signal and establish the port.

If PORT_INIT_SIGNAL is started by the parent window, there has to be a delay (with setTimeout) to wait for the child frame to listen to the signal. Parent window can easily listen to iframe onload event, but there is no way to know when child JS is ready to listen to the message without letting child postMessage a signal first.

It's much easier to make sure parent window is ready (listening) before iframe is loaded, so MessageChannel is designed to be created from iframe's side.

It's a good practice to call destroy() on either side of the channel to close MessageChannel and release resources.

Implements

Constructors

Properties

Methods

Constructors

  • Parameters

    • targetWindow: Window

    Returns IframeParentIO

    const io = new IframeParentIO(iframeRef.contentWindow);
    const rpc = new RPCChannel(io, {
    expose: {
    add: (a: number, b: number) => Promise.resolve(a + b),
    },
    });

Properties

name: string = "iframe-parent-io"

Methods