refactor(#27): Simplify appraisal
This commit is contained in:
@@ -1,47 +1,17 @@
|
||||
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<MarketTypePrice> = new RegionalMarketCache(CACHE_DURATION);
|
||||
export const getPrice = async (type: MarketType, locationId?: number): Promise<MarketTypePrice> => (await getPrices([type], locationId))[0];
|
||||
|
||||
const getPricesUncached = getMammonPrices;
|
||||
export const getPrices = async (types: MarketType[], locationId?: number): Promise<MarketTypePrice[]> => {
|
||||
const batches: Promise<MarketTypePrice[]>[] = [];
|
||||
|
||||
const getPrice = async (type: MarketType, locationId?: number): Promise<MarketTypePrice> => (await getPrices([type], locationId))[0];
|
||||
const getPrices = async (types: MarketType[], locationId?: number): Promise<MarketTypePrice[]> => {
|
||||
const cached: MarketTypePrice[] = [];
|
||||
const uncached: MarketType[] = [];
|
||||
const lId = locationId ?? jitaId;
|
||||
for (let i = 0; i < types.length; i += BATCH_SIZE) {
|
||||
batches.push(getMammonPrices(types.slice(i, i + BATCH_SIZE), locationId));
|
||||
}
|
||||
|
||||
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<MarketTypePrice[]>[] = [];
|
||||
|
||||
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 };
|
||||
});
|
||||
return (await Promise.all(batches)).flat();
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user