This commit is contained in:
parent
428b042fe9
commit
dbe74f4ed1
20
index.js
20
index.js
|
@ -1,4 +1,4 @@
|
|||
const storageType = 'sessionStorage'
|
||||
const storageType = 'localStorage'
|
||||
|
||||
var data = {}
|
||||
|
||||
|
@ -26,7 +26,7 @@ function locallyGetInstances () {
|
|||
const item = window[storageType].getItem('instances')
|
||||
try {
|
||||
return JSON.parse(item)
|
||||
} finally {
|
||||
} catch (_err) {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
@ -47,6 +47,7 @@ async function remotelyGetInstances () {
|
|||
.then((json) => Object.entries(json.instances))
|
||||
.then((entries) => entries.filter(([_url, details]) => {
|
||||
if (details.network_type !== 'normal') return
|
||||
if (details.uptime == null) return
|
||||
if (details.uptime.uptimeDay !== 100) return
|
||||
return true
|
||||
}))
|
||||
|
@ -55,14 +56,21 @@ async function remotelyGetInstances () {
|
|||
console.error(err)
|
||||
return []
|
||||
})
|
||||
window[storageType].setItem('timestamp', Date.now().toString())
|
||||
return instances
|
||||
}
|
||||
|
||||
// Get local instances
|
||||
data.instances = locallyGetInstances()
|
||||
const remoteInstances = await remotelyGetInstances()
|
||||
if (remoteInstances.length > 0) {
|
||||
data.instances = remoteInstances
|
||||
locallySetInstances()
|
||||
|
||||
// Fetch remote instances if necessary
|
||||
const timestamp = +window[storageType].getItem('timestamp')
|
||||
if (Date.now() > timestamp + 3_600_000 /* 1 hour */) {
|
||||
const remoteInstances = await remotelyGetInstances()
|
||||
if (remoteInstances.length > 0) {
|
||||
data.instances = remoteInstances
|
||||
locallySetInstances()
|
||||
}
|
||||
}
|
||||
|
||||
if (data.instances.length > 0) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user