refactor default modules: move scheduleTimer to one place (#3837)

see #3819

---------

Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com>
Co-authored-by: Veeck <github@veeck.de>
This commit is contained in:
Karsten Hassel 2025-07-10 08:12:09 +02:00 committed by GitHub
parent e115475a9d
commit a05eb23306
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 33 additions and 33 deletions

View File

@ -16,7 +16,8 @@ Thanks to: @dathbe.
### Changed ### Changed
- [clock] Add CSS to prevent line breaking of sunset/sunrise time display (#3816) - [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 ### Updated

18
js/module_functions.js Normal file
View File

@ -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 };

View File

@ -3,6 +3,7 @@ const ical = require("node-ical");
const Log = require("logger"); const Log = require("logger");
const NodeHelper = require("node_helper"); const NodeHelper = require("node_helper");
const CalendarFetcherUtils = require("./calendarfetcherutils"); const CalendarFetcherUtils = require("./calendarfetcherutils");
const { scheduleTimer } = require("#module_functions");
/** /**
* *
@ -65,31 +66,18 @@ const CalendarFetcher = function (url, reloadInterval, excludedEvents, maximumEn
}); });
} catch (error) { } catch (error) {
fetchFailedCallback(this, error); fetchFailedCallback(this, error);
scheduleTimer(); scheduleTimer(reloadTimer, reloadInterval, fetchCalendar);
return; return;
} }
this.broadcastEvents(); this.broadcastEvents();
scheduleTimer(); scheduleTimer(reloadTimer, reloadInterval, fetchCalendar);
}) })
.catch((error) => { .catch((error) => {
fetchFailedCallback(this, 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 */ /* public methods */
/** /**

View File

@ -5,6 +5,7 @@ const iconv = require("iconv-lite");
const { htmlToText } = require("html-to-text"); const { htmlToText } = require("html-to-text");
const Log = require("logger"); const Log = require("logger");
const NodeHelper = require("node_helper"); const NodeHelper = require("node_helper");
const { scheduleTimer } = require("#module_functions");
/** /**
* Responsible for requesting an update on the set interval and broadcasting the data. * Responsible for requesting an update on the set interval and broadcasting the data.
@ -79,12 +80,12 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
parser.on("error", (error) => { parser.on("error", (error) => {
fetchFailedCallback(this, 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 //"end" event is not broadcast if the feed is empty but "finish" is used for both
parser.on("finish", () => { parser.on("finish", () => {
scheduleTimer(); scheduleTimer(reloadTimer, reloadIntervalMS, fetchNews);
}); });
parser.on("ttl", (minutes) => { parser.on("ttl", (minutes) => {
@ -120,23 +121,10 @@ const NewsfeedFetcher = function (url, reloadInterval, encoding, logFeedWarnings
}) })
.catch((error) => { .catch((error) => {
fetchFailedCallback(this, 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 */ /* public methods */
/** /**

View File

@ -23,6 +23,11 @@
"https://github.com/MagicMirrorOrg/MagicMirror/graphs/contributors" "https://github.com/MagicMirrorOrg/MagicMirror/graphs/contributors"
], ],
"type": "commonjs", "type": "commonjs",
"imports": {
"#module_functions": {
"default": "./js/module_functions.js"
}
},
"main": "js/electron.js", "main": "js/electron.js",
"scripts": { "scripts": {
"config:check": "node js/check_config.js", "config:check": "node js/check_config.js",