From 493a6ee05b9425a58753e9386442f4de222a09aa Mon Sep 17 00:00:00 2001 From: minie4 Date: Sun, 27 Aug 2023 14:12:38 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Implement=20caching=20of=20timetabl?= =?UTF-8?q?e=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store.js | 51 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/src/store.js b/src/store.js index 7e90acc..175c488 100644 --- a/src/store.js +++ b/src/store.js @@ -30,19 +30,6 @@ export const activeProfileId = ref( localStorage.getItem("activeProfile") || profiles.value[0].id, ); -watch( - () => activeProfile.value.classFilter, - () => { - fetchData(getNextAndPrevDay(selectedDate.value), false); - }, -); - -export const localTimetables = ref( - JSON.parse(localStorage.getItem("timetables")) || [], -); - -export const theme = ref(localStorage.getItem("theme") || "auto"); - watch( profiles, (newValue) => { @@ -53,6 +40,27 @@ watch( watch(activeProfileId, (newValue) => { localStorage.setItem("activeProfile", newValue); }); +watch( + () => activeProfile.value.classFilter, + () => { + fetchData(getNextAndPrevDay(selectedDate.value), false); + }, +); + +export const cachedTimetables = ref( + JSON.parse(localStorage.getItem("cachedTimetables")) || {}, +); +export const localTimetables = ref( + JSON.parse(localStorage.getItem("timetables")) || [], +); + +watch( + cachedTimetables, + (newValue) => { + localStorage.setItem("cachedTimetables", JSON.stringify(newValue)); + }, + { deep: true }, +); watch( localTimetables, (newValue) => { @@ -60,6 +68,8 @@ watch( }, { deep: true }, ); + +export const theme = ref(localStorage.getItem("theme") || "auto"); watch(theme, (newValue) => { localStorage.setItem("theme", newValue); }); @@ -92,8 +102,12 @@ export const timetable = computed(() => { ); }); export const sessionInfo = ref({}); -export const timetables = ref([]); -export const times = ref([]); +export const timetables = ref( + (cachedTimetables.value[activeProfileId.value] || {}).timetables || [], +); +export const times = ref( + (cachedTimetables.value[activeProfileId.value] || {}).times || [], +); export const substitutions = ref({}); export const history = ref({}); export const classList = ref([]); @@ -182,6 +196,13 @@ export async function fetchTimetables() { } else { timetables.value = timetableData.timetables; times.value = timetableData.times; + + cachedTimetables.value[activeProfileId.value] = + structuredClone(timetableData); + for (const timetable of cachedTimetables.value[activeProfileId.value] + .timetables) { + timetable.fromCache = true; + } } }