From 2c728c7037b955766a09a168ee03e839919753f9 Mon Sep 17 00:00:00 2001 From: Sirttas Date: Sat, 23 Sep 2023 11:44:04 +0200 Subject: [PATCH] fix --- src/market/appraisal.ts | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/market/appraisal.ts b/src/market/appraisal.ts index a1b085a..c2833b5 100644 --- a/src/market/appraisal.ts +++ b/src/market/appraisal.ts @@ -27,17 +27,27 @@ export const useApraisalStore = defineStore('appraisal', () => { const getPrice = async (type: MarketType): Promise => (await getPrices([type]))[0]; const getPrices = async (types: MarketType[]): Promise => { - const now = new Date().getTime(); - const cached = types.map(t => cache.value[t.id]).filter(c => c && now - c.date.getTime() < cacheDuration); - const uncached = types.filter(t => !cached.find(c => c.price.type.id === t.id)); + const now = new Date(); + const cached: MarketTypePrice[] = []; + const uncached: MarketType[] = []; + + types.forEach(t => { + const cachedPrice = cache.value[t.id]; + + if (cachedPrice && now.getTime() - cachedPrice.date.getTime() < cacheDuration) { + cached.push(cachedPrice.price); + } else { + uncached.push(t); + } + }); if (uncached.length > 0) { const prices = await getPricesUncached(uncached); - prices.forEach(p => cache.value[p.type.id] = { price: p, date: new Date() }); - return [...cached.map(c => c.price), ...prices]; + prices.forEach(p => cache.value[p.type.id] = { price: p, date: now }); + return [...cached, ...prices]; } - return cached.map(c => c.price); + return cached; }; return { getPrice, getPrices }; }); \ No newline at end of file