diff options
author | 0neGal <mail@0negal.com> | 2024-12-20 19:03:41 +0100 |
---|---|---|
committer | 0neGal <mail@0negal.com> | 2024-12-20 19:03:41 +0100 |
commit | 010126567cca3a02073d71a29030ba86d96c195f (patch) | |
tree | 7cd2d18efd2489b5b60bd2c0f81859b6ead90ea3 /src/app/js/request.js | |
parent | 1be8c774251c3a1e1e3559f628aa24b465ab8685 (diff) | |
download | Viper-010126567cca3a02073d71a29030ba86d96c195f.tar.gz Viper-010126567cca3a02073d71a29030ba86d96c195f.zip |
added requests.check() and uses of it
Very minimal uses of it, currently it simply checks every 30s and on
startup whether a set of domains/endpoints we use work, and if all fail,
then we assume something is wrong with the internet.
Diffstat (limited to 'src/app/js/request.js')
-rw-r--r-- | src/app/js/request.js | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/src/app/js/request.js b/src/app/js/request.js index b9b5081..a8d4dd3 100644 --- a/src/app/js/request.js +++ b/src/app/js/request.js @@ -1,18 +1,27 @@ const ipcRenderer = require("electron").ipcRenderer; +let sent_error_toast = false; + let update_status = () => { // if offline, show toast and offline icon if (! navigator.onLine) { - ipcRenderer.send("no-internet"); + if (! sent_error_toast) { + sent_error_toast = true; + ipcRenderer.send("no-internet"); + } + offline.classList.remove("hidden"); - } else { // remove offline icon - offline.classList.add("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); -update_status(); // invokes `requests.get()` from `src/modules/requests.js` through the // main process, and returns the output @@ -20,8 +29,52 @@ let request = async (...args) => { return await ipcRenderer.invoke("request", ...args); } +// invokes `requests.check()` from `src/modules/requests.js` through the +// main process, and returns the output +request.check = async (...args) => { + return await ipcRenderer.invoke("request-check", ...args); +} + request.delete_cache = () => { ipcRenderer.send("delete-request-cache"); } +// 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"); + } + + // check endpoints + let status = await request.check([ + "https://github.com", + "https://northstar.tf", + "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"); + } +}; check_endpoints(); + +// check endpoints every 30 seconds +setInterval(check_endpoints, 30000); + module.exports = request; |