Skip to content

Commit

Permalink
refactor: Router
Browse files Browse the repository at this point in the history
  • Loading branch information
ochairo committed Mar 26, 2024
1 parent f3acb7b commit d51e7a3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/app/infrastructure/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ class Router {
) {}

/**
* Router navigation method
* @returns Promise<void>
* Initializes the router by adding event listeners and rendering the initial module.
*/
async navigate(): Promise<void> {
init(): void {
window.addEventListener("popstate", () => this.navigate());
document.addEventListener("DOMContentLoaded", () => this.navigate());
}

private async navigate(): Promise<void> {
try {
const currentPath = this.getSanitizedPath(window.location.pathname);
const matchedRoute = await this.findMatchingRoute(
Expand Down
4 changes: 1 addition & 3 deletions src/app/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit d51e7a3

Please sign in to comment.