MCPcopy Create free account
hub / github.com/MagicMirrorOrg/MagicMirror / start

Function start

defaultmodules/clock/clock.js:42–89  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

40 },
41 // Define start sequence.
42 start () {
43 Log.info(`Starting module: ${this.name}`);
44
45 // Schedule update interval.
46 this.second = moment().second();
47 this.minute = moment().minute();
48
49 // Calculate how many ms should pass until next update depending on if seconds is displayed or not
50 const delayCalculator = (reducedSeconds) => {
51 const EXTRA_DELAY = 50; // Deliberate imperceptible delay to prevent off-by-one timekeeping errors
52
53 if (this.config.displaySeconds) {
54 return 1000 - moment().milliseconds() + EXTRA_DELAY;
55 } else {
56 return (60 - reducedSeconds) * 1000 - moment().milliseconds() + EXTRA_DELAY;
57 }
58 };
59
60 // A recursive timeout function instead of interval to avoid drifting
61 const notificationTimer = () => {
62 this.updateDom();
63
64 if (this.config.sendNotifications) {
65 // If seconds is displayed CLOCK_SECOND-notification should be sent (but not when CLOCK_MINUTE-notification is sent)
66 if (this.config.displaySeconds) {
67 this.second = moment().second();
68 if (this.second !== 0) {
69 this.sendNotification("CLOCK_SECOND", this.second);
70 setTimeout(notificationTimer, delayCalculator(0));
71 return;
72 }
73 }
74
75 // If minute changed or seconds isn't displayed send CLOCK_MINUTE-notification
76 this.minute = moment().minute();
77 this.sendNotification("CLOCK_MINUTE", this.minute);
78 }
79
80 setTimeout(notificationTimer, delayCalculator(0));
81 };
82
83 // Set the initial timeout with the amount of seconds elapsed as
84 // reducedSeconds, so it will trigger when the minute changes
85 setTimeout(notificationTimer, delayCalculator(this.second));
86
87 // Set locale.
88 moment.locale(config.language);
89 },
90 // Override dom generator.
91 getDom () {
92 const wrapper = document.createElement("div");

Callers

nothing calls this directly

Calls 1

delayCalculatorFunction · 0.85

Tested by

no test coverage detected