Cleaned code; overwrite cache if newer results are found
This commit is contained in:
@ -28,7 +28,7 @@ export function getDomain() {
|
||||
return window.location.hostname.split('.').slice(-2).join('.')
|
||||
}
|
||||
|
||||
export async function downloadCache<T extends { id: string }>(endpoint: string, storage: BaseStorage<T>) {
|
||||
export async function downloadCache<T extends { id: string; updatedAt: Date }>(endpoint: string, storage: BaseStorage<T>) {
|
||||
const request = await fetch(`${config.server_endpoint}/cache/${endpoint}`)
|
||||
const response = (await request.json()) as HttpResponse<T[]>
|
||||
|
||||
@ -38,7 +38,15 @@ export async function downloadCache<T extends { id: string }>(endpoint: string,
|
||||
}
|
||||
|
||||
const items = response.data ?? []
|
||||
|
||||
for (const item of items) {
|
||||
await storage.add(item)
|
||||
let overwrite = false
|
||||
const existingItem = await storage.get(item.id)
|
||||
|
||||
if (!existingItem || item.updatedAt > existingItem.updatedAt) {
|
||||
overwrite = true
|
||||
}
|
||||
|
||||
await storage.add(item, overwrite)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user