fix(plugins): Fix issues with loadScript not executing onload callback

This commit is contained in:
Erik Ziegler 2018-08-09 11:31:06 +02:00
parent ddbe8b38aa
commit 640aec42dd

View File

@ -8,17 +8,20 @@ export class OHIFPlugin {
// load an individual script URL // load an individual script URL
static loadScript(scriptURL, type = "text/javascript") { static loadScript(scriptURL, type = "text/javascript") {
const script = document.createElement("script"); return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = scriptURL; script.onload = resolve;
script.type = type; script.onerror = reject;
script.async = false;
const head = document.getElementsByTagName("head")[0]; script.src = scriptURL;
head.appendChild(script); script.type = type;
head.removeChild(script); script.async = false;
return script; const head = document.getElementsByTagName("head")[0];
head.appendChild(script);
head.removeChild(script);
});
} }
// reload all the dependency scripts and also // reload all the dependency scripts and also
@ -26,14 +29,14 @@ export class OHIFPlugin {
static reloadPlugin(plugin) { static reloadPlugin(plugin) {
if (plugin.scriptURLs && plugin.scriptURLs.length) { if (plugin.scriptURLs && plugin.scriptURLs.length) {
plugin.scriptURLs.forEach(scriptURL => { plugin.scriptURLs.forEach(scriptURL => {
this.loadScript(scriptURL).onload = function() {} this.loadScript(scriptURL);
}); });
} }
// TODO: Later we should probably merge script and module URLs // TODO: Later we should probably merge script and module URLs
if (plugin.moduleURLs && plugin.moduleURLs.length) { if (plugin.moduleURLs && plugin.moduleURLs.length) {
plugin.moduleURLs.forEach(moduleURLs => { plugin.moduleURLs.forEach(moduleURLs => {
this.loadScript(moduleURLs, "module").onload = function() {} this.loadScript(moduleURLs, "module");
}); });
} }
@ -45,12 +48,14 @@ export class OHIFPlugin {
const type = plugin.module === true ? 'module' : 'text/javascript' const type = plugin.module === true ? 'module' : 'text/javascript'
this.loadScript(scriptURL, type).onload = function() { this.loadScript(scriptURL, type).then(function() {
const entryPointFunction = OHIF.plugins.entryPoints[plugin.name]; const entryPointFunction = OHIF.plugins.entryPoints[plugin.name];
if (entryPointFunction) { if (entryPointFunction) {
entryPointFunction(); entryPointFunction();
} }
} }, error => {
throw new Error(error);
});
} }
} }