This commit is contained in:
Danny Harpigny 2023-10-22 17:21:39 +02:00
parent 428b042fe9
commit dbe74f4ed1

View File

@ -1,4 +1,4 @@
const storageType = 'sessionStorage' const storageType = 'localStorage'
var data = {} var data = {}
@ -26,7 +26,7 @@ function locallyGetInstances () {
const item = window[storageType].getItem('instances') const item = window[storageType].getItem('instances')
try { try {
return JSON.parse(item) return JSON.parse(item)
} finally { } catch (_err) {
return [] return []
} }
} }
@ -47,6 +47,7 @@ async function remotelyGetInstances () {
.then((json) => Object.entries(json.instances)) .then((json) => Object.entries(json.instances))
.then((entries) => entries.filter(([_url, details]) => { .then((entries) => entries.filter(([_url, details]) => {
if (details.network_type !== 'normal') return if (details.network_type !== 'normal') return
if (details.uptime == null) return
if (details.uptime.uptimeDay !== 100) return if (details.uptime.uptimeDay !== 100) return
return true return true
})) }))
@ -55,15 +56,22 @@ async function remotelyGetInstances () {
console.error(err) console.error(err)
return [] return []
}) })
window[storageType].setItem('timestamp', Date.now().toString())
return instances return instances
} }
// Get local instances
data.instances = locallyGetInstances() data.instances = locallyGetInstances()
// Fetch remote instances if necessary
const timestamp = +window[storageType].getItem('timestamp')
if (Date.now() > timestamp + 3_600_000 /* 1 hour */) {
const remoteInstances = await remotelyGetInstances() const remoteInstances = await remotelyGetInstances()
if (remoteInstances.length > 0) { if (remoteInstances.length > 0) {
data.instances = remoteInstances data.instances = remoteInstances
locallySetInstances() locallySetInstances()
} }
}
if (data.instances.length > 0) { if (data.instances.length > 0) {
infoEl.textContent = `${data.instances.length} instances` infoEl.textContent = `${data.instances.length} instances`