sdk: replace all the early-load logic with promises

This commit is contained in:
mappu 2023-05-17 18:09:28 +12:00
parent f13618fef1
commit ad56309cb0

View File

@ -12,27 +12,14 @@
return currentScript.replace(currentScriptFile, ''); return currentScript.replace(currentScriptFile, '');
}; };
var loadScript = function(url, onLoad) { var loadScript = function(url) {
return new Promise(function(resolve, reject) {
var script = document.createElement('script'); var script = document.createElement('script');
script.onload = onLoad; script.onload = resolve;
script.onerror = reject;
script.src = url; script.src = url;
document.head.appendChild(script); document.head.appendChild(script);
}; });
var loadScripts = function(urls, onLoad) {
// load sequentially
var i = 0;
var loadNext = function() {
if (i === urls.length) {
onLoad();
return;
}
var url = urls[i];
i += 1;
loadScript(url, loadNext);
};
loadNext();
}; };
var formatBytes = function(bytes) { var formatBytes = function(bytes) {
@ -221,7 +208,7 @@
</div> </div>
`; `;
var initArea = function (elementSelector, onUploaded, onClose) { var initArea = function (aboutInfo, elementSelector, onUploaded, onClose) {
onUploaded = onUploaded || function () { }; onUploaded = onUploaded || function () { };
onClose = onClose || function () { }; onClose = onClose || function () { };
@ -233,15 +220,12 @@
// <input type="hidden" name="MAX_FILE_SIZE" value="` + ret.MaxUploadBytes + `" /> // <input type="hidden" name="MAX_FILE_SIZE" value="` + ret.MaxUploadBytes + `" />
// Create a new div for ourselves on top of the existing area // Create a new div for ourselves on top of the existing area
$.get(contented.baseURL + "about", function (ret) {
var extraText = ""; var extraText = "";
if (ret.MaxUploadBytes > 0) { if (aboutInfo.MaxUploadBytes > 0) {
extraText = " (max " + formatBytes(ret.MaxUploadBytes) + ")"; extraText = " (max " + formatBytes(aboutInfo.MaxUploadBytes) + ")";
} }
var $f = $("<div>").html(widgetHtml); var $f = $("<div>").html(widgetHtml);
$f.find(".contented-extratext").text(extraText); $f.find(".contented-extratext").text(extraText);
@ -512,24 +496,6 @@
} }
// .
});
});
};
var afterScriptsLoaded = function() {
// Update fields in global variable
contented.init = initArea;
contented.loaded = true;
// Call initArea for all pre-initialised elements
for (var i = 0; i < contented.__preInit.length; ++i) {
initArea(contented.__preInit[i][0], contented.__preInit[i][1], contented.__preInit[i][2]);
}
}; };
var init = function() { var init = function() {
@ -537,6 +503,34 @@
var currentScriptPath = getCurrentScriptPath(); var currentScriptPath = getCurrentScriptPath();
var baseURL = currentScriptPath.replace('sdk.js', ''); var baseURL = currentScriptPath.replace('sdk.js', '');
// Kick off background promises
var loader = new Promise(function(resolve, reject) {
if (typeof jQuery === "undefined") {
loadScript(contented.baseURL + "jquery-3.7.0.min.js").then(resolve);
} else {
resolve();
}
}).then(function() { return new Promise(function(resolve, reject) {
if (typeof DrawingBoard === "undefined") {
loadScript(contented.baseURL + "drawingboard-0.4.6.min.js").then(resolve);
} else {
resolve();
}
})}).then(function() { return new Promise(function(resolve, reject) {
$.get(contented.baseURL + "about", function (aboutInfo) {
resolve(aboutInfo);
});
})}).then(function(aboutInfo) {
// Update fields in global variable
window.contented.loaded = true;
return aboutInfo;
});
window.contented = { window.contented = {
"loaded": false, "loaded": false,
@ -552,7 +546,9 @@
* @param Function onClose Called when the widget is being destroyed * @param Function onClose Called when the widget is being destroyed
*/ */
"init": function(elementSelector, onUploaded, onClose) { "init": function(elementSelector, onUploaded, onClose) {
contented.__preInit.push([elementSelector, onUploaded, onClose]); loader.then(function(aboutInfo) {
initArea(aboutInfo, elementSelector, onUploaded, onClose);
});
}, },
/** /**
@ -589,16 +585,6 @@
} }
}; };
// Load scripts
var needScripts = [];
if (typeof jQuery === "undefined") {
needScripts.push(contented.baseURL + "jquery-3.7.0.min.js");
}
if (typeof DrawingBoard === "undefined") {
needScripts.push(contented.baseURL + "drawingboard-0.4.6.min.js");
}
loadScripts(needScripts, afterScriptsLoaded);
}; };
init(); init();