From d2d1c20d90b39d46e214b0f603965c9c3b4076e3 Mon Sep 17 00:00:00 2001 From: veeck Date: Sun, 19 Feb 2023 22:22:37 +0100 Subject: [PATCH 1/2] Convert module start to async --- js/loader.js | 33 ++++++++++++++++------ js/module.js | 2 +- modules/default/alert/alert.js | 4 +-- modules/default/compliments/compliments.js | 9 +++--- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/js/loader.js b/js/loader.js index d48188bce4..a9c8ad5f86 100644 --- a/js/loader.js +++ b/js/loader.js @@ -47,20 +47,35 @@ const Loader = (function () { * Loops thru all modules and requests start for every module. */ const startModules = function () { + const modulePromises = []; for (const module of moduleObjects) { - module.start(); + try { + modulePromises.push(module.start()); + } catch (error) { + Log.error(`Error when starting node_helper for module ${module.name}:`); + Log.error(error); + } } - // Notify core of loaded modules. - MM.modulesStarted(moduleObjects); + 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); + } + }); + + // 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(); + // 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(); + } } - } + }); }; /** diff --git a/js/module.js b/js/module.js index 0c5445c3fc..3786f61fb3 100644 --- a/js/module.js +++ b/js/module.js @@ -40,7 +40,7 @@ const Module = Class.extend({ /** * Called when the module is started. */ - start: function () { + start: async function () { Log.info("Starting module: " + this.name); }, diff --git a/modules/default/alert/alert.js b/modules/default/alert/alert.js index d1f42c8170..1bd11d02f6 100644 --- a/modules/default/alert/alert.js +++ b/modules/default/alert/alert.js @@ -44,7 +44,7 @@ Module.register("alert", { return `templates/${type}.njk`; }, - start() { + async start() { Log.info(`Starting module: ${this.name}`); if (this.config.effect === "slide") { @@ -53,7 +53,7 @@ Module.register("alert", { if (this.config.welcome_message) { const message = this.config.welcome_message === true ? this.translate("welcome") : this.config.welcome_message; - this.showNotification({ title: this.translate("sysTitle"), message }); + await this.showNotification({ title: this.translate("sysTitle"), message }); } }, diff --git a/modules/default/compliments/compliments.js b/modules/default/compliments/compliments.js index a1fffb8455..dae0a7d0e7 100644 --- a/modules/default/compliments/compliments.js +++ b/modules/default/compliments/compliments.js @@ -33,16 +33,15 @@ Module.register("compliments", { }, // Define start sequence. - start: function () { + start: async function () { Log.info("Starting module: " + this.name); this.lastComplimentIndex = -1; if (this.config.remoteFile !== null) { - this.loadComplimentFile().then((response) => { - this.config.compliments = JSON.parse(response); - this.updateDom(); - }); + const response = await this.loadComplimentFile(); + this.config.compliments = JSON.parse(response); + this.updateDom(); } // Schedule update timer. From e8d373d44c78e0409872faf60793dfde394de567 Mon Sep 17 00:00:00 2001 From: veeck Date: Tue, 21 Feb 2023 23:08:01 +0100 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 413afd5452..1e3276fa2f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ _This release is scheduled to be released on 2023-04-01._ - Update dates in Calendar widgets every minute - Cleanup jest coverage for patches - Update `stylelint` dependencies, switch to `stylelint-config-standard` and handle `stylelint` issues +- Convert module start to async/await - Convert translator callbacks to async/await ### Fixed