91 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-05-31 22:12:26 +02:00
const NodeHelper = require("node_helper");
const defaultModules = require("../defaultmodules");
const GitHelper = require("./git_helper");
2016-10-15 13:08:46 +02:00
const ONE_MINUTE = 60 * 1000;
2016-10-15 13:08:46 +02:00
module.exports = NodeHelper.create({
config: {},
updateTimer: null,
2019-06-29 15:59:03 -05:00
updateProcessStarted: false,
2016-10-15 13:08:46 +02:00
gitHelper: new GitHelper(),
2019-06-29 15:59:03 -05:00
async configureModules(modules) {
for (const moduleName of modules) {
if (!this.ignoreUpdateChecking(moduleName)) {
await this.gitHelper.add(moduleName);
2016-11-16 18:19:44 +01:00
}
}
if (!this.ignoreUpdateChecking("MagicMirror")) {
await this.gitHelper.add("MagicMirror");
}
2016-10-15 13:08:46 +02:00
},
async socketNotificationReceived(notification, payload) {
switch (notification) {
case "CONFIG":
this.config = payload;
break;
case "MODULES":
// if this is the 1st time thru the update check process
if (!this.updateProcessStarted) {
this.updateProcessStarted = true;
await this.configureModules(payload);
await this.performFetch();
}
break;
case "SCAN_UPDATES":
// 1st time of check allows to force new scan
if (this.updateProcessStarted) {
clearTimeout(this.updateTimer);
await this.performFetch();
}
break;
2016-10-15 13:08:46 +02:00
}
},
async performFetch() {
const repos = await this.gitHelper.getRepos();
for (const repo of repos) {
this.sendSocketNotification("STATUS", repo);
}
2016-10-15 13:08:46 +02:00
if (this.config.sendUpdatesNotifications) {
const updates = await this.gitHelper.checkUpdates();
if (updates.length) this.sendSocketNotification("UPDATES", updates);
}
2016-10-15 13:08:46 +02:00
this.scheduleNextFetch(this.config.updateInterval);
},
scheduleNextFetch(delay) {
2016-10-15 13:08:46 +02:00
clearTimeout(this.updateTimer);
this.updateTimer = setTimeout(
() => {
this.performFetch();
},
Math.max(delay, ONE_MINUTE)
);
},
ignoreUpdateChecking(moduleName) {
// Should not check for updates for default modules
if (defaultModules.includes(moduleName)) {
return true;
}
// Should not check for updates for ignored modules
if (this.config.ignoreModules.includes(moduleName)) {
return true;
}
// The rest of the modules that passes should check for updates
return false;
2016-10-15 13:08:46 +02:00
}
2016-10-17 17:03:10 +02:00
});