Skip to content

Commit

Permalink
feat: unsubscribe on destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jun 25, 2024
1 parent 2247344 commit 01be280
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions libs/angular-accelerator/src/lib/services/breadcrumb.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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'
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<MenuItem[]>([])
generatedItemsSource = new BehaviorSubject<MenuItem[]>([])

Expand All @@ -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) {
Expand All @@ -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)
Expand Down

0 comments on commit 01be280

Please sign in to comment.