Cleaned code; overwrite cache if newer results are found
This commit is contained in:
parent
7a922261e3
commit
bc685c63ef
@ -28,7 +28,7 @@ export function getDomain() {
|
|||||||
return window.location.hostname.split('.').slice(-2).join('.')
|
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 request = await fetch(`${config.server_endpoint}/cache/${endpoint}`)
|
||||||
const response = (await request.json()) as HttpResponse<T[]>
|
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 ?? []
|
const items = response.data ?? []
|
||||||
|
|
||||||
for (const item of items) {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,11 +15,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts" async>
|
<script setup lang="ts" async>
|
||||||
import config from '@/application/config'
|
|
||||||
import type { HttpResponse, MapObject } from '@/application/types'
|
|
||||||
import type { BaseStorage } from '@/storage/baseStorage'
|
|
||||||
import { CharacterHairStorage, CharacterTypeStorage, MapObjectStorage, MapStorage, SpriteStorage, TileStorage } from '@/storage/storages'
|
import { CharacterHairStorage, CharacterTypeStorage, MapObjectStorage, MapStorage, SpriteStorage, TileStorage } from '@/storage/storages'
|
||||||
// import type { Map } from '@/application/types'
|
|
||||||
import { useGameStore } from '@/stores/gameStore'
|
import { useGameStore } from '@/stores/gameStore'
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { downloadCache } from '@/application/utilities'
|
import { downloadCache } from '@/application/utilities'
|
||||||
@ -29,14 +25,10 @@ const gameStore = useGameStore()
|
|||||||
const totalItems = ref(0)
|
const totalItems = ref(0)
|
||||||
const currentItem = ref(0)
|
const currentItem = ref(0)
|
||||||
|
|
||||||
const tileStorage = new TileStorage()
|
|
||||||
const mapStorage = new MapStorage()
|
|
||||||
const mapObjectStorage = new MapObjectStorage()
|
|
||||||
|
|
||||||
Promise.all([
|
Promise.all([
|
||||||
downloadCache('tiles', tileStorage),
|
downloadCache('tiles', new TileStorage()),
|
||||||
downloadCache('maps', mapStorage),
|
downloadCache('maps', new MapStorage()),
|
||||||
downloadCache('map_objects', mapObjectStorage),
|
downloadCache('map_objects', new MapObjectStorage()),
|
||||||
downloadCache('sprites', new SpriteStorage()),
|
downloadCache('sprites', new SpriteStorage()),
|
||||||
downloadCache('character_types', new CharacterTypeStorage()),
|
downloadCache('character_types', new CharacterTypeStorage()),
|
||||||
downloadCache('character_hair', new CharacterHairStorage())
|
downloadCache('character_hair', new CharacterHairStorage())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user