Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Fix case where "ready" message is received before frame "load" event (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
toasted-nutbread committed Jul 11, 2021
1 parent a03ee0f commit 2b2b74d
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions ext/js/templates/template-renderer-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,32 @@ class TemplateRendererProxy {

_loadFrame(frame, url, timeout=5000) {
return new Promise((resolve, reject) => {
let state = 0x0; // 0x1 = frame added; 0x2 = frame loaded; 0x4 = frame ready
const cleanup = () => {
frame.removeEventListener('load', onLoad, false);
window.removeEventListener('message', onWindowMessage, false);
if (timer !== null) {
clearTimeout(timer);
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) => {
if ((state & 0x5) !== 0x1) { return; }
const frameWindow = frame.contentWindow;
if (frameWindow === null || frameWindow !== e.source) { return; }
const {data} = e;
if (!(typeof data === 'object' && data !== null && data.action === 'ready')) { return; }
cleanup();
resolve();
updateState(0x4);
};

let timer = setTimeout(() => {
Expand All @@ -91,9 +103,11 @@ class TemplateRendererProxy {

frame.removeAttribute('src');
frame.removeAttribute('srcdoc');
frame.addEventListener('load', onLoad, false);
window.addEventListener('message', onWindowMessage, false);
try {
document.body.appendChild(frame);
state = 0x1;
frame.contentDocument.location.href = url;
} catch (e) {
cleanup();
Expand Down

0 comments on commit 2b2b74d

Please sign in to comment.