ソースを参照

buy: cache fetches

raylu 2 週間 前
コミット
b98dd55748
1 ファイル変更18 行追加9 行削除
  1. 18 9
      ts/buy.ts

+ 18 - 9
ts/buy.ts

@@ -117,7 +117,7 @@ async function calculate(username: string, apiKey: string, supplyForDays: number
 }
 
 async function getPrices(cx: string) {
-	const rawPrices= await fetch('https://refined-prun.github.io/refined-prices/all.json').then(r => r.json());
+	const rawPrices= await cachedFetchJSON('https://refined-prun.github.io/refined-prices/all.json');
 	const prices = new Map<string, RawPrice>();
 	for (const p of rawPrices)
 		if (p.ExchangeCode === cx)
@@ -126,21 +126,21 @@ async function getPrices(cx: string) {
 }
 
 async function getPlanets(username: string, apiKey: string) {
-	const fioBurns: FIOBurn[] = await fetch('https://rest.fnar.net/fioweb/burn/user/' + username,
-		{headers: {'Authorization': apiKey}}).then(r => r.json());
+	const fioBurns: FIOBurn[] = await cachedFetchJSON('https://rest.fnar.net/fioweb/burn/user/' + username,
+			{headers: {'Authorization': apiKey}});
 	const planets = fioBurns.map(burn => new Planet(burn));
 	return planets;
 }
 
 async function warehouseInventory(username: string, apiKey: string, whName: string): Promise<Map<string, number>> {
-	const warehouses: Warehouse[] = await fetch('https://rest.fnar.net/sites/warehouses/' + username,
-		{headers: {'Authorization': apiKey}}).then(r => r.json());
+	const warehouses: Warehouse[] = await cachedFetchJSON('https://rest.fnar.net/sites/warehouses/' + username,
+			{headers: {'Authorization': apiKey}});
 	
 	const inventory = new Map<string, number>();
 	for (const warehouse of warehouses)
 		if (warehouse.LocationNaturalId === whName) {
-			const storage: Storage = await fetch(`https://rest.fnar.net/storage/${username}/${warehouse.StoreId}`,
-				{headers: {'Authorization': apiKey}}).then(r => r.json());
+			const storage: Storage = await cachedFetchJSON(`https://rest.fnar.net/storage/${username}/${warehouse.StoreId}`,
+					{headers: {'Authorization': apiKey}});
 			for (const item of storage.StorageItems)
 				inventory.set(item.MaterialTicker, item.MaterialAmount);
 			break;
@@ -149,8 +149,8 @@ async function warehouseInventory(username: string, apiKey: string, whName: stri
 }
 
 async function getBids(username: string, apiKey: string, cx: string) {
-	const allOrders: ExchangeOrder[] = await fetch('https://rest.fnar.net/cxos/' + username,
-		{headers: {'Authorization': apiKey}}).then(r => r.json());
+	const allOrders: ExchangeOrder[] = await cachedFetchJSON('https://rest.fnar.net/cxos/' + username,
+			{headers: {'Authorization': apiKey}});
 	const orders = allOrders.filter(order =>
 			order.OrderType === 'BUYING' && order.Status !== 'FILLED' && order.ExchangeCode === cx);
 	const bids = new Map<string, number>();
@@ -159,6 +159,15 @@ async function getBids(username: string, apiKey: string, cx: string) {
 	return {bids, orders};
 }
 
+const fetchCache = new Map<string, any>();
+async function cachedFetchJSON(url: string, options?: RequestInit): Promise<any> {
+	if (fetchCache.has(url))
+		return fetchCache.get(url);
+	const response = await fetch(url, options).then((r) => r.json());
+	fetchCache.set(url, response);
+	return response;
+}
+
 class Planet {
 	name: string;
 	inventory: Map<string, number>;