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 () {},
configureModules: function (modules) {
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
simpleGits.push({ module: "default", git: SimpleGit(path.normalize(__dirname + "/../../../")) });
var promises = [];
for (var moduleName in modules) {
for (let moduleName in modules) {
if (!this.ignoreUpdateChecking(moduleName)) {
// Default modules are included in the main MagicMirror repo
var moduleFolder = path.normalize(__dirname + "/../../" + moduleName);
let moduleFolder = path.normalize(__dirname + "/../../" + moduleName);
try {
Log.info("Checking git for module: " + moduleName);
let stat = fs.statSync(path.join(moduleFolder, ".git"));
promises.push(this.resolveRemote(moduleName, moduleFolder));
// Throws error if file doesn't exist
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) {
// 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
continue;
}
}
}
return Promise.all(promises);
},
socketNotificationReceived: function (notification, payload) {
@ -54,36 +54,36 @@ module.exports = NodeHelper.create({
}
},
resolveRemote: function (moduleName, moduleFolder) {
return new Promise((resolve, reject) => {
var git = SimpleGit(moduleFolder);
git.getRemotes(true, (err, remotes) => {
if (remotes.length < 1 || remotes[0].name.length < 1) {
// No valid remote for folder, skip
return resolve();
}
// Folder has .git and has at least one git remote, watch this folder
simpleGits.unshift({ module: moduleName, git: git });
resolve();
});
});
resolveRemote: async function (moduleFolder) {
let git = SimpleGit(moduleFolder);
let remotes = await git.getRemotes(true);
if (remotes.length < 1 || remotes[0].name.length < 1) {
throw new Error("No valid remote for folder " + moduleFolder);
}
return git;
},
performFetch: function () {
var self = this;
simpleGits.forEach((sg) => {
sg.git.fetch(["--dry-run"]).status((err, data) => {
data.module = sg.module;
if (!err) {
sg.git.log({ "-1": null }, (err, data2) => {
if (!err && data2.latest && "hash" in data2.latest) {
data.hash = data2.latest.hash;
self.sendSocketNotification("STATUS", data);
}
performFetch: async function () {
for (sg of simpleGits) {
try {
let fetchData = await sg.git.fetch(["--dry-run"]).status();
let logData = await sg.git.log({ "-1": null });
if (logData.latest && "hash" in logData.latest) {
this.sendSocketNotification("STATUS", {
module: sg.module,
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);
},
@ -93,7 +93,7 @@ module.exports = NodeHelper.create({
delay = 60 * 1000;
}
var self = this;
let self = this;
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(function () {
self.performFetch();