@remote-ui/rpc
Advanced tools
Comparing version
@@ -5,2 +5,4 @@ 'use strict'; | ||
var constants = require('./constants.js'); | ||
function fromInsideIframe({ | ||
@@ -11,4 +13,10 @@ targetOrigin = '*' | ||
throw new Error(`This does not appear to be a child iframe, because there is no parent window.`); | ||
} | ||
} // We wait until the document is ready before advertising to the parent that | ||
// communication can commence. | ||
// However, it's possible that the parent isn't listening to messages at this time. | ||
// Which can lead to communication never starting. | ||
// Therefore we also wait for the parent to send a message once it's ready to (re)send the | ||
// ready message from within the child iframe. | ||
const { | ||
@@ -18,6 +26,14 @@ parent | ||
const ready = () => parent.postMessage('remote-ui::ready', targetOrigin); // Listening to `readyState` in iframe, though the child iframe could probably | ||
// send a `postMessage` that it is ready to receive messages sooner than that. | ||
const ready = () => parent.postMessage(constants.READY_MESSAGE_KEY, targetOrigin); | ||
window.addEventListener('message', event => { | ||
if (event.source !== parent || document.readyState !== 'complete') { | ||
return; | ||
} | ||
if (event.data === constants.READY_MESSAGE_KEY) { | ||
ready(); | ||
} | ||
}); | ||
if (document.readyState === 'complete') { | ||
@@ -24,0 +40,0 @@ ready(); |
@@ -5,2 +5,4 @@ 'use strict'; | ||
var constants = require('./constants.js'); | ||
function fromIframe(target, { | ||
@@ -10,2 +12,4 @@ terminate: shouldTerminate = true, | ||
} = {}) { | ||
var _target$contentWindow; | ||
if (typeof window === 'undefined') { | ||
@@ -24,3 +28,3 @@ throw new Error(`You can only run fromIframe() in a browser context, but no window was found.`); | ||
if (event.data === 'remote-ui::ready') { | ||
if (event.data === constants.READY_MESSAGE_KEY) { | ||
window.removeEventListener('message', onMessage); | ||
@@ -31,2 +35,3 @@ resolveIFrameReadyPromise(); | ||
(_target$contentWindow = target.contentWindow) === null || _target$contentWindow === void 0 ? void 0 : _target$contentWindow.postMessage(constants.READY_MESSAGE_KEY, targetOrigin); | ||
const iframeReadyPromise = new Promise(resolve => { | ||
@@ -33,0 +38,0 @@ resolveIFrameReadyPromise = resolve; |
# Changelog | ||
## 1.4.2 | ||
### Patch Changes | ||
- [#215](https://github.com/Shopify/remote-ui/pull/215) [`981d67c`](https://github.com/Shopify/remote-ui/commit/981d67c60754dc6e9f5dbee5794dffcaeb902261) Thanks [@robin-drexler](https://github.com/robin-drexler)! - Ensures iframe connection can always be established | ||
## 1.4.1 | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "@remote-ui/rpc", | ||
"description": "An RPC library with strong support for simulating the transfer of functions via postMessage", | ||
"version": "1.4.1", | ||
"version": "1.4.2", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "access": "public" |
import type {MessageEndpoint} from '../types'; | ||
import {READY_MESSAGE_KEY} from './constants'; | ||
export function fromInsideIframe({targetOrigin = '*'} = {}): MessageEndpoint { | ||
@@ -10,8 +12,25 @@ if (typeof self === 'undefined' || self.parent == null) { | ||
// We wait until the document is ready before advertising to the parent that | ||
// communication can commence. | ||
// However, it's possible that the parent isn't listening to messages at this time. | ||
// Which can lead to communication never starting. | ||
// Therefore we also wait for the parent to send a message once it's ready to (re)send the | ||
// ready message from within the child iframe. | ||
const {parent} = self; | ||
const ready = () => parent.postMessage('remote-ui::ready', targetOrigin); | ||
const ready = () => parent.postMessage(READY_MESSAGE_KEY, targetOrigin); | ||
// Listening to `readyState` in iframe, though the child iframe could probably | ||
// send a `postMessage` that it is ready to receive messages sooner than that. | ||
window.addEventListener('message', (event) => { | ||
if (event.source !== parent || document.readyState !== 'complete') { | ||
return; | ||
} | ||
if (event.data === READY_MESSAGE_KEY) { | ||
ready(); | ||
} | ||
}); | ||
if (document.readyState === 'complete') { | ||
@@ -18,0 +37,0 @@ ready(); |
import type {MessageEndpoint} from '../types'; | ||
import {READY_MESSAGE_KEY} from './constants'; | ||
export function fromIframe( | ||
@@ -26,3 +28,3 @@ target: HTMLIFrameElement, | ||
if (event.data === 'remote-ui::ready') { | ||
if (event.data === READY_MESSAGE_KEY) { | ||
window.removeEventListener('message', onMessage); | ||
@@ -33,2 +35,4 @@ resolveIFrameReadyPromise(); | ||
target.contentWindow?.postMessage(READY_MESSAGE_KEY, targetOrigin); | ||
const iframeReadyPromise = new Promise<void>((resolve) => { | ||
@@ -42,3 +46,2 @@ resolveIFrameReadyPromise = resolve; | ||
await iframeReadyPromise; | ||
target.contentWindow!.postMessage(message, targetOrigin, transfer); | ||
@@ -45,0 +48,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
163349
2.7%81
8%2558
2.2%