diff options
author | Fushihara <1039534+fushihara@users.noreply.github.com> | 2024-09-25 09:19:13 +0900 |
---|---|---|
committer | Fushihara <1039534+fushihara@users.noreply.github.com> | 2024-09-25 09:19:13 +0900 |
commit | 4ccee1438e3c341075fdcf1d62e6900eb29d1908 (patch) | |
tree | 852dd4e1b74f6d45371619d1c55d3f94343c5c38 /src/util/articleLoader.ts | |
parent | 2ce86e17bdeaaa00d758ecb312f2171c33d7d5ec (diff) |
データ読み込みのクラスをシングルトン化
Diffstat (limited to 'src/util/articleLoader.ts')
-rw-r--r-- | src/util/articleLoader.ts | 14 |
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[] }; /** * パンくずリストを管理 |