fix
This commit is contained in:
@@ -27,17 +27,27 @@ export const useApraisalStore = defineStore('appraisal', () => {
|
|||||||
|
|
||||||
const getPrice = async (type: MarketType): Promise<MarketTypePrice> => (await getPrices([type]))[0];
|
const getPrice = async (type: MarketType): Promise<MarketTypePrice> => (await getPrices([type]))[0];
|
||||||
const getPrices = async (types: MarketType[]): Promise<MarketTypePrice[]> => {
|
const getPrices = async (types: MarketType[]): Promise<MarketTypePrice[]> => {
|
||||||
const now = new Date().getTime();
|
const now = new Date();
|
||||||
const cached = types.map(t => cache.value[t.id]).filter(c => c && now - c.date.getTime() < cacheDuration);
|
const cached: MarketTypePrice[] = [];
|
||||||
const uncached = types.filter(t => !cached.find(c => c.price.type.id === t.id));
|
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) {
|
if (uncached.length > 0) {
|
||||||
const prices = await getPricesUncached(uncached);
|
const prices = await getPricesUncached(uncached);
|
||||||
|
|
||||||
prices.forEach(p => cache.value[p.type.id] = { price: p, date: new Date() });
|
prices.forEach(p => cache.value[p.type.id] = { price: p, date: now });
|
||||||
return [...cached.map(c => c.price), ...prices];
|
return [...cached, ...prices];
|
||||||
}
|
}
|
||||||
return cached.map(c => c.price);
|
return cached;
|
||||||
};
|
};
|
||||||
return { getPrice, getPrices };
|
return { getPrice, getPrices };
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user