diff --git a/src/native/days.ts b/src/native/days.ts index 91c0e21d..af9275d0 100644 --- a/src/native/days.ts +++ b/src/native/days.ts @@ -1,8 +1,20 @@ import { NAPI, nativex } from './api'; import { API } from '../services/API'; import { has } from './basic'; +import * as utils from '../services/utils'; import type { IDay, IPhoto } from '../types'; +/** Memcache for */ +const daysCache = new Map(); + +// Clear the cache whenever the timeline is refreshed +if (has()) { + document.addEventListener('DOMContentLoaded', () => { + utils.bus.on('memories:timeline:soft-refresh', () => daysCache.clear()); + utils.bus.on('memories:timeline:hard-refresh', () => daysCache.clear()); + }); +} + /** * Merge incoming days into current days. * Both arrays MUST be sorted by dayid descending. @@ -105,12 +117,18 @@ export async function getLocalDays(): Promise { export async function getLocalDay(dayId: number): Promise { if (!has()) return []; + // Check cache + if (daysCache.has(dayId)) return daysCache.get(dayId)!; + const res = await fetch(NAPI.DAY(dayId)); if (!res.ok) return []; const photos: IPhoto[] = await res.json(); photos.forEach((p) => (p.islocal = true)); + // Cache the response + daysCache.set(dayId, photos); + return photos; }