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 = {}
|
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,14 +56,21 @@ 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()
|
||||||
const remoteInstances = await remotelyGetInstances()
|
|
||||||
if (remoteInstances.length > 0) {
|
// Fetch remote instances if necessary
|
||||||
data.instances = remoteInstances
|
const timestamp = +window[storageType].getItem('timestamp')
|
||||||
locallySetInstances()
|
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) {
|
if (data.instances.length > 0) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user