diff --git a/projects/dynamic-menu/src/lib/dynamic-menu-extras.ts b/projects/dynamic-menu/src/lib/dynamic-menu-extras.ts index 44dd4e3..3b2972c 100644 --- a/projects/dynamic-menu/src/lib/dynamic-menu-extras.ts +++ b/projects/dynamic-menu/src/lib/dynamic-menu-extras.ts @@ -1,4 +1,4 @@ -import { InjectionToken, Provider } from '@angular/core'; +import { Inject, Injectable, InjectionToken, Provider } from '@angular/core'; /** * Extra configuration for {@link DynamicMenuModule} @@ -19,19 +19,30 @@ export interface DynamicMenuExtras { * DI token that hold extra configuration * @internal */ -export class DynamicMenuExtrasToken implements DynamicMenuExtras { - constructor(public listenForConfigChanges = false) {} +export const DynamicMenuExtrasToken = new InjectionToken( + 'DynamicMenuExtrasToken', +); + +/** + * @internal + */ +@Injectable({ providedIn: 'root' }) +export class DynamicMenuExtrasService implements DynamicMenuExtras { + listenForConfigChanges = this.extras.listenForConfigChanges; + + constructor( + @Inject(DynamicMenuExtrasToken) + private extras: DynamicMenuExtras = { listenForConfigChanges: false }, + ) {} } /** * Helper function to provide {@link DYNAMIC_MENU_EXTRAS_TOKEN} * @internal */ -export function provideDynamicMenuExtras( - extras: DynamicMenuExtras = {}, -): Provider { +export function provideDynamicMenuExtras(extras?: DynamicMenuExtras): Provider { return { provide: DynamicMenuExtrasToken, - useValue: new DynamicMenuExtrasToken(extras.listenForConfigChanges), + useValue: extras, }; } diff --git a/projects/dynamic-menu/src/lib/dynamic-menu.service.ts b/projects/dynamic-menu/src/lib/dynamic-menu.service.ts index 54edf51..10e21c2 100644 --- a/projects/dynamic-menu/src/lib/dynamic-menu.service.ts +++ b/projects/dynamic-menu/src/lib/dynamic-menu.service.ts @@ -19,7 +19,7 @@ import { takeUntil, } from 'rxjs/operators'; -import { DynamicMenuExtrasToken } from './dynamic-menu-extras'; +import { DynamicMenuExtrasService } from './dynamic-menu-extras'; import { DYNAMIC_MENU_ROUTES_TOKEN } from './dynamic-menu-routes'; import { SUB_MENU_MAP_TOKEN, SubMenuMap } from './sub-menu-map-provider'; import { @@ -58,7 +58,7 @@ export class DynamicMenuService implements OnDestroy { refCount(), ); - private configChanged$ = this.dynamicMenuExtrasToken.listenForConfigChanges + private configChanged$ = this.dynamicMenuExtrasService.listenForConfigChanges ? this.router.events.pipe(filter(e => e instanceof RouteConfigLoadEnd)) : EMPTY; @@ -103,7 +103,7 @@ export class DynamicMenuService implements OnDestroy { constructor( private injector: Injector, private router: Router, - private dynamicMenuExtrasToken: DynamicMenuExtrasToken, + private dynamicMenuExtrasService: DynamicMenuExtrasService, ) { this.dynamicMenu$.pipe(takeUntil(this.destroyed$)).subscribe(); }