From c2d2c278e0ed10716cdabad32d22eb9da737bddf Mon Sep 17 00:00:00 2001 From: Felix Wiedenbach Date: Sat, 18 Sep 2021 03:53:40 +0200 Subject: [PATCH] fixed updatenotification async/await issues and more es6 --- .../default/updatenotification/node_helper.js | 56 +++++++++---------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/modules/default/updatenotification/node_helper.js b/modules/default/updatenotification/node_helper.js index c24e190a..a0e68305 100644 --- a/modules/default/updatenotification/node_helper.js +++ b/modules/default/updatenotification/node_helper.js @@ -1,70 +1,66 @@ -const GitHelper = require(__dirname + "/git_helper.js"); -const defaultModules = require(__dirname + "/../defaultmodules.js"); +const GitHelper = require("./git_helper"); +const defaultModules = require("../defaultmodules"); const NodeHelper = require("node_helper"); +const ONE_MINUTE = 60 * 1000; + module.exports = NodeHelper.create({ config: {}, updateTimer: null, updateProcessStarted: false, - gitHelper: new GitHelper.gitHelper(), + gitHelper: new GitHelper(), - start: function () {}, - - configureModules: async function (modules) { - // Push MagicMirror itself , biggest chance it'll show up last in UI and isn't overwritten - // others will be added in front - // this method returns promises so we can't wait for every one to resolve before continuing - this.gitHelper.add("default"); - - for (let moduleName in modules) { + async configureModules(modules) { + for (const moduleName of modules) { if (!this.ignoreUpdateChecking(moduleName)) { - this.gitHelper.add(moduleName); + await this.gitHelper.add(moduleName); } } + + await this.gitHelper.add("default"); }, - socketNotificationReceived: function (notification, payload) { + async socketNotificationReceived(notification, payload) { if (notification === "CONFIG") { this.config = payload; } else if (notification === "MODULES") { // if this is the 1st time thru the update check process if (!this.updateProcessStarted) { this.updateProcessStarted = true; - this.configureModules(payload).then(() => this.performFetch()); + await this.configureModules(payload); + await this.performFetch(); } } }, - performFetch: async function () { - for (let gitInfo of await this.gitHelper.getRepos()) { - this.sendSocketNotification("STATUS", gitInfo); + async performFetch() { + const repos = await this.gitHelper.getRepos(); + + for (const repo of repos) { + this.sendSocketNotification("STATUS", repo); } this.scheduleNextFetch(this.config.updateInterval); }, - scheduleNextFetch: function (delay) { - if (delay < 60 * 1000) { - delay = 60 * 1000; - } - - let self = this; + scheduleNextFetch(delay) { clearTimeout(this.updateTimer); - this.updateTimer = setTimeout(function () { - self.performFetch(); - }, delay); + + this.updateTimer = setTimeout(() => { + this.performFetch(); + }, Math.max(delay, ONE_MINUTE)); }, - ignoreUpdateChecking: function (moduleName) { + ignoreUpdateChecking(moduleName) { // Should not check for updates for default modules - if (defaultModules.indexOf(moduleName) >= 0) { + if (defaultModules.includes(moduleName)) { return true; } // Should not check for updates for ignored modules - if (this.config.ignoreModules.indexOf(moduleName) >= 0) { + if (this.config.ignoreModules.includes(moduleName)) { return true; }