summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2021-07-10 21:37:37 -0400
committerGitHub <noreply@github.com>2021-07-10 21:37:37 -0400
commit437b588411590ad44991f16ed26e2b2f35505e89 (patch)
treef53a16f912586c5bab859c1546befeb670361f31
parent2387ed43d95676de9a5f8c992cb5cfbd91b72875 (diff)
Fix case where "ready" message is received before frame "load" event (#1822)
-rw-r--r--ext/js/templates/template-renderer-proxy.js18
1 files changed, 16 insertions, 2 deletions
diff --git a/ext/js/templates/template-renderer-proxy.js b/ext/js/templates/template-renderer-proxy.js
index b93fdd6b..a1648963 100644
--- a/ext/js/templates/template-renderer-proxy.js
+++ b/ext/js/templates/template-renderer-proxy.js
@@ -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(() => {
@@ -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();