Browse Source

refactor cachedFetchJSON into cache.ts

raylu 5 ngày trước cách đây
mục cha
commit
6b042a24a3
2 tập tin đã thay đổi với 9 bổ sung9 xóa
  1. 1 9
      ts/buy.ts
  2. 8 0
      ts/cache.ts

+ 1 - 9
ts/buy.ts

@@ -1,3 +1,4 @@
+import {cachedFetchJSON} from './cache';
 import {setupPopover} from './popover';
 
 const warehouseNames = {
@@ -210,15 +211,6 @@ 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 {
 	id: string;
 	name: string;

+ 8 - 0
ts/cache.ts

@@ -0,0 +1,8 @@
+const fetchCache = new Map<string, any>();
+export 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;
+}