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 CACHE_DURATION = 1000 * 60 * 5; // 5 minutes const BATCH_SIZE = 100; export const useAppraisalStore = defineStore('appraisal', () => { const cache: RegionalMarketCache = new RegionalMarketCache(CACHE_DURATION); const getPricesUncached = getMammonPrices; const getPrice = async (type: MarketType, locationId?: number): Promise => (await getPrices([type], locationId))[0]; const getPrices = async (types: MarketType[], locationId?: number): Promise => { const cached: MarketTypePrice[] = []; const uncached: MarketType[] = []; const lId = locationId ?? jitaId; types.forEach(t => { const cachedPrice = cache.get(lId, t.id); if (cachedPrice) { cached.push(cachedPrice); } else { uncached.push(t); } }); if (uncached.length > 0) { const batches: Promise[] = []; for (let i = 0; i < uncached.length; i += BATCH_SIZE) { batches.push(getPricesUncached(uncached.slice(i, i + BATCH_SIZE), lId)); } const prices = (await Promise.all(batches)).flat(); prices.forEach(p => cache.set(lId, p.type.id, p)); return [ ...cached, ...prices ]; } return cached; }; return { getPrice, getPrices }; });