Stop being lazy and use proper "next day" timer logic

This commit is contained in:
2024-12-20 19:13:15 +00:00
parent c2f112dfc9
commit d069167f59
4 changed files with 20 additions and 12 deletions

View File

@@ -2,6 +2,7 @@
import { ref } from 'vue'; import { ref } from 'vue';
import { useCalendarStore } from '@/stores/calendarStore'; import { useCalendarStore } from '@/stores/calendarStore';
import { format, startOfDay, endOfDay } from 'date-fns'; import { format, startOfDay, endOfDay } from 'date-fns';
import { setNextDayTimer } from '@/nextDayTimer';
import CalendarDay from '@/models/calendar/calendar-day'; import CalendarDay from '@/models/calendar/calendar-day';
const props = defineProps(['days', 'refreshInterval']); const props = defineProps(['days', 'refreshInterval']);
@@ -46,12 +47,14 @@
calendarDays.value = newCalendarDays; calendarDays.value = newCalendarDays;
calendarReady.value = true; calendarReady.value = true;
setNextDayTimer(loadCalendar, 10000);
}); });
} }
loadCalendar(); loadCalendar();
setInterval(() => loadCalendar(), props.refreshInterval); setInterval(loadCalendar, props.refreshInterval);
</script> </script>
<template> <template>

View File

@@ -1,32 +1,25 @@
<script lang="ts" setup> <script lang="ts" setup>
import { ref } from 'vue'; import { ref } from 'vue';
import { useCalendarStore } from '@/stores/calendarStore'; import { useCalendarStore } from '@/stores/calendarStore';
import { startOfDay, isEqual } from 'date-fns'; import { setNextDayTimer } from '@/nextDayTimer';
import NationalDayEntry from '@/models/calendar/national-day'; import NationalDayEntry from '@/models/calendar/national-day';
const calendarStore = useCalendarStore(); const calendarStore = useCalendarStore();
const nationalDays = ref([] as NationalDayEntry[]); const nationalDays = ref([] as NationalDayEntry[]);
const nationalDaysReady = ref(false); const nationalDaysReady = ref(false);
const loadedNationalDay = ref(null as Date | null);
function loadNationalDays() { function loadNationalDays() {
calendarStore.getNationalDays().then((data) => { calendarStore.getNationalDays().then((data) => {
nationalDays.value = data.sort((a, b) => a.name.localeCompare(b.name)); nationalDays.value = data.sort((a, b) => a.name.localeCompare(b.name));
loadedNationalDay.value = startOfDay(new Date());
nationalDaysReady.value = true; nationalDaysReady.value = true;
setNextDayTimer(loadNationalDays, 10000);
}); });
} }
loadNationalDays(); loadNationalDays();
setInterval(() => {
if (loadedNationalDay.value && !isEqual(loadedNationalDay.value, startOfDay(new Date()))) {
loadNationalDays();
}
}, 10 * 1000);
</script> </script>
<template> <template>

View File

@@ -0,0 +1,12 @@
import { addDays, startOfDay } from 'date-fns';
type CallbackFunction = () => void;
export function setNextDayTimer(callback: CallbackFunction, offset: number): void {
const now = new Date();
const startOfNextDay = startOfDay(addDays(now, 1));
const millisecondsUntilNextDay = startOfNextDay.getTime() - now.getTime() + offset;
setTimeout(callback, millisecondsUntilNextDay);
}

View File

@@ -151,7 +151,7 @@
<CalendarAgenda <CalendarAgenda
class="kiosk-calendar" class="kiosk-calendar"
days="7" days="7"
:refresh-interval="30 * 60 * 1000" /> :refresh-interval="5 * 60 * 1000" />
<NationalDays class="kiosk-national-days" /> <NationalDays class="kiosk-national-days" />
</div> </div>
</v-container> </v-container>