Fix case where "ready" message is received before frame "load" event (#1822)

This commit is contained in:
toasted-nutbread 2021-07-10 21:37:37 -04:00 committed by GitHub
parent 2387ed43d9
commit 437b588411
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -67,20 +67,32 @@ class TemplateRendererProxy {
_loadFrame(frame, url, timeout=5000) { _loadFrame(frame, url, timeout=5000) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let state = 0x0; // 0x1 = frame added; 0x2 = frame loaded; 0x4 = frame ready
const cleanup = () => { const cleanup = () => {
frame.removeEventListener('load', onLoad, false);
window.removeEventListener('message', onWindowMessage, false); window.removeEventListener('message', onWindowMessage, false);
if (timer !== null) { if (timer !== null) {
clearTimeout(timer); clearTimeout(timer);
timer = null; timer = null;
} }
}; };
const updateState = (flags) => {
state |= flags;
if (state !== 0x7) { return; }
cleanup();
resolve();
};
const onLoad = () => {
if ((state & 0x3) !== 0x1) { return; }
updateState(0x2);
};
const onWindowMessage = (e) => { const onWindowMessage = (e) => {
if ((state & 0x5) !== 0x1) { return; }
const frameWindow = frame.contentWindow; const frameWindow = frame.contentWindow;
if (frameWindow === null || frameWindow !== e.source) { return; } if (frameWindow === null || frameWindow !== e.source) { return; }
const {data} = e; const {data} = e;
if (!(typeof data === 'object' && data !== null && data.action === 'ready')) { return; } if (!(typeof data === 'object' && data !== null && data.action === 'ready')) { return; }
cleanup(); updateState(0x4);
resolve();
}; };
let timer = setTimeout(() => { let timer = setTimeout(() => {
@ -91,9 +103,11 @@ class TemplateRendererProxy {
frame.removeAttribute('src'); frame.removeAttribute('src');
frame.removeAttribute('srcdoc'); frame.removeAttribute('srcdoc');
frame.addEventListener('load', onLoad, false);
window.addEventListener('message', onWindowMessage, false); window.addEventListener('message', onWindowMessage, false);
try { try {
document.body.appendChild(frame); document.body.appendChild(frame);
state = 0x1;
frame.contentDocument.location.href = url; frame.contentDocument.location.href = url;
} catch (e) { } catch (e) {
cleanup(); cleanup();