Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ _This release is scheduled to be released on 2023-04-01._
- Update weather tests
- Changed updatenotification module for MagicMirror repo only: Send only notifications for `master` if there is a tag on a newer commit
- Update dates in Calendar widgets every minute
- More callback -> async refactoring

### Fixed

Expand Down
85 changes: 40 additions & 45 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ function App() {
* Loads the config file. Combines it with the defaults, and runs the
* callback with the found config as argument.
*
* @param {Function} callback Function to be called after loading the config
* @returns {Promise<*>}
*/
function loadConfig(callback) {
async function loadConfig() {
Log.log("Loading config ...");
const defaults = require(`${__dirname}/defaults`);

Expand All @@ -68,8 +68,7 @@ function App() {
fs.accessSync(configFilename, fs.F_OK);
const c = require(configFilename);
checkDeprecatedOptions(c);
const config = Object.assign(defaults, c);
callback(config);
return Object.assign(defaults, c);
} catch (e) {
if (e.code === "ENOENT") {
Log.error(Utils.colors.error("WARNING! Could not find config file. Please create one. Starting with default configuration."));
Expand All @@ -78,7 +77,7 @@ function App() {
} else {
Log.error(Utils.colors.error(`WARNING! Could not load config file. Starting with default configuration. Error found: ${e}`));
}
callback(defaults);
return defaults;
}
}

Expand Down Expand Up @@ -212,58 +211,54 @@ function App() {
* Start the core app.
*
* It loads the config, then it loads all modules. When it's done it
* executes the callback with the config as argument.
* executes the callback with the config as argument
*
* @param {Function} callback Function to be called after start
* @returns {Promise<*>}
*/
this.start = function (callback) {
loadConfig(function (c) {
config = c;
this.start = async function () {
config = await loadConfig();

Log.setLogLevel(config.logLevel);
Log.setLogLevel(config.logLevel);

let modules = [];
let modules = [];

for (const module of config.modules) {
if (!modules.includes(module.module) && !module.disabled) {
modules.push(module.module);
}
for (const module of config.modules) {
if (!modules.includes(module.module) && !module.disabled) {
modules.push(module.module);
}
}

loadModules(modules, async function () {
httpServer = new Server(config);
const { app, io } = await httpServer.open();
Log.log("Server started ...");

const nodePromises = [];
for (let nodeHelper of nodeHelpers) {
nodeHelper.setExpressApp(app);
nodeHelper.setSocketIO(io);

try {
nodePromises.push(nodeHelper.start());
} catch (error) {
Log.error(`Error when starting node_helper for module ${nodeHelper.name}:`);
Log.error(error);
}
loadModules(modules, async function () {
httpServer = new Server(config);
const { app, io } = await httpServer.open();
Log.log("Server started ...");

const nodePromises = [];
for (let nodeHelper of nodeHelpers) {
nodeHelper.setExpressApp(app);
nodeHelper.setSocketIO(io);

try {
nodePromises.push(nodeHelper.start());
} catch (error) {
Log.error(`Error when starting node_helper for module ${nodeHelper.name}:`);
Log.error(error);
}
}

Promise.allSettled(nodePromises).then((results) => {
// Log errors that happened during async node_helper startup
results.forEach((result) => {
if (result.status === "rejected") {
Log.error(result.reason);
}
});

Log.log("Sockets connected & modules started ...");
Promise.allSettled(nodePromises).then((results) => {
// Log errors that happened during async node_helper startup
results.forEach((result) => {
if (result.status === "rejected") {
Log.error(result.reason);
}
});
});

if (typeof callback === "function") {
callback(config);
}
Log.log("Sockets connected & modules started ...");
});
});

return config;
};

/**
Expand Down
4 changes: 1 addition & 3 deletions js/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,5 @@ app.on("certificate-error", (event, webContents, url, error, certificate, callba
// Start the core application if server is run on localhost
// This starts all node helpers and starts the webserver.
if (["localhost", "127.0.0.1", "::1", "::ffff:127.0.0.1", undefined].includes(config.address)) {
core.start(function (c) {
config = c;
});
core.start().then((c) => (config = c));
}
90 changes: 40 additions & 50 deletions js/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Loader = (function () {
// This is done after all the modules so we can
// overwrite all the defined styles.

loadFile(config.customCss, function () {
loadFile(config.customCss).then(() => {
// custom.css loaded. Start all modules.
startModules();
});
Expand Down Expand Up @@ -137,7 +137,7 @@ const Loader = (function () {
if (loadedModuleFiles.indexOf(url) !== -1) {
afterLoad();
} else {
loadFile(url, function () {
loadFile(url).then(() => {
loadedModuleFiles.push(url);
afterLoad();
});
Expand Down Expand Up @@ -173,52 +173,45 @@ const Loader = (function () {
* Load a script or stylesheet by adding it to the dom.
*
* @param {string} fileName Path of the file we want to load.
* @param {Function} callback Function called when done.
* @returns {Promise} resolved when the file is loaded
*/
const loadFile = function (fileName, callback) {
const loadFile = function (fileName) {
const extension = fileName.slice((Math.max(0, fileName.lastIndexOf(".")) || Infinity) + 1);
let script, stylesheet;

switch (extension.toLowerCase()) {
case "js":
Log.log("Load script: " + fileName);
script = document.createElement("script");
script.type = "text/javascript";
script.src = fileName;
script.onload = function () {
if (typeof callback === "function") {
callback();
}
};
script.onerror = function () {
Log.error("Error on loading script:", fileName);
if (typeof callback === "function") {
callback();
}
};

document.getElementsByTagName("body")[0].appendChild(script);
break;
return new Promise((resolve, reject) => {
Log.log("Load script: " + fileName);
script = document.createElement("script");
script.type = "text/javascript";
script.src = fileName;
script.onload = function () {
resolve();
};
script.onerror = function () {
Log.error("Error on loading script:", fileName);
resolve();
};
document.getElementsByTagName("body")[0].appendChild(script);
});
case "css":
Log.log("Load stylesheet: " + fileName);
stylesheet = document.createElement("link");
stylesheet.rel = "stylesheet";
stylesheet.type = "text/css";
stylesheet.href = fileName;
stylesheet.onload = function () {
if (typeof callback === "function") {
callback();
}
};
stylesheet.onerror = function () {
Log.error("Error on loading stylesheet:", fileName);
if (typeof callback === "function") {
callback();
}
};

document.getElementsByTagName("head")[0].appendChild(stylesheet);
break;
return new Promise((resolve, reject) => {
Log.log("Load stylesheet: " + fileName);

stylesheet = document.createElement("link");
stylesheet.rel = "stylesheet";
stylesheet.type = "text/css";
stylesheet.href = fileName;
stylesheet.onload = function () {
resolve();
};
stylesheet.onerror = function () {
Log.error("Error on loading stylesheet:", fileName);
resolve();
};
document.getElementsByTagName("head")[0].appendChild(stylesheet);
});
}
};

Expand All @@ -237,35 +230,32 @@ const Loader = (function () {
*
* @param {string} fileName Path of the file we want to load.
* @param {Module} module The module that calls the loadFile function.
* @param {Function} callback Function called when done.
* @returns {Promise} resolved when the file is loaded
*/
loadFile: function (fileName, module, callback) {
loadFile: function (fileName, module) {
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
Log.log("File already loaded: " + fileName);
callback();
return;
return Promise.resolve();
}

if (fileName.indexOf("http://") === 0 || fileName.indexOf("https://") === 0 || fileName.indexOf("/") !== -1) {
// This is an absolute or relative path.
// Load it and then return.
loadedFiles.push(fileName.toLowerCase());
loadFile(fileName, callback);
return;
return loadFile(fileName);
}

if (vendor[fileName] !== undefined) {
// This file is available in the vendor folder.
// Load it from this vendor folder.
loadedFiles.push(fileName.toLowerCase());
loadFile(config.paths.vendor + "/" + vendor[fileName], callback);
return;
return loadFile(config.paths.vendor + "/" + vendor[fileName]);
}

// File not loaded yet.
// Load it based on the module path.
loadedFiles.push(fileName.toLowerCase());
loadFile(module.file(fileName), callback);
return loadFile(module.file(fileName));
}
};
})();
2 changes: 1 addition & 1 deletion js/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ const Module = Class.extend({
const loadNextDependency = () => {
if (dependencies.length > 0) {
const nextDependency = dependencies[0];
Loader.loadFile(nextDependency, this, () => {
Loader.loadFile(nextDependency, this).then(() => {
dependencies = dependencies.slice(1);
loadNextDependency();
});
Expand Down
2 changes: 1 addition & 1 deletion serveronly/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const app = require("../js/app.js");
const Log = require("logger");

app.start((config) => {
app.start().then((config) => {
const bindAddress = config.address ? config.address : "localhost";
const httpType = config.useHttps ? "https" : "http";
Log.log("\nReady to go! Please point your browser to: " + httpType + "://" + bindAddress + ":" + config.port);
Expand Down