From dbe74f4ed15d4f2360c73392563cd49b4d17d184 Mon Sep 17 00:00:00 2001 From: Danny Harpigny Date: Sun, 22 Oct 2023 17:21:39 +0200 Subject: [PATCH] --- index.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index aba8ced..0ce4547 100644 --- a/index.js +++ b/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) {