From 5b3e218d446ac805685e43c8c95ca524755b6601 Mon Sep 17 00:00:00 2001 From: 0neGal Date: Wed, 24 Jan 2024 12:03:25 +0100 Subject: entirely refactor src/modules/requests.js Its now been split into 2, requests.js and releases.js, the latter simply gets relevant info from GitHub release pages. The prior however gives simple functions for doing `GET` requests, and caching the result, and then transparently it'll use that cache when you request it next time. On top of this, some requests made by the renderer will now also use this, and this in turn ends up making loading the mod browser much faster. As instead of having to request the list of packages from Thunderstore, we can simply load the result of an old request. The current lifetime of the cache is 5 minutes, however this can also easily be adjusted. This also moves the cached requests away from /viper-requests.json, and over to /Viper/cached-requests.json --- src/app/js/browser.js | 16 +++++++++++++++- src/app/js/launcher.js | 10 +++++++++- src/app/main.js | 6 ++++++ 3 files changed, 30 insertions(+), 2 deletions(-) (limited to 'src/app') diff --git a/src/app/js/browser.js b/src/app/js/browser.js index c1da9b5..07572d9 100644 --- a/src/app/js/browser.js +++ b/src/app/js/browser.js @@ -166,7 +166,21 @@ var Browser = { packagecount = 0; if (packages.length < 1) { - packages = await (await fetch("https://northstar.thunderstore.io/api/v1/package/")).json(); + let host = "northstar.thunderstore.io"; + let path = "/api/v1/package/"; + + packages = []; + + // attempt to get the list of packages from Thunderstore, if + // this has been done recently, it'll simply return a cached + // version of the request + try { + packages = JSON.parse( + await request(host, path, "thunderstore-packages") + ) + }catch(err) { + console.error(err) + } Browser.add_pkg_properties(); diff --git a/src/app/js/launcher.js b/src/app/js/launcher.js index 5330b7a..c1901f3 100644 --- a/src/app/js/launcher.js +++ b/src/app/js/launcher.js @@ -127,7 +127,15 @@ async function loadServers() { serverstatus.classList.add("checking"); try { - let servers = await (await fetch("https://northstar.tf/client/servers")).json(); + let host = "northstar.tf"; + let path = "/client/servers"; + + // ask the masterserver for the list of servers, if this has + // been done recently, it'll simply return the cached version + let servers = JSON.parse( + await request(host, path, "ns-servers") + ) + masterserver = true; playercount = 0; diff --git a/src/app/main.js b/src/app/main.js index f5fe19f..98a5fda 100644 --- a/src/app/main.js +++ b/src/app/main.js @@ -30,6 +30,12 @@ var settings = { ] } +// invokes `requests.get()` from `src/modules/requests.js` through the +// main process, and returns the output +async function request(...args) { + return await ipcRenderer.invoke("request", ...args); +} + // Sets the lang to the system default ipcRenderer.send("setlang", settings.lang); -- cgit v1.2.3