Skip to content

Commit

Permalink
js/index-1.ts: seamless autorefresh support
Browse files Browse the repository at this point in the history
This patch adds seamless autorefresh. Every 60 seconds, the client will re-fetch the statistics data. Because the table is built in the background, it won't be modified until it's completely ready - all the browser has to do, essentially, is re-parse the HTML once it's pushed from generate_table into the DOM, a relatively fast operation compared to the other JS operations which have to happen for the table to be recreated.

Signed-off-by: Amy Parker <amy@amyip.net>
  • Loading branch information
amyipdev committed Jul 26, 2023
1 parent 3a26d8e commit f7e2054
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions js/index-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ let listOfServers: Array<string>;
let serverInfoList: Array<ServerInfo> = [];

async function serverListInitialization() {
const r = await fetch("/api/v1/servers");
listOfServers = await r.json();
if (typeof listOfServers === "undefined") {
const r = await fetch("/api/v1/servers");
listOfServers = await r.json();
}
for (const srv of listOfServers) {
const sr = await fetch("/api/v1/uptime/" + srv);
serverInfoList.push(await sr.json());
Expand Down Expand Up @@ -207,4 +209,15 @@ function detectViewportChange(): void {
}
}

window.onresize = detectViewportChange;
window.onresize = detectViewportChange;

// We could just refresh the page, which would reload everything.
// However, it's much more efficient to just re-request the API data
function refreshOnInterval(): void {
// This is perfectly valid code, but ts doesn't like the type issues
// Thus, we must:
// @ts-ignore
serverListInitialization();
}

setInterval(refreshOnInterval, 60000);

0 comments on commit f7e2054

Please sign in to comment.