summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorFushihara <1039534+fushihara@users.noreply.github.com>2024-09-25 09:19:13 +0900
committerFushihara <1039534+fushihara@users.noreply.github.com>2024-09-25 09:19:13 +0900
commit4ccee1438e3c341075fdcf1d62e6900eb29d1908 (patch)
tree852dd4e1b74f6d45371619d1c55d3f94343c5c38 /src/util
parent2ce86e17bdeaaa00d758ecb312f2171c33d7d5ec (diff)
データ読み込みのクラスをシングルトン化
Diffstat (limited to 'src/util')
-rw-r--r--src/util/animeLoader.ts10
-rw-r--r--src/util/articleLoader.ts14
2 files changed, 17 insertions, 7 deletions
diff --git a/src/util/animeLoader.ts b/src/util/animeLoader.ts
index 120123b..92dbd5f 100644
--- a/src/util/animeLoader.ts
+++ b/src/util/animeLoader.ts
@@ -58,18 +58,19 @@ const zodType = z.array(
);
const MAX_ITEM_LIMIT = process.env["AKIBA_SOUKEN_MAX_ITEM_LIMIT"];
export type AnimeLoaderData = z.infer<typeof zodType>[number]
-class AnimeLoader {
+export class AnimeLoader {
+ static instance = new AnimeLoader();
constructor() { }
- private dataCache: Awaited<ReturnType<AnimeLoader["loadData_"]>> | null = null;
+ private dataCache: Awaited<ReturnType<AnimeLoader["_loadData"]>> | null = null;
async loadData() {
if (this.dataCache != null) {
return this.dataCache;
}
- const loadedData = await this.loadData_();
+ const loadedData = await this._loadData();
this.dataCache = loadedData;
return loadedData;
}
- private async loadData_() {
+ private async _loadData() {
const articleJsonPath = process.env["AKIBA_SOUKEN_ANIME_JSON"]!;
//console.log(`[${articleJsonPath}]`);
const jsonStr = await readFile(articleJsonPath, { encoding: "utf-8" }).then(text => {
@@ -90,4 +91,3 @@ class AnimeLoader {
return parsedObj;
}
}
-export const animeLoader = new AnimeLoader(); \ No newline at end of file
diff --git a/src/util/articleLoader.ts b/src/util/articleLoader.ts
index f63b28c..860593d 100644
--- a/src/util/articleLoader.ts
+++ b/src/util/articleLoader.ts
@@ -12,8 +12,18 @@ const zodType = z.array(
);
const MAX_ITEM_LIMIT = process.env["AKIBA_SOUKEN_MAX_ITEM_LIMIT"];
export class ArticleLoader {
- constructor() { }
+ static instance = new ArticleLoader();
+ private constructor() { }
+ private dataCache: Awaited<ReturnType<ArticleLoader["_loadData"]>> | null = null;
async loadData() {
+ if (this.dataCache != null) {
+ return this.dataCache;
+ }
+ const loadedData = await this._loadData();
+ this.dataCache = loadedData;
+ return loadedData;
+ }
+ private async _loadData() {
const articleJsonPath = process.env["AKIBA_SOUKEN_ARTICLE_JSON"]!;
const jsonStr = await readFile(articleJsonPath, { encoding: "utf-8" }).then(text => {
const jsonObj = JSON.parse(text);
@@ -28,7 +38,6 @@ export class ArticleLoader {
}
return parsedObj;
}
-
async getCategoryList() {
const loadedData = await this.loadData();
// key:カテゴリ名 , val:回数
@@ -95,6 +104,7 @@ export class ArticleLoader {
}
}
+
type BreadcrumbInternal = { name: string, count: number, child: BreadcrumbInternal[] };
/**
* パンくずリストを管理