From 01be280ce90e523ea7b158a9f071134dddc42f3b Mon Sep 17 00:00:00 2001 From: markuczy Date: Tue, 25 Jun 2024 16:10:28 +0200 Subject: [PATCH] feat: unsubscribe on destroy --- .../src/lib/services/breadcrumb.service.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts b/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts index f25cb622..b51b4e84 100644 --- a/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts +++ b/libs/angular-accelerator/src/lib/services/breadcrumb.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@angular/core' +import { Injectable, OnDestroy } from '@angular/core' import { ActivatedRoute, ActivatedRouteSnapshot, Data, NavigationEnd, ParamMap, Router } from '@angular/router' import { TranslateService } from '@ngx-translate/core' import { BehaviorSubject, filter } from 'rxjs' @@ -6,7 +6,7 @@ import { MenuItem } from 'primeng/api' import { BreadCrumbMenuItem } from '../model/breadcrumb-menu-item.model' @Injectable({ providedIn: 'any' }) -export class BreadcrumbService { +export class BreadcrumbService implements OnDestroy { private itemsSource = new BehaviorSubject([]) generatedItemsSource = new BehaviorSubject([]) @@ -19,6 +19,10 @@ export class BreadcrumbService { this.generateBreadcrumbs(root) }) } + ngOnDestroy(): void { + this.itemsSource.unsubscribe() + this.generatedItemsSource.unsubscribe() + } private generateBreadcrumbs(route: ActivatedRouteSnapshot | null) { if (route && route.url) { @@ -31,7 +35,8 @@ export class BreadcrumbService { ] const baseUrl: string[] = (route.data['mfeInfo'].baseHref as string).split('/').filter((value) => value) const parentUrl: string[] = route.url.map((url) => url.path) - if (!parentUrl.every((item) => baseUrl.includes(item))) { + const isUsingMatcher = !parentUrl.every((item) => baseUrl.includes(item)) + if (isUsingMatcher) { this.createBreadcrumb(route, parentUrl, breadcrumbs) } this.addBreadcrumb(route.firstChild, parentUrl, breadcrumbs)