From 8055cd089f451e830212a7c8c4ffa2eee1759a38 Mon Sep 17 00:00:00 2001 From: Max Goodhart Date: Tue, 23 Jun 2020 23:57:46 -0700 Subject: [PATCH] Use cached stream data if API fails --- src/node/data.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/node/data.js b/src/node/data.js index 221f7cc..112bdc1 100644 --- a/src/node/data.js +++ b/src/node/data.js @@ -20,6 +20,7 @@ function compareStrings(a, b) { export async function* pollPublicData() { const publicDataURL = 'https://woke.net/api/streams.json' const refreshInterval = 5 * 1000 + let lastData = [] while (true) { let data = [] try { @@ -28,7 +29,15 @@ export async function* pollPublicData() { } catch (err) { console.warn('error loading stream data', err) } - yield filterLive(data) + + // If the endpoint errors or returns an empty dataset, keep the cached data. + if (!data.length && lastData.length) { + console.warn('using cached stream data') + } else { + yield filterLive(data) + lastData = data + } + await sleep(refreshInterval) } }