From d208437c055d47a64306436a74097d168ec6ae29 Mon Sep 17 00:00:00 2001 From: buxxi Date: Sun, 22 Sep 2019 17:33:01 +0200 Subject: [PATCH] Changes to the clock module regarding the notifications sent. Disable sending of CLOCK_SECOND when displaySeconds not set. Avoid drifting by calculating the interval each time and use setTimeout instead of setInterval. Make sure the values sent with CLOCK_SECOND and CLOCK_MINUTE has the correct values instead of starting at 00:00 on startup --- CHANGELOG.md | 2 ++ modules/default/clock/clock.js | 50 ++++++++++++++++++++++------------ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index faba8887..79d1ebcc 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,12 +24,14 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Updated - Updatenotification module: Display update notification for a limited (configurable) time. - The css/custom.css will be rename after the next release. We've add into `run-start.sh` a instruction by GIT to ignore with `--skip-worktree` and `rm --cached`. The history about this change [#1540]. +- Disable sending of notification CLOCK_SECOND when displaySeconds is false ### Fixed - Updatenotification module: Properly handle race conditions, prevent crash. - Send `NEWS_FEED` notification also for the first news messages which are shown - Fixed issue where weather module would not refresh data after a network or API outage [#1722](https://github.com/MichMich/MagicMirror/issues/1722) - Fixed weatherforecast module not displaying rain amount on fallback endpoint +- Notifications CLOCK_SECOND & CLOCK_MINUTE being from startup instead of matched against the clock and avoid drifting ## [2.8.0] - 2019-07-01 diff --git a/modules/default/clock/clock.js b/modules/default/clock/clock.js index 00a71eaa..d566c52f 100644 --- a/modules/default/clock/clock.js +++ b/modules/default/clock/clock.js @@ -41,26 +41,40 @@ Module.register("clock",{ // Schedule update interval. var self = this; - self.second = 0; - self.minute = 0; - self.lastDisplayedMinute = null; - setInterval(function() { - if (self.config.displaySeconds || self.lastDisplayedMinute !== moment().minute()) { - self.updateDom(); - } - if (self.second === 59) { - self.second = 0; - if (self.minute === 59){ - self.minute = 0; - } else { - self.minute++; - } - self.sendNotification("CLOCK_MINUTE", self.minute); + self.second = moment().second(); + self.minute = moment().minute(); + + //Calculate how many ms should pass until next update depending on if seconds is displayed or not + var delayCalculator = function(reducedSeconds) { + if (self.config.displaySeconds) { + return 1000 - moment().milliseconds(); } else { - self.second++; - self.sendNotification("CLOCK_SECOND", self.second); + return ((60 - reducedSeconds) * 1000) - moment().milliseconds(); } - }, 1000); + }; + + //A recursive timeout function instead of interval to avoid drifting + var notificationTimer = function() { + self.updateDom(); + + //If seconds is displayed CLOCK_SECOND-notification should be sent (but not when CLOCK_MINUTE-notification is sent) + if (self.config.displaySeconds) { + self.second = (self.second + 1) % 60; + if (self.second !== 0) { + self.sendNotification("CLOCK_SECOND", self.second); + setTimeout(notificationTimer, delayCalculator(0)); + return; + } + } + + //If minute changed or seconds isn't displayed send CLOCK_MINUTE-notification + self.minute = (self.minute + 1) % 60; + self.sendNotification("CLOCK_MINUTE", self.minute); + setTimeout(notificationTimer, delayCalculator(0)); + }; + + //Set the initial timeout with the amount of seconds elapsed as reducedSeconds so it will trigger when the minute changes + setTimeout(notificationTimer, delayCalculator(self.second)); // Set locale. moment.locale(config.language);