From d51e7a37a0d11888052aaeb00418db0821add497 Mon Sep 17 00:00:00 2001 From: ochairo <19258508+ochairo@users.noreply.github.com> Date: Tue, 26 Mar 2024 17:31:42 +0900 Subject: [PATCH] refactor: Router --- src/app/infrastructure/router/router.ts | 10 +++++++--- src/app/main.ts | 4 +--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/app/infrastructure/router/router.ts b/src/app/infrastructure/router/router.ts index 19e4eaf..a5f9d84 100644 --- a/src/app/infrastructure/router/router.ts +++ b/src/app/infrastructure/router/router.ts @@ -9,10 +9,14 @@ class Router { ) {} /** - * Router navigation method - * @returns Promise + * Initializes the router by adding event listeners and rendering the initial module. */ - async navigate(): Promise { + init(): void { + window.addEventListener("popstate", () => this.navigate()); + document.addEventListener("DOMContentLoaded", () => this.navigate()); + } + + private async navigate(): Promise { try { const currentPath = this.getSanitizedPath(window.location.pathname); const matchedRoute = await this.findMatchingRoute( diff --git a/src/app/main.ts b/src/app/main.ts index 81b934f..f61bbe0 100644 --- a/src/app/main.ts +++ b/src/app/main.ts @@ -3,6 +3,4 @@ import { routesConfig } from "./routes"; const rootElement = document.getElementById("root")!; const router = new Router(routesConfig, rootElement); - -window.addEventListener("popstate", () => router.navigate()); -document.addEventListener("DOMContentLoaded", () => router.navigate()); +router.init();