Skip to content

Commit 71aa71a

Browse files
committed
Add a flag to indicate whether the sandbox IFrame is ready to receive messages and execute scripts.
1 parent 7dc7a03 commit 71aa71a

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/ui/widgets/EmbeddedDisplay/scripts/scriptExecutor.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import log from "loglevel";
22
import { v4 as uuidv4 } from "uuid";
33

44
let iFrameSandboxScriptRunner: HTMLIFrameElement | null = null;
5+
let iFrameSandboxReady = false;
56

67
export interface ScriptResponse {
78
functionReturnValue: any;
@@ -95,6 +96,7 @@ const buildSandboxIframe = async (): Promise<HTMLIFrameElement> => {
9596
event.source === iFrameSandboxScriptRunner?.contentWindow
9697
) {
9798
log.debug("The script runner iframe has started");
99+
iFrameSandboxReady = true;
98100
window.removeEventListener("message", onMessage);
99101
resolve(iFrameSandboxScriptRunner as HTMLIFrameElement);
100102
}
@@ -129,6 +131,18 @@ export const executeDynamicScriptInSandbox = async (
129131
await buildSandboxIframe();
130132
}
131133

134+
if (!iFrameSandboxReady) {
135+
// The iFrame sandbox is still starting up, this can happen on app startup.
136+
// We don't want to block, so log and return an empty response.
137+
log.warn(
138+
"The Iframe sandbox is starting up, dynamic script execution skipped on this occasion."
139+
);
140+
return {
141+
functionReturnValue: "",
142+
widgetProps: {}
143+
};
144+
}
145+
132146
if (!iFrameSandboxScriptRunner?.contentWindow) {
133147
throw new Error("Iframe content window not available");
134148
}

0 commit comments

Comments
 (0)