From 417ebe16a9573c123538658bae8853199d244b30 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Tue, 3 May 2022 00:32:59 +0200 Subject: Add caching of data (currently live-time is 5 minutes) --- src/components/WeatherCurrent.svelte | 26 +++++++++- src/stores.ts | 99 ++++++++++++++++++++++++++++++++---- 2 files changed, 112 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/components/WeatherCurrent.svelte b/src/components/WeatherCurrent.svelte index 9c47873..778fb12 100644 --- a/src/components/WeatherCurrent.svelte +++ b/src/components/WeatherCurrent.svelte @@ -2,10 +2,29 @@ import SpinnerRoller from './Spinner/SpinnerRoller.svelte'; import { onMount } from 'svelte'; - import { navigator_location, earth_weather, space_weather } from '../stores'; + import { get } from 'svelte/store'; + import { data_max_age, navigator_location, earth_weather, space_weather } from '../stores'; const monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ]; + + let oldestUpdateTime = new Date(0,0,0); + let timeagoLastUpdate = 0; + function updateOldestUpdateTime() { + let earthWeatherUpdate = get(earth_weather).updated; + let spaceWeatherUpdate = get(space_weather).updated; + + let now = new Date(); + if (now - spaceWeatherUpdate > now - earthWeatherUpdate) { + oldestUpdateTime = spaceWeatherUpdate; + timeagoLastUpdate = now - spaceWeatherUpdate; + } else { + oldestUpdateTime = earthWeatherUpdate; + timeagoLastUpdate = now - earthWeatherUpdate; + } + } + + setInterval(updateOldestUpdateTime, 500);