Refactoring update notification to use async/await

This commit is contained in:
buxxi 2021-03-13 11:12:08 +01:00
parent e4f47178fc
commit 3ed223a550

View File

@ -14,32 +14,32 @@ module.exports = NodeHelper.create({
start: function () {}, start: function () {},
configureModules: function (modules) { configureModules: async function (modules) {
// Push MagicMirror itself , biggest chance it'll show up last in UI and isn't overwritten // Push MagicMirror itself , biggest chance it'll show up last in UI and isn't overwritten
// others will be added in front // others will be added in front
// this method returns promises so we can't wait for every one to resolve before continuing // this method returns promises so we can't wait for every one to resolve before continuing
simpleGits.push({ module: "default", git: SimpleGit(path.normalize(__dirname + "/../../../")) }); simpleGits.push({ module: "default", git: SimpleGit(path.normalize(__dirname + "/../../../")) });
var promises = []; for (let moduleName in modules) {
for (var moduleName in modules) {
if (!this.ignoreUpdateChecking(moduleName)) { if (!this.ignoreUpdateChecking(moduleName)) {
// Default modules are included in the main MagicMirror repo // Default modules are included in the main MagicMirror repo
var moduleFolder = path.normalize(__dirname + "/../../" + moduleName); let moduleFolder = path.normalize(__dirname + "/../../" + moduleName);
try { try {
Log.info("Checking git for module: " + moduleName); Log.info("Checking git for module: " + moduleName);
let stat = fs.statSync(path.join(moduleFolder, ".git")); // Throws error if file doesn't exist
promises.push(this.resolveRemote(moduleName, moduleFolder)); fs.statSync(path.join(moduleFolder, ".git"));
// Fetch the git or throw error if no remotes
let git = await this.resolveRemote(moduleFolder);
// Folder has .git and has at least one git remote, watch this folder
simpleGits.unshift({ module: moduleName, git: git });
} catch (err) { } catch (err) {
// Error when directory .git doesn't exist // Error when directory .git doesn't exist or doesn't have any remotes
// This module is not managed with git, skip // This module is not managed with git, skip
continue; continue;
} }
} }
} }
return Promise.all(promises);
}, },
socketNotificationReceived: function (notification, payload) { socketNotificationReceived: function (notification, payload) {
@ -54,36 +54,36 @@ module.exports = NodeHelper.create({
} }
}, },
resolveRemote: function (moduleName, moduleFolder) { resolveRemote: async function (moduleFolder) {
return new Promise((resolve, reject) => { let git = SimpleGit(moduleFolder);
var git = SimpleGit(moduleFolder); let remotes = await git.getRemotes(true);
git.getRemotes(true, (err, remotes) => {
if (remotes.length < 1 || remotes[0].name.length < 1) { if (remotes.length < 1 || remotes[0].name.length < 1) {
// No valid remote for folder, skip throw new Error("No valid remote for folder " + moduleFolder);
return resolve(); }
}
// Folder has .git and has at least one git remote, watch this folder return git;
simpleGits.unshift({ module: moduleName, git: git });
resolve();
});
});
}, },
performFetch: function () { performFetch: async function () {
var self = this; for (sg of simpleGits) {
simpleGits.forEach((sg) => { try {
sg.git.fetch(["--dry-run"]).status((err, data) => { let fetchData = await sg.git.fetch(["--dry-run"]).status();
data.module = sg.module; let logData = await sg.git.log({ "-1": null });
if (!err) {
sg.git.log({ "-1": null }, (err, data2) => { if (logData.latest && "hash" in logData.latest) {
if (!err && data2.latest && "hash" in data2.latest) { this.sendSocketNotification("STATUS", {
data.hash = data2.latest.hash; module: sg.module,
self.sendSocketNotification("STATUS", data); behind: fetchData.behind,
} current: fetchData.current,
hash: logData.latest.hash,
tracking: fetchData.tracking
}); });
} }
}); } catch (err) {
}); Log.error("Failed to fetch data for git: " + err);
}
}
this.scheduleNextFetch(this.config.updateInterval); this.scheduleNextFetch(this.config.updateInterval);
}, },
@ -93,7 +93,7 @@ module.exports = NodeHelper.create({
delay = 60 * 1000; delay = 60 * 1000;
} }
var self = this; let self = this;
clearTimeout(this.updateTimer); clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function () { this.updateTimer = setTimeout(function () {
self.performFetch(); self.performFetch();