Skip to content

Commit

Permalink
データ読み込みのクラスをシングルトン化
Browse files Browse the repository at this point in the history
  • Loading branch information
fushihara committed Sep 25, 2024
1 parent 2ce86e1 commit 4ccee14
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/app/anime/[animeId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "./style.css";
import style from "./style.module.css";
import dateformat from "dateformat";
import { animeLoader, AnimeLoaderData } from "../../../util/animeLoader";
import { AnimeLoader, AnimeLoaderData } from "../../../util/animeLoader";
import Link from "next/link";
dateformat.i18n.dayNames = [
'日', '月', '火', '水', '木', '金', '土',
Expand All @@ -12,7 +12,7 @@ type PageType = {
params: { animeId: string, }
}
export async function generateMetadata(context: PageType) {
const loadedData = await animeLoader.loadData().then(d => {
const loadedData = await AnimeLoader.instance.loadData().then(d => {
const r = d.find(a => a.animeId == Number(context.params.animeId))!;
return r;
});
Expand All @@ -21,7 +21,7 @@ export async function generateMetadata(context: PageType) {
}
}
export default async function Page(context: PageType) {
const loadedData = await animeLoader.loadData().then(d => {
const loadedData = await AnimeLoader.instance.loadData().then(d => {
const r = d.find(a => a.animeId == Number(context.params.animeId))!;
return r;
});;
Expand Down Expand Up @@ -224,7 +224,7 @@ function episodeHitokotoList(list: AnimeLoaderData["episodeHitokotoList"]) {
</>);
}
export async function generateStaticParams() {
const loadedData = await animeLoader.loadData();
const loadedData = await AnimeLoader.instance.loadData();
return loadedData.map(c => {
return { animeId: String(c.animeId) };
}) satisfies PageType["params"][];
Expand Down
4 changes: 2 additions & 2 deletions src/app/anime/all/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { animeLoader } from "../../../util/animeLoader";
import { AnimeLoader } from "../../../util/animeLoader";
import style from "./style.module.css";
import Link from "next/link";
type PageType = {
Expand All @@ -11,7 +11,7 @@ export async function generateMetadata(context: PageType) {
}
}
export default async function Page(context: PageType) {
const loadedData = await animeLoader.loadData();
const loadedData = await AnimeLoader.instance.loadData();
return (
<div className="p-1 gap-16">
<div className="text-right">全:{loadedData.length}</div>
Expand Down
6 changes: 2 additions & 4 deletions src/app/article/all/[pageId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export async function generateMetadata(context: PageType) {
}
export default async function Page(context: PageType) {
const pageId = getPageIdNumber(context.params.pageId);
const al = new ArticleLoader()
const loadedData = await al.loadData();
const loadedData = await ArticleLoader.instance.loadData();
const chunkdData = chunk(loadedData, PPV);
const displayData = chunkdData[pageId - 1];
return (
Expand Down Expand Up @@ -142,8 +141,7 @@ if (!Number.isInteger(PPV)) {
}
//export const dynamicParams = true;
export async function generateStaticParams() {
const al = new ArticleLoader()
const loadedData = await al.loadData();
const loadedData = await ArticleLoader.instance.loadData();
const chunkdData = chunk(loadedData, PPV);
return chunkdData.map((data, index) => {
return { pageId: `page-${index + 1}`, data: data };
Expand Down
5 changes: 2 additions & 3 deletions src/app/article/tag/[tagName]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ export async function generateMetadata(context: PageType) {
}
}
export default async function Page(context: PageType) {
const al = new ArticleLoader()
const nowPageTagName = decodeURIComponent(context.params.tagName);
const loadedData = await al.loadData().then(articles => {
const loadedData = await ArticleLoader.instance.loadData().then(articles => {
const filterd = articles.filter(article => {
if (article.tags.includes(nowPageTagName)) {
return true;
Expand All @@ -35,7 +34,7 @@ export default async function Page(context: PageType) {
);
}
export async function generateStaticParams() {
const tagList = await new ArticleLoader().getTagList();
const tagList = await ArticleLoader.instance.getTagList();
return tagList.map((data, index) => {
return { tagName: data.tag };
});
Expand Down
2 changes: 1 addition & 1 deletion src/app/article/tag/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function generateMetadata(context: PageType) {
}
}
export default async function Page(context: PageType) {
const tagList = await new ArticleLoader().getTagList();
const tagList = await ArticleLoader.instance.getTagList();
type TAG = { tag: string, count: number, primary?: boolean };
const elementListPcPart: TAG[] = [];
const elementListAkiba: TAG[] = [];
Expand Down
10 changes: 5 additions & 5 deletions src/util/animeLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -90,4 +91,3 @@ class AnimeLoader {
return parsedObj;
}
}
export const animeLoader = new AnimeLoader();
14 changes: 12 additions & 2 deletions src/util/articleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -28,7 +38,6 @@ export class ArticleLoader {
}
return parsedObj;
}

async getCategoryList() {
const loadedData = await this.loadData();
// key:カテゴリ名 , val:回数
Expand Down Expand Up @@ -95,6 +104,7 @@ export class ArticleLoader {
}
}


type BreadcrumbInternal = { name: string, count: number, child: BreadcrumbInternal[] };
/**
* パンくずリストを管理
Expand Down

0 comments on commit 4ccee14

Please sign in to comment.