Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Thanks to: @dathbe.
### Changed

- [clock] Add CSS to prevent line breaking of sunset/sunrise time display (#3816)
- [core] Enhance system information logging format and include additional env and RAM details
- [core] Enhance system information logging format and include additional env and RAM details (#3839)
- [refactor] Add new file `js/module_functions.js` to move code used in several modules to one place (#3837)

### Updated

Expand Down
18 changes: 18 additions & 0 deletions js/module_functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Schedule the timer for the next update
* @param {object} timer The timer of the module
* @param {bigint} intervalMS interval in milliseconds
* @param {Function} callback function to call when the timer expires
*/
const scheduleTimer = function (timer, intervalMS, callback) {
if (process.env.JEST_WORKER_ID === undefined) {
// only set timer when not running in jest
let tmr = timer;
clearTimeout(tmr);
tmr = setTimeout(function () {
callback();
}, intervalMS);
}
};

module.exports = { scheduleTimer };
20 changes: 4 additions & 16 deletions modules/default/calendar/calendarfetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const ical = require("node-ical");
const Log = require("logger");
const NodeHelper = require("node_helper");
const CalendarFetcherUtils = require("./calendarfetcherutils");
const { scheduleTimer } = require("#module_functions");

/**
*
Expand Down Expand Up @@ -65,31 +66,18 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
});
} catch (error) {
fetchFailedCallback(this, error);
scheduleTimer();
scheduleTimer(reloadTimer, reloadInterval, fetchCalendar);
return;
}
this.broadcastEvents();
scheduleTimer();
scheduleTimer(reloadTimer, reloadInterval, fetchCalendar);
})
.catch((error) => {
fetchFailedCallback(this, error);
scheduleTimer();
scheduleTimer(reloadTimer, reloadInterval, fetchCalendar);
});
};

/**
* Schedule the timer for the next update.
*/
const scheduleTimer = function () {
if (process.env.JEST_WORKER_ID === undefined) {
// only set timer when not running in jest
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function () {
fetchCalendar();
}, reloadInterval);
}
};

/* public methods */

/**
Expand Down
20 changes: 4 additions & 16 deletions modules/default/newsfeed/newsfeedfetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const iconv = require("iconv-lite");
const { htmlToText } = require("html-to-text");
const Log = require("logger");
const NodeHelper = require("node_helper");
const { scheduleTimer } = require("#module_functions");

/**
* Responsible for requesting an update on the set interval and broadcasting the data.
Expand Down Expand Up @@ -79,12 +80,12 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings

parser.on("error", (error) => {
fetchFailedCallback(this, error);
scheduleTimer();
scheduleTimer(reloadTimer, reloadIntervalMS, fetchNews);
});

//"end" event is not broadcast if the feed is empty but "finish" is used for both
parser.on("finish", () => {
scheduleTimer();
scheduleTimer(reloadTimer, reloadIntervalMS, fetchNews);
});

parser.on("ttl", (minutes) => {
Expand Down Expand Up @@ -120,23 +121,10 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
})
.catch((error) => {
fetchFailedCallback(this, error);
scheduleTimer();
scheduleTimer(reloadTimer, reloadIntervalMS, fetchNews);
});
};

/**
* Schedule the timer for the next update.
*/
const scheduleTimer = function () {
if (process.env.JEST_WORKER_ID === undefined) {
// only set timer when not running in jest
clearTimeout(reloadTimer);
reloadTimer = setTimeout(function () {
fetchNews();
}, reloadIntervalMS);
}
};

/* public methods */

/**
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"https://github.com/MagicMirrorOrg/MagicMirror/graphs/contributors"
],
"type": "commonjs",
"imports": {
"#module_functions": {
"default": "./js/module_functions.js"
}
},
"main": "js/electron.js",
"scripts": {
"config:check": "node js/check_config.js",
Expand Down