From 76f252b728708635b198518896bd7cda91b93aaa Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:24:26 +0200 Subject: [PATCH 01/14] Add Promises with chained all --- webapp/src/app/page.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index af3f571b..2998aad3 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -31,7 +31,7 @@ export default async function Home() { const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); const messages = await msgAdapter.getMessages(); const store = await Cache.getProjectStore(projectConfig); - const languages = await Promise.all( + const languages = await new Promises( projectConfig.languages.map(async (lang) => { const translations = await store.getTranslations(lang); return { @@ -42,7 +42,7 @@ export default async function Home() { : 0, }; }), - ); + ).all(); return { href: `/projects/${project.name}`, @@ -55,3 +55,11 @@ export default async function Home() { return ; } + +class Promises { + constructor(private promises: Array>) {} + + all() { + return Promise.all(this.promises); + } +} From 2bb2ae488335dc8bb9b3e784c5d62803a42ebcc4 Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:31:56 +0200 Subject: [PATCH 02/14] Factor languages calculation First access data, then extract props. --- webapp/src/app/page.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index 2998aad3..16dd1faf 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -34,6 +34,10 @@ export default async function Home() { const languages = await new Promises( projectConfig.languages.map(async (lang) => { const translations = await store.getTranslations(lang); + return { lang, translations }; + }), + ) + .map(({ lang, translations }) => { return { href: `/projects/${project.name}/${lang}`, language: lang, @@ -41,8 +45,8 @@ export default async function Home() { ? (Object.keys(translations).length / messages.length) * 100 : 0, }; - }), - ).all(); + }) + .all(); return { href: `/projects/${project.name}`, @@ -59,6 +63,10 @@ export default async function Home() { class Promises { constructor(private promises: Array>) {} + map(callbackfn: (value: T) => U): Promises { + return new Promises(this.promises.map((p) => p.then(callbackfn))); + } + all() { return Promise.all(this.promises); } From 26150160a5c4a01cb5b3487cb98b4e3d1fb28ded Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Mon, 29 Jul 2024 22:56:12 +0200 Subject: [PATCH 03/14] Factor projects calculation First access data, then extract props. --- webapp/src/app/page.tsx | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index 16dd1faf..ea8e91cc 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -20,8 +20,8 @@ export const dynamic = 'force-dynamic'; export default async function Home() { const serverConfig = await ServerConfig.read(); - const projects = await Promise.all( - serverConfig.projects.map>(async (project) => { + const projects = await new Promises( + serverConfig.projects.map(async (project) => { await RepoGit.cloneIfNotExist(project); const repoGit = await RepoGit.getRepoGit(project); const lyraConfig = await repoGit.getLyraConfig(); @@ -31,15 +31,21 @@ export default async function Home() { const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); const messages = await msgAdapter.getMessages(); const store = await Cache.getProjectStore(projectConfig); - const languages = await new Promises( - projectConfig.languages.map(async (lang) => { + const languagesWithTranslations = projectConfig.languages.map( + async (lang) => { const translations = await store.getTranslations(lang); return { lang, translations }; - }), - ) + }, + ); + return { languagesWithTranslations, messages, name: project.name }; + }), + ) + .map(async (value) => { + const { name, messages, languagesWithTranslations } = value; + const languages = await new Promises(languagesWithTranslations) .map(({ lang, translations }) => { return { - href: `/projects/${project.name}/${lang}`, + href: `/projects/${name}/${lang}`, language: lang, progress: translations ? (Object.keys(translations).length / messages.length) * 100 @@ -49,13 +55,13 @@ export default async function Home() { .all(); return { - href: `/projects/${project.name}`, + href: `/projects/${name}`, languages, messageCount: messages.length, - name: project.name, + name, }; - }), - ); + }) + .all(); return ; } @@ -63,7 +69,7 @@ export default async function Home() { class Promises { constructor(private promises: Array>) {} - map(callbackfn: (value: T) => U): Promises { + map(callbackfn: (value: T) => U | Promise): Promises { return new Promises(this.promises.map((p) => p.then(callbackfn))); } From 27522a661d8fcfdee56759ac1ed138eed2560f8e Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Mon, 29 Jul 2024 23:08:18 +0200 Subject: [PATCH 04/14] Extract data access layer --- webapp/src/app/page.tsx | 28 +++------------------------- webapp/src/dataAccess.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 webapp/src/dataAccess.ts diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index ea8e91cc..a3c62a08 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -1,9 +1,6 @@ -import { Cache } from '@/Cache'; +import { accessProjects } from '@/dataAccess'; import HomeDashboard from '@/components/HomeDashboard'; -import MessageAdapterFactory from '@/utils/adapters/MessageAdapterFactory'; import { ProjectCardProps } from '@/components/ProjectCard'; -import { RepoGit } from '@/RepoGit'; -import { ServerConfig } from '@/utils/serverConfig'; // Force dynamic rendering for this page. By default Next.js attempts to render // this page statically. That means that it tries to render the page at build @@ -19,27 +16,8 @@ import { ServerConfig } from '@/utils/serverConfig'; export const dynamic = 'force-dynamic'; export default async function Home() { - const serverConfig = await ServerConfig.read(); - const projects = await new Promises( - serverConfig.projects.map(async (project) => { - await RepoGit.cloneIfNotExist(project); - const repoGit = await RepoGit.getRepoGit(project); - const lyraConfig = await repoGit.getLyraConfig(); - const projectConfig = lyraConfig.getProjectConfigByPath( - project.projectPath, - ); - const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); - const messages = await msgAdapter.getMessages(); - const store = await Cache.getProjectStore(projectConfig); - const languagesWithTranslations = projectConfig.languages.map( - async (lang) => { - const translations = await store.getTranslations(lang); - return { lang, translations }; - }, - ); - return { languagesWithTranslations, messages, name: project.name }; - }), - ) + const projectData = await accessProjects(); + const projects = await new Promises(projectData) .map(async (value) => { const { name, messages, languagesWithTranslations } = value; const languages = await new Promises(languagesWithTranslations) diff --git a/webapp/src/dataAccess.ts b/webapp/src/dataAccess.ts new file mode 100644 index 00000000..ba7a1a70 --- /dev/null +++ b/webapp/src/dataAccess.ts @@ -0,0 +1,26 @@ +import { Cache } from '@/Cache'; +import MessageAdapterFactory from '@/utils/adapters/MessageAdapterFactory'; +import { RepoGit } from '@/RepoGit'; +import { ServerConfig } from '@/utils/serverConfig'; + +export async function accessProjects() { + const serverConfig = await ServerConfig.read(); + return serverConfig.projects.map(async (project) => { + await RepoGit.cloneIfNotExist(project); + const repoGit = await RepoGit.getRepoGit(project); + const lyraConfig = await repoGit.getLyraConfig(); + const projectConfig = lyraConfig.getProjectConfigByPath( + project.projectPath, + ); + const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); + const messages = await msgAdapter.getMessages(); + const store = await Cache.getProjectStore(projectConfig); + const languagesWithTranslations = projectConfig.languages.map( + async (lang) => { + const translations = await store.getTranslations(lang); + return { lang, translations }; + }, + ); + return { languagesWithTranslations, messages, name: project.name }; + }); +} From 70387f6347f3f712dcfaeefc95e81ff4ed5807b5 Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Mon, 29 Jul 2024 23:15:15 +0200 Subject: [PATCH 05/14] Refactor a little * Destructure parameter directly * Infer type variable to fit long line --- webapp/src/app/page.tsx | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index a3c62a08..8e238dc0 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -1,6 +1,5 @@ import { accessProjects } from '@/dataAccess'; import HomeDashboard from '@/components/HomeDashboard'; -import { ProjectCardProps } from '@/components/ProjectCard'; // Force dynamic rendering for this page. By default Next.js attempts to render // this page statically. That means that it tries to render the page at build @@ -18,8 +17,7 @@ export const dynamic = 'force-dynamic'; export default async function Home() { const projectData = await accessProjects(); const projects = await new Promises(projectData) - .map(async (value) => { - const { name, messages, languagesWithTranslations } = value; + .map(async ({ name, messages, languagesWithTranslations }) => { const languages = await new Promises(languagesWithTranslations) .map(({ lang, translations }) => { return { From 110667812994d38eeb1f525ae803cc121ef1a285 Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Mon, 29 Jul 2024 23:59:35 +0200 Subject: [PATCH 06/14] Refactor a little Use expression body instead of block body. --- webapp/src/app/page.tsx | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index 8e238dc0..9ce5ea9a 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -19,15 +19,13 @@ export default async function Home() { const projects = await new Promises(projectData) .map(async ({ name, messages, languagesWithTranslations }) => { const languages = await new Promises(languagesWithTranslations) - .map(({ lang, translations }) => { - return { - href: `/projects/${name}/${lang}`, - language: lang, - progress: translations - ? (Object.keys(translations).length / messages.length) * 100 - : 0, - }; - }) + .map(({ lang, translations }) => ({ + href: `/projects/${name}/${lang}`, + language: lang, + progress: translations + ? (Object.keys(translations).length / messages.length) * 100 + : 0, + })) .all(); return { From 675c824ec18d8a35841cc8536e367b26bf7ec695 Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Tue, 30 Jul 2024 00:35:00 +0200 Subject: [PATCH 07/14] Extract languagesWithTranslations --- webapp/src/app/page.tsx | 13 +------------ webapp/src/app/projects/[projectName]/page.tsx | 14 ++++++++++---- webapp/src/utils/Promises.ts | 11 +++++++++++ 3 files changed, 22 insertions(+), 16 deletions(-) create mode 100644 webapp/src/utils/Promises.ts diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index 9ce5ea9a..85cbcd43 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -1,4 +1,5 @@ import { accessProjects } from '@/dataAccess'; +import { Promises } from '@/utils/Promises'; import HomeDashboard from '@/components/HomeDashboard'; // Force dynamic rendering for this page. By default Next.js attempts to render @@ -39,15 +40,3 @@ export default async function Home() { return ; } - -class Promises { - constructor(private promises: Array>) {} - - map(callbackfn: (value: T) => U | Promise): Promises { - return new Promises(this.promises.map((p) => p.then(callbackfn))); - } - - all() { - return Promise.all(this.promises); - } -} diff --git a/webapp/src/app/projects/[projectName]/page.tsx b/webapp/src/app/projects/[projectName]/page.tsx index 24c30d71..c79c50cc 100644 --- a/webapp/src/app/projects/[projectName]/page.tsx +++ b/webapp/src/app/projects/[projectName]/page.tsx @@ -2,6 +2,7 @@ import { NextPage } from 'next'; import { notFound } from 'next/navigation'; import { Cache } from '@/Cache'; +import { Promises } from '@/utils/Promises'; import MessageAdapterFactory from '@/utils/adapters/MessageAdapterFactory'; import ProjectDashboard from '@/components/ProjectDashboard'; import { RepoGit } from '@/RepoGit'; @@ -26,9 +27,14 @@ const ProjectPage: NextPage<{ const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); const messages = await msgAdapter.getMessages(); const store = await Cache.getProjectStore(projectConfig); - const languages = await Promise.all( - projectConfig.languages.map(async (lang) => { + const languagesWithTranslations = projectConfig.languages.map( + async (lang) => { const translations = await store.getTranslations(lang); + return { lang, translations }; + }, + ); + const languages = await new Promises(languagesWithTranslations) + .map(({ lang, translations }) => { return { href: `/projects/${project.name}/${lang}`, language: lang, @@ -37,8 +43,8 @@ const ProjectPage: NextPage<{ ? (Object.keys(translations).length / messages.length) * 100 : 0, }; - }), - ); + }) + .all(); return ( { + constructor(private promises: Array>) {} + + map(callbackfn: (value: T) => U | Promise): Promises { + return new Promises(this.promises.map((p) => p.then(callbackfn))); + } + + all() { + return Promise.all(this.promises); + } +} From f0b466427152c5e4586d202d31a72c7eab9d25bf Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Tue, 30 Jul 2024 00:39:48 +0200 Subject: [PATCH 08/14] Extract constans for project name --- webapp/src/app/projects/[projectName]/page.tsx | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/webapp/src/app/projects/[projectName]/page.tsx b/webapp/src/app/projects/[projectName]/page.tsx index c79c50cc..9c4be7a5 100644 --- a/webapp/src/app/projects/[projectName]/page.tsx +++ b/webapp/src/app/projects/[projectName]/page.tsx @@ -11,9 +11,11 @@ import { ServerConfig } from '@/utils/serverConfig'; const ProjectPage: NextPage<{ params: { projectName: string }; }> = async ({ params }) => { + const projectNameParam = params.projectName; + const serverConfig = await ServerConfig.read(); const project = serverConfig.projects.find( - (project) => project.name === params.projectName, + (project) => project.name === projectNameParam, ); if (!project) { @@ -33,10 +35,11 @@ const ProjectPage: NextPage<{ return { lang, translations }; }, ); + const projectName = project.name; const languages = await new Promises(languagesWithTranslations) .map(({ lang, translations }) => { return { - href: `/projects/${project.name}/${lang}`, + href: `/projects/${projectName}/${lang}`, language: lang, messagesLeft: messages.length - Object.keys(translations).length, progress: translations @@ -50,7 +53,7 @@ const ProjectPage: NextPage<{ ); }; From 2d2ae600690357ab015986b589a971a36739d0f4 Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Tue, 30 Jul 2024 00:48:58 +0200 Subject: [PATCH 09/14] Extract accessProject --- .../src/app/projects/[projectName]/page.tsx | 27 +++---------------- webapp/src/dataAccess.ts | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/webapp/src/app/projects/[projectName]/page.tsx b/webapp/src/app/projects/[projectName]/page.tsx index 9c4be7a5..5c1a06f3 100644 --- a/webapp/src/app/projects/[projectName]/page.tsx +++ b/webapp/src/app/projects/[projectName]/page.tsx @@ -1,41 +1,20 @@ import { NextPage } from 'next'; import { notFound } from 'next/navigation'; -import { Cache } from '@/Cache'; +import { accessProject } from '@/dataAccess'; import { Promises } from '@/utils/Promises'; -import MessageAdapterFactory from '@/utils/adapters/MessageAdapterFactory'; import ProjectDashboard from '@/components/ProjectDashboard'; -import { RepoGit } from '@/RepoGit'; -import { ServerConfig } from '@/utils/serverConfig'; const ProjectPage: NextPage<{ params: { projectName: string }; }> = async ({ params }) => { const projectNameParam = params.projectName; - const serverConfig = await ServerConfig.read(); - const project = serverConfig.projects.find( - (project) => project.name === projectNameParam, - ); - + const project = await accessProject(projectNameParam); if (!project) { return notFound(); } - - await RepoGit.cloneIfNotExist(project); - const repoGit = await RepoGit.getRepoGit(project); - const lyraConfig = await repoGit.getLyraConfig(); - const projectConfig = lyraConfig.getProjectConfigByPath(project.projectPath); - const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); - const messages = await msgAdapter.getMessages(); - const store = await Cache.getProjectStore(projectConfig); - const languagesWithTranslations = projectConfig.languages.map( - async (lang) => { - const translations = await store.getTranslations(lang); - return { lang, translations }; - }, - ); - const projectName = project.name; + const { projectName, messages, languagesWithTranslations } = project; const languages = await new Promises(languagesWithTranslations) .map(({ lang, translations }) => { return { diff --git a/webapp/src/dataAccess.ts b/webapp/src/dataAccess.ts index ba7a1a70..8491c647 100644 --- a/webapp/src/dataAccess.ts +++ b/webapp/src/dataAccess.ts @@ -24,3 +24,30 @@ export async function accessProjects() { return { languagesWithTranslations, messages, name: project.name }; }); } + +export async function accessProject(projectNameParam: string) { + const serverConfig = await ServerConfig.read(); + const project = serverConfig.projects.find( + (project) => project.name === projectNameParam, + ); + + if (!project) { + return null; + } + + await RepoGit.cloneIfNotExist(project); + const repoGit = await RepoGit.getRepoGit(project); + const lyraConfig = await repoGit.getLyraConfig(); + const projectConfig = lyraConfig.getProjectConfigByPath(project.projectPath); + const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); + const messages = await msgAdapter.getMessages(); + const store = await Cache.getProjectStore(projectConfig); + const languagesWithTranslations = projectConfig.languages.map( + async (lang) => { + const translations = await store.getTranslations(lang); + return { lang, translations }; + }, + ); + const projectName = project.name; + return { languagesWithTranslations, messages, projectName }; +} From 34b962714e9b2bf46f690b003f18a4474f18466a Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Tue, 30 Jul 2024 00:55:59 +0200 Subject: [PATCH 10/14] Refactor a little * Shorter parameter and property names * Inline constants * Use expression body --- .../src/app/projects/[projectName]/page.tsx | 26 ++++++++----------- webapp/src/dataAccess.ts | 7 +++-- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/webapp/src/app/projects/[projectName]/page.tsx b/webapp/src/app/projects/[projectName]/page.tsx index 5c1a06f3..32d14e5f 100644 --- a/webapp/src/app/projects/[projectName]/page.tsx +++ b/webapp/src/app/projects/[projectName]/page.tsx @@ -8,31 +8,27 @@ import ProjectDashboard from '@/components/ProjectDashboard'; const ProjectPage: NextPage<{ params: { projectName: string }; }> = async ({ params }) => { - const projectNameParam = params.projectName; - - const project = await accessProject(projectNameParam); + const project = await accessProject(params.projectName); if (!project) { return notFound(); } - const { projectName, messages, languagesWithTranslations } = project; + const { name, messages, languagesWithTranslations } = project; const languages = await new Promises(languagesWithTranslations) - .map(({ lang, translations }) => { - return { - href: `/projects/${projectName}/${lang}`, - language: lang, - messagesLeft: messages.length - Object.keys(translations).length, - progress: translations - ? (Object.keys(translations).length / messages.length) * 100 - : 0, - }; - }) + .map(({ lang, translations }) => ({ + href: `/projects/${name}/${lang}`, + language: lang, + messagesLeft: messages.length - Object.keys(translations).length, + progress: translations + ? (Object.keys(translations).length / messages.length) * 100 + : 0, + })) .all(); return ( ); }; diff --git a/webapp/src/dataAccess.ts b/webapp/src/dataAccess.ts index 8491c647..9bc102ba 100644 --- a/webapp/src/dataAccess.ts +++ b/webapp/src/dataAccess.ts @@ -25,10 +25,10 @@ export async function accessProjects() { }); } -export async function accessProject(projectNameParam: string) { +export async function accessProject(name: string) { const serverConfig = await ServerConfig.read(); const project = serverConfig.projects.find( - (project) => project.name === projectNameParam, + (project) => project.name === name, ); if (!project) { @@ -48,6 +48,5 @@ export async function accessProject(projectNameParam: string) { return { lang, translations }; }, ); - const projectName = project.name; - return { languagesWithTranslations, messages, projectName }; + return { languagesWithTranslations, messages, name: project.name }; } From 3beb4c0e386a344f411365ae5ba0607f08a4514d Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Tue, 30 Jul 2024 01:02:41 +0200 Subject: [PATCH 11/14] Extract readProject --- webapp/src/dataAccess.ts | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/webapp/src/dataAccess.ts b/webapp/src/dataAccess.ts index 9bc102ba..35040d4c 100644 --- a/webapp/src/dataAccess.ts +++ b/webapp/src/dataAccess.ts @@ -1,27 +1,12 @@ import { Cache } from '@/Cache'; import MessageAdapterFactory from '@/utils/adapters/MessageAdapterFactory'; import { RepoGit } from '@/RepoGit'; -import { ServerConfig } from '@/utils/serverConfig'; +import { ServerConfig, ServerProjectConfig } from '@/utils/serverConfig'; export async function accessProjects() { const serverConfig = await ServerConfig.read(); return serverConfig.projects.map(async (project) => { - await RepoGit.cloneIfNotExist(project); - const repoGit = await RepoGit.getRepoGit(project); - const lyraConfig = await repoGit.getLyraConfig(); - const projectConfig = lyraConfig.getProjectConfigByPath( - project.projectPath, - ); - const msgAdapter = MessageAdapterFactory.createAdapter(projectConfig); - const messages = await msgAdapter.getMessages(); - const store = await Cache.getProjectStore(projectConfig); - const languagesWithTranslations = projectConfig.languages.map( - async (lang) => { - const translations = await store.getTranslations(lang); - return { lang, translations }; - }, - ); - return { languagesWithTranslations, messages, name: project.name }; + return await readProject(project); }); } @@ -35,6 +20,10 @@ export async function accessProject(name: string) { return null; } + return readProject(project); +} + +async function readProject(project: ServerProjectConfig) { await RepoGit.cloneIfNotExist(project); const repoGit = await RepoGit.getRepoGit(project); const lyraConfig = await repoGit.getLyraConfig(); From e296f737834e62497c9c1370d758d9424835cbe2 Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:50:00 +0200 Subject: [PATCH 12/14] Rename parameter MDN uses a captial F in its name for the parameter. --- webapp/src/utils/Promises.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webapp/src/utils/Promises.ts b/webapp/src/utils/Promises.ts index fd0e0642..df08cce1 100644 --- a/webapp/src/utils/Promises.ts +++ b/webapp/src/utils/Promises.ts @@ -1,8 +1,8 @@ export class Promises { constructor(private promises: Array>) {} - map(callbackfn: (value: T) => U | Promise): Promises { - return new Promises(this.promises.map((p) => p.then(callbackfn))); + map(callbackFn: (value: T) => U | Promise): Promises { + return new Promises(this.promises.map((p) => p.then(callbackFn))); } all() { From e5486f7f0e5bcf64f419c8f8377970b58d32a42c Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Sun, 18 Aug 2024 16:59:09 +0200 Subject: [PATCH 13/14] Wrap constructor in factory Instantiating objects with new can be a little ambigious. For example new X().Y() is either creating a new instance with X as a constructor or with Y as a constructor. Being nameable, factory functions are also better than constructors when you want to offer multiple different signatures for creating instances. --- webapp/src/app/page.tsx | 4 ++-- webapp/src/app/projects/[projectName]/page.tsx | 2 +- webapp/src/utils/Promises.ts | 6 +++++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/webapp/src/app/page.tsx b/webapp/src/app/page.tsx index 85cbcd43..643b3e3c 100644 --- a/webapp/src/app/page.tsx +++ b/webapp/src/app/page.tsx @@ -17,9 +17,9 @@ export const dynamic = 'force-dynamic'; export default async function Home() { const projectData = await accessProjects(); - const projects = await new Promises(projectData) + const projects = await Promises.of(projectData) .map(async ({ name, messages, languagesWithTranslations }) => { - const languages = await new Promises(languagesWithTranslations) + const languages = await Promises.of(languagesWithTranslations) .map(({ lang, translations }) => ({ href: `/projects/${name}/${lang}`, language: lang, diff --git a/webapp/src/app/projects/[projectName]/page.tsx b/webapp/src/app/projects/[projectName]/page.tsx index 32d14e5f..a660d648 100644 --- a/webapp/src/app/projects/[projectName]/page.tsx +++ b/webapp/src/app/projects/[projectName]/page.tsx @@ -13,7 +13,7 @@ const ProjectPage: NextPage<{ return notFound(); } const { name, messages, languagesWithTranslations } = project; - const languages = await new Promises(languagesWithTranslations) + const languages = await Promises.of(languagesWithTranslations) .map(({ lang, translations }) => ({ href: `/projects/${name}/${lang}`, language: lang, diff --git a/webapp/src/utils/Promises.ts b/webapp/src/utils/Promises.ts index df08cce1..0aaf6917 100644 --- a/webapp/src/utils/Promises.ts +++ b/webapp/src/utils/Promises.ts @@ -1,5 +1,9 @@ export class Promises { - constructor(private promises: Array>) {} + private constructor(private promises: Array>) {} + + static of(array: Array>) { + return new Promises(array); + } map(callbackFn: (value: T) => U | Promise): Promises { return new Promises(this.promises.map((p) => p.then(callbackFn))); From 034bc7e95e4a5aebfef4288a601ac131b9ddbd7d Mon Sep 17 00:00:00 2001 From: WULCAN <7813515+WULCAN@users.noreply.github.com> Date: Sun, 18 Aug 2024 17:08:27 +0200 Subject: [PATCH 14/14] Rename field It is more important to highlight that the field has a different type than that the field too contains promises. --- webapp/src/utils/Promises.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webapp/src/utils/Promises.ts b/webapp/src/utils/Promises.ts index 0aaf6917..792da728 100644 --- a/webapp/src/utils/Promises.ts +++ b/webapp/src/utils/Promises.ts @@ -1,15 +1,15 @@ export class Promises { - private constructor(private promises: Array>) {} + private constructor(private array: Array>) {} static of(array: Array>) { return new Promises(array); } map(callbackFn: (value: T) => U | Promise): Promises { - return new Promises(this.promises.map((p) => p.then(callbackFn))); + return new Promises(this.array.map((p) => p.then(callbackFn))); } all() { - return Promise.all(this.promises); + return Promise.all(this.array); } }