This commit is contained in:
2023-09-23 11:44:04 +02:00
parent 14b2f01ef1
commit 2c728c7037

View File

@@ -27,17 +27,27 @@ export const useApraisalStore = defineStore('appraisal', () => {
const getPrice = async (type: MarketType): Promise<MarketTypePrice> => (await getPrices([type]))[0];
const getPrices = async (types: MarketType[]): Promise<MarketTypePrice[]> => {
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 };
});