summaryrefslogtreecommitdiff
path: root/src/util/articleLoader.ts
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/articleLoader.ts
parent2ce86e17bdeaaa00d758ecb312f2171c33d7d5ec (diff)
データ読み込みのクラスをシングルトン化
Diffstat (limited to 'src/util/articleLoader.ts')
-rw-r--r--src/util/articleLoader.ts14
1 files changed, 12 insertions, 2 deletions
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[] };
/**
* パンくずリストを管理