diff options
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/js/request.js | 70 |
1 files changed, 29 insertions, 41 deletions
diff --git a/src/app/js/request.js b/src/app/js/request.js index a8d4dd3..d0ef39b 100644 --- a/src/app/js/request.js +++ b/src/app/js/request.js @@ -1,28 +1,5 @@ const ipcRenderer = require("electron").ipcRenderer; -let sent_error_toast = false; - -let update_status = () => { - // if offline, show toast and offline icon - if (! navigator.onLine) { - if (! sent_error_toast) { - sent_error_toast = true; - ipcRenderer.send("no-internet"); - } - - offline.classList.remove("hidden"); - return; - } - - // remove offline icon - offline.classList.add("hidden"); - sent_error_toast = false; -} - -update_status(); -window.addEventListener("online", update_status); -window.addEventListener("offline", update_status); - // invokes `requests.get()` from `src/modules/requests.js` through the // main process, and returns the output let request = async (...args) => { @@ -39,18 +16,39 @@ request.delete_cache = () => { ipcRenderer.send("delete-request-cache"); } +// keeps track of whether we've already sent a toast since we last went +// offline, to prevent multiple toasts +let sent_error_toast = false; + +// shows or hides offline icon, and shows toast depending on `is_online` +let state_action = (is_online) => { + if (is_online) { + // hide offline icon + sent_error_toast = false; + offline.classList.add("hidden"); + } else { + // show toast + if (! sent_error_toast) { + sent_error_toast = true; + ipcRenderer.send("no-internet"); + } + + // show offline icon + offline.classList.remove("hidden"); + } +} + +state_action(navigator.onLine); +window.addEventListener("online", () => state_action(navigator.onLine)); +window.addEventListener("offline", () => state_action(navigator.onLine)); + // checks a list of endpoints/domains we need to be functioning for a // lot of the WAN functionality let check_endpoints = async () => { // if we're not online according to the navigator, it's highly // unlikely the endpoints will succeed if (! navigator.onLine) { - if (! sent_error_toast) { - sent_error_toast = true; - ipcRenderer.send("no-internet"); - } - - return offline.classList.remove("hidden"); + return state_action(false); } // check endpoints @@ -60,18 +58,8 @@ let check_endpoints = async () => { "https://thunderstore.io" ]) - // if just 1 endpoint succeeded, we probably have internet - if (status.succeeded.length) { - sent_error_toast = false; - offline.classList.add("hidden"); - } else { // no endpoint succeeded! - if (! sent_error_toast) { - sent_error_toast = true; - ipcRenderer.send("no-internet"); - } - - offline.classList.remove("hidden"); - } + // handle result of check + state_action(!! status.succeeded.length); }; check_endpoints(); // check endpoints every 30 seconds |