@remote-ui/rpc
Advanced tools
Comparing version
@@ -14,6 +14,21 @@ 'use strict'; | ||
parent | ||
} = self; // We need to store the listener, because we wrap it to do some origin checking. Ideally, | ||
} = self; | ||
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. | ||
if (document.readyState === 'complete') { | ||
ready(); | ||
} else { | ||
document.addEventListener('readystatechange', () => { | ||
if (document.readyState === 'complete') { | ||
ready(); | ||
} | ||
}); | ||
} // We need to store the listener, because we wrap it to do some origin checking. Ideally, | ||
// we’d instead store an `AbortController`, and use its signal to cancel the listeners, | ||
// but that isn’t widely supported. | ||
const listenerMap = new WeakMap(); | ||
@@ -20,0 +35,0 @@ return { |
@@ -9,4 +9,2 @@ 'use strict'; | ||
} = {}) { | ||
var _target$contentDocume; | ||
if (typeof window === 'undefined') { | ||
@@ -20,5 +18,9 @@ throw new Error(`You can only run fromIframe() in a browser context, but no window was found.`); | ||
const listenerMap = new WeakMap(); | ||
const iframeLoadPromise = ((_target$contentDocume = target.contentDocument) === null || _target$contentDocume === void 0 ? void 0 : _target$contentDocume.readyState) === 'complete' ? Promise.resolve() : new Promise(resolve => { | ||
target.addEventListener('load', () => resolve(), { | ||
once: true | ||
const iframeReadyPromise = new Promise(resolve => { | ||
window.addEventListener('message', event => { | ||
if (event.source !== target.contentWindow) return; | ||
if (event.data === 'remote-ui::ready') { | ||
resolve(); | ||
} | ||
}); | ||
@@ -28,8 +30,3 @@ }); | ||
async postMessage(message, transfer) { | ||
var _target$contentDocume2; | ||
if (((_target$contentDocume2 = target.contentDocument) === null || _target$contentDocume2 === void 0 ? void 0 : _target$contentDocume2.readyState) !== 'complete') { | ||
await iframeLoadPromise; | ||
} | ||
await iframeReadyPromise; | ||
target.contentWindow.postMessage(message, targetOrigin, transfer); | ||
@@ -36,0 +33,0 @@ }, |
{ | ||
"name": "@remote-ui/rpc", | ||
"description": "An RPC library with strong support for simulating the transfer of functions via postMessage", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"publishConfig": { | ||
@@ -26,3 +26,3 @@ "access": "public" | ||
}, | ||
"gitHead": "74cd41c4fe67ef9b9bb7f8c597c722b7eb848b50" | ||
"gitHead": "a35cf5c1e3d89949e60af9b90339c6551325396d" | ||
} |
@@ -12,2 +12,16 @@ import type {MessageEndpoint} from '../types'; | ||
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. | ||
if (document.readyState === 'complete') { | ||
ready(); | ||
} else { | ||
document.addEventListener('readystatechange', () => { | ||
if (document.readyState === 'complete') { | ||
ready(); | ||
} | ||
}); | ||
} | ||
// We need to store the listener, because we wrap it to do some origin checking. Ideally, | ||
@@ -14,0 +28,0 @@ // we’d instead store an `AbortController`, and use its signal to cancel the listeners, |
@@ -21,16 +21,14 @@ import type {MessageEndpoint} from '../types'; | ||
const iframeLoadPromise = | ||
target.contentDocument?.readyState === 'complete' | ||
? Promise.resolve() | ||
: new Promise<void>((resolve) => { | ||
target.addEventListener('load', () => resolve(), { | ||
once: true, | ||
}); | ||
}); | ||
const iframeReadyPromise = new Promise<void>((resolve) => { | ||
window.addEventListener('message', (event) => { | ||
if (event.source !== target.contentWindow) return; | ||
if (event.data === 'remote-ui::ready') { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
return { | ||
async postMessage(message, transfer) { | ||
if (target.contentDocument?.readyState !== 'complete') { | ||
await iframeLoadPromise; | ||
} | ||
await iframeReadyPromise; | ||
@@ -37,0 +35,0 @@ target.contentWindow!.postMessage(message, targetOrigin, transfer); |
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
150023
0.53%2347
1.29%