Load framework API script earlier to speed up cast init

This commit is contained in:
hensm
2022-04-16 03:53:41 +01:00
parent 31757bb775
commit c217f6cde1

View File

@@ -17,7 +17,7 @@ if (!_window.chrome) {
_window.chrome.cast = cast; _window.chrome.cast = cast;
let bridgeInfo: any; let bridgeInfo: any;
let isFramework = false; let frameworkScriptPromise: Promise<HTMLScriptElement>;
/** /**
* If loaded within a page via a <script> element, * If loaded within a page via a <script> element,
@@ -35,11 +35,11 @@ if (document.currentScript) {
_window.cast = {}; _window.cast = {};
} }
/** // Queue up the framework script load to speed up init
* Set isFramework flag to load framework once the base cast API is frameworkScriptPromise = loadScript(CAST_FRAMEWORK_SCRIPT_URL);
* initialized frameworkScriptPromise.catch(() => {
*/ logger.error("Failed to load CAF script!");
isFramework = true; });
} }
} }
@@ -48,13 +48,9 @@ onMessage(async message => {
case "cast:initialized": { case "cast:initialized": {
bridgeInfo = message.data; bridgeInfo = message.data;
// If framework API is requested, load that first // If framework API is requested, ensure loaded
if (isFramework) { if (frameworkScriptPromise) {
try { await frameworkScriptPromise;
await loadScript(CAST_FRAMEWORK_SCRIPT_URL);
} catch (err) {
logger.error("Failed to load CAF script!");
}
} }
// Call page script/framework API script's init function // Call page script/framework API script's init function