40 lines
1.4 KiB
TypeScript
40 lines
1.4 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import { RegionalMarketCache } from '../RegionalMarketCache';
|
|
import { jitaId } from '../market';
|
|
import { MarketType } from "../type";
|
|
import { MarketTypePrice } from './MarketTypePrice';
|
|
import { getMammonPrices } from './mammon';
|
|
|
|
const cacheDuration = 1000 * 60 * 5; // 5 minutes
|
|
|
|
export const useApraisalStore = defineStore('appraisal', () => {
|
|
const cache: RegionalMarketCache<MarketTypePrice> = new RegionalMarketCache(cacheDuration);
|
|
|
|
const getPricesUncached = getMammonPrices;
|
|
|
|
const getPrice = async (type: MarketType, regionId?: number): Promise<MarketTypePrice> => (await getPrices([type], regionId))[0];
|
|
const getPrices = async (types: MarketType[], regionId?: number): Promise<MarketTypePrice[]> => {
|
|
const cached: MarketTypePrice[] = [];
|
|
const uncached: MarketType[] = [];
|
|
const rId = regionId ?? jitaId;
|
|
|
|
types.forEach(t => {
|
|
const cachedPrice = cache.get(rId, t.id);
|
|
|
|
if (cachedPrice) {
|
|
cached.push(cachedPrice);
|
|
} else {
|
|
uncached.push(t);
|
|
}
|
|
});
|
|
|
|
if (uncached.length > 0) {
|
|
const prices = await getPricesUncached(uncached);
|
|
|
|
prices.forEach(p => cache.set(rId, p.type.id, p));
|
|
return [ ...cached, ...prices ];
|
|
}
|
|
return cached;
|
|
};
|
|
return { getPrice, getPrices };
|
|
}); |