Cleanup more callback things (#3051)

Looks quite stable on my computers, so maybe we give it a try?

---------

Co-authored-by: veeck <michael@veeck.de>
This commit is contained in:
Veeck
2023-02-26 18:36:47 +01:00
committed by GitHub
parent b5b61246e6
commit 1b2785cc56
12 changed files with 1243 additions and 1244 deletions

View File

@@ -16,37 +16,9 @@ const Loader = (function () {
/* Private Methods */
/**
* Loops thru all modules and requests load for every module.
* Loops through all modules and requests start for every module.
*/
const loadModules = function () {
let moduleData = getModuleData();
const loadNextModule = function () {
if (moduleData.length > 0) {
const nextModule = moduleData[0];
loadModule(nextModule, function () {
moduleData = moduleData.slice(1);
loadNextModule();
});
} else {
// All modules loaded. Load custom.css
// This is done after all the modules so we can
// overwrite all the defined styles.
loadFile(config.customCss).then(() => {
// custom.css loaded. Start all modules.
startModules();
});
}
};
loadNextModule();
};
/**
* Loops thru all modules and requests start for every module.
*/
const startModules = function () {
const startModules = async function () {
const modulePromises = [];
for (const module of moduleObjects) {
try {
@@ -57,25 +29,25 @@ const Loader = (function () {
}
}
Promise.allSettled(modulePromises).then((results) => {
// Log errors that happened during async node_helper startup
results.forEach((result) => {
if (result.status === "rejected") {
Log.error(result.reason);
}
});
const results = await Promise.allSettled(modulePromises);
// Notify core of loaded modules.
MM.modulesStarted(moduleObjects);
// Starting modules also hides any modules that have requested to be initially hidden
for (const thisModule of moduleObjects) {
if (thisModule.data.hiddenOnStartup) {
Log.info("Initially hiding " + thisModule.name);
thisModule.hide();
}
// Log errors that happened during async node_helper startup
results.forEach((result) => {
if (result.status === "rejected") {
Log.error(result.reason);
}
});
// Notify core of loaded modules.
MM.modulesStarted(moduleObjects);
// Starting modules also hides any modules that have requested to be initially hidden
for (const thisModule of moduleObjects) {
if (thisModule.data.hiddenOnStartup) {
Log.info("Initially hiding " + thisModule.name);
thisModule.hide();
}
}
};
/**
@@ -130,32 +102,30 @@ const Loader = (function () {
};
/**
* Load modules via ajax request and create module objects.s
* Load modules via ajax request and create module objects.
*
* @param {object} module Information about the module we want to load.
* @param {Function} callback Function called when done.
* @returns {Promise<void>} resolved when module is loaded
*/
const loadModule = function (module, callback) {
const loadModule = async function (module) {
const url = module.path + module.file;
const afterLoad = function () {
/**
* @returns {Promise<void>}
*/
const afterLoad = async function () {
const moduleObject = Module.create(module.name);
if (moduleObject) {
bootstrapModule(module, moduleObject, function () {
callback();
});
} else {
callback();
await bootstrapModule(module, moduleObject);
}
};
if (loadedModuleFiles.indexOf(url) !== -1) {
afterLoad();
await afterLoad();
} else {
loadFile(url).then(() => {
loadedModuleFiles.push(url);
afterLoad();
});
await loadFile(url);
loadedModuleFiles.push(url);
await afterLoad();
}
};
@@ -164,24 +134,21 @@ const Loader = (function () {
*
* @param {object} module Information about the module we want to load.
* @param {Module} mObj Modules instance.
* @param {Function} callback Function called when done.
*/
const bootstrapModule = function (module, mObj, callback) {
const bootstrapModule = async function (module, mObj) {
Log.info("Bootstrapping module: " + module.name);
mObj.setData(module);
mObj.loadScripts().then(() => {
Log.log("Scripts loaded for: " + module.name);
mObj.loadStyles().then(() => {
Log.log("Styles loaded for: " + module.name);
mObj.loadTranslations().then(() => {
Log.log("Translations loaded for: " + module.name);
moduleObjects.push(mObj);
callback();
});
});
});
await mObj.loadScripts();
Log.log("Scripts loaded for: " + module.name);
await mObj.loadStyles();
Log.log("Styles loaded for: " + module.name);
await mObj.loadTranslations();
Log.log("Translations loaded for: " + module.name);
moduleObjects.push(mObj);
};
/**
@@ -235,8 +202,28 @@ const Loader = (function () {
/**
* Load all modules as defined in the config.
*/
loadModules: function () {
loadModules();
loadModules: async function () {
let moduleData = getModuleData();
/**
* @returns {Promise<void>} when all modules are loaded
*/
const loadNextModule = async function () {
if (moduleData.length > 0) {
const nextModule = moduleData[0];
await loadModule(nextModule);
moduleData = moduleData.slice(1);
await loadNextModule();
} else {
// All modules loaded. Load custom.css
// This is done after all the modules so we can
// overwrite all the defined styles.
await loadFile(config.customCss);
// custom.css loaded. Start all modules.
await startModules();
}
};
await loadNextModule();
},
/**
@@ -250,7 +237,7 @@ const Loader = (function () {
loadFileForModule: async function (fileName, module) {
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
Log.log("File already loaded: " + fileName);
return Promise.resolve();
return;
}
if (fileName.indexOf("http://") === 0 || fileName.indexOf("https://") === 0 || fileName.indexOf("/") !== -1) {