-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
89 lines (78 loc) · 2.67 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// This file is required by the index.html file and will
// be executed in the renderer process for that window.
// Pull in environment variables exposed by preload
const {
DISPLAY_CORNER_TITLE,
MAX_REFRESH_INTERVAL,
} = window.preload.envVars;
// set corner title
document.getElementById('title').innerHTML = DISPLAY_CORNER_TITLE || 'Pi Display';
// pull in IPC function references
const {
updateApiContent,
} = window.preload.ipc;
// pull template processors
const {
clockTemplate,
} = window.preload.templates;
// pull in utility functions
const {
format,
} = window.preload.utils;
/** Infinite loop. Uses render mode flag to call relevant render functions and handles setting the
* interval between when the data is refreshed. Will render less frequently in the case of errors
* being detected. Once a successful render happens, the render interval is reset to it's initial
* value.
*
* @param {Number} interval - Time till next render cycle (ms)
* @param {Number} initialInterval - Original / base render interval (ms)
*/
function handleReload(interval, initialInterval) {
// Display the active reload interval in the page footer
const reloadSpan = document.getElementById('reload-interval');
reloadSpan.innerHTML = `Reloading every ${interval / 1000}s`;
const target = document.getElementById('main');
// Local helper function
function errorBackOff(e) {
console.error(e);
handleReload(
Math.min(interval + initialInterval, parseInt(MAX_REFRESH_INTERVAL * 1000, 10)),
initialInterval,
);
}
setTimeout(() => {
// This call initiates a fetch to configured API server(s) in main.
updateApiContent().then(async (result) => {
// handle returned stuff from main
target.innerHTML = await result;
// successful update; reset reload interval to initial state
handleReload(initialInterval, initialInterval);
})
.catch((e) => errorBackOff(e));
}, interval);
}
/**
* Helper function that renders time data into HTML
*/
function markupClock() {
return clockTemplate({ time: format(new Date(), 'EEEE, MMMM do yyyy, h:mm:ss a') });
}
/**
* Infinite loop. Updates the displayed time every second (1000ms)
*/
function showClock() {
setTimeout(() => {
const target = document.getElementById('clock');
target.innerHTML = markupClock();
showClock();
}, 1000);
}
/**
* Initial starting point for data display on the screen.
* Sets the initial interval and hands off control to handleReload and starts up the clock.
*/
window.onload = () => {
const initialInterval = 30000; // milliseconds
handleReload(5000, initialInterval); // set short initial interval to get quick first load time.
showClock();
};