thisday: cache
parent
f6775a3287
commit
d1d255d4b1
|
@ -93,7 +93,23 @@ export default class OnThisDay extends Mixins(GlobalMixin) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async refresh() {
|
async refresh() {
|
||||||
|
// Look for cache
|
||||||
|
const dayIdToday = utils.dateToDayId(new Date());
|
||||||
|
const cacheUrl = `/onthisday/${dayIdToday}`;
|
||||||
|
const cache = await utils.getCachedData<IPhoto[]>(cacheUrl);
|
||||||
|
if (cache) this.process(cache);
|
||||||
|
|
||||||
|
// Network request
|
||||||
const photos = await dav.getOnThisDayRaw();
|
const photos = await dav.getOnThisDayRaw();
|
||||||
|
utils.cacheData(cacheUrl, photos);
|
||||||
|
|
||||||
|
// Check if exactly same as cache
|
||||||
|
if (cache?.length === photos.length &&
|
||||||
|
cache.every((p, i) => p.fileid === photos[i].fileid)) return;
|
||||||
|
this.process(photos);
|
||||||
|
}
|
||||||
|
|
||||||
|
async process(photos: IPhoto[]) {
|
||||||
let currentYear = 9999;
|
let currentYear = 9999;
|
||||||
|
|
||||||
for (const photo of photos) {
|
for (const photo of photos) {
|
||||||
|
|
|
@ -214,10 +214,10 @@ export async function openCache() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get data from the cache */
|
/** Get data from the cache */
|
||||||
export async function getCachedData(url: string) {
|
export async function getCachedData<T>(url: string): Promise<T> {
|
||||||
const cache = staticCache || await openCache();
|
const cache = staticCache || await openCache();
|
||||||
const cachedResponse = await cache.match(url);
|
const cachedResponse = await cache.match(url);
|
||||||
if (!cachedResponse || !cachedResponse.ok) return false;
|
if (!cachedResponse || !cachedResponse.ok) return undefined;
|
||||||
return await cachedResponse.json();
|
return await cachedResponse.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue