-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use lazy injection token for router listener (#913)
- Loading branch information
Showing
3 changed files
with
39 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 35 additions & 35 deletions
70
projects/ngx-meta/src/routing/src/listener/router-listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,51 @@ | ||
import { inject, InjectionToken, OnDestroy } from '@angular/core' | ||
import { inject, OnDestroy } from '@angular/core' | ||
import { NavigationEnd, Router } from '@angular/router' | ||
import { filter, Subscription } from 'rxjs' | ||
import { _formatDevMessage, NgxMetaService } from '@davidlj95/ngx-meta/core' | ||
import { | ||
_formatDevMessage, | ||
_makeInjectionToken, | ||
NgxMetaService, | ||
} from '@davidlj95/ngx-meta/core' | ||
import { MODULE_NAME } from '../module-name' | ||
|
||
// WTF is this? Why not just import `EventType`? Well, compatibility reasons 🙃 | ||
// See https://github.com/davidlj95/ngx/pull/246 for the details | ||
const NAVIGATION_END_EVENT_TYPE = new NavigationEnd(0, '', '').type | ||
|
||
export const ROUTER_LISTENER = new InjectionToken<RouterListener>( | ||
ngDevMode ? 'NgxMeta Router listener' : 'NgxMetaRL', | ||
{ | ||
factory: () => { | ||
const router = inject(Router) | ||
const ngxMetaService = inject(NgxMetaService) | ||
let subscription: Subscription | undefined | ||
return { | ||
listen() { | ||
ngDevMode && | ||
subscription && | ||
// https://github.com/istanbuljs/istanbuljs/issues/719 | ||
// prettier-ignore | ||
console.warn( | ||
export const routerListener = () => | ||
_makeInjectionToken(ngDevMode ? 'Router listener' : 'RL', () => { | ||
const router = inject(Router) | ||
const ngxMetaService = inject(NgxMetaService) | ||
let subscription: Subscription | undefined | ||
return { | ||
listen() { | ||
ngDevMode && | ||
subscription && | ||
// https://github.com/istanbuljs/istanbuljs/issues/719 | ||
// prettier-ignore | ||
console.warn( | ||
_formatDevMessage( | ||
[ | ||
'prevented listening for route changes twice', | ||
'Ensure routing provider or module is only imported once', | ||
].join('\n'), | ||
{ module: MODULE_NAME }, | ||
)) | ||
/* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */ | ||
if (subscription) { | ||
return | ||
} | ||
subscription = router.events | ||
.pipe(filter(({ type }) => type === NAVIGATION_END_EVENT_TYPE)) | ||
.subscribe({ | ||
next: () => ngxMetaService.set(), | ||
}) | ||
}, | ||
// Can't use DestroyRef is introduced in Angular v16 | ||
// https://github.com/angular/angular/pull/49158 | ||
// It uses EnvironmentInjector's internal `onDestroy` under the hood | ||
// Tried that too, but it's @internal + raises some errors. So back to the `ngOnDestroy` | ||
ngOnDestroy: () => subscription?.unsubscribe(), | ||
} | ||
}, | ||
}, | ||
) | ||
/* istanbul ignore next https://github.com/istanbuljs/istanbuljs/issues/719 */ | ||
if (subscription) { | ||
return | ||
} | ||
subscription = router.events | ||
.pipe(filter(({ type }) => type === NAVIGATION_END_EVENT_TYPE)) | ||
.subscribe({ | ||
next: () => ngxMetaService.set(), | ||
}) | ||
}, | ||
// Can't use DestroyRef is introduced in Angular v16 | ||
// https://github.com/angular/angular/pull/49158 | ||
// It uses EnvironmentInjector's internal `onDestroy` under the hood | ||
// Tried that too, but it's @internal + raises some errors. So back to the `ngOnDestroy` | ||
ngOnDestroy: () => subscription?.unsubscribe(), | ||
} | ||
}) | ||
export type RouterListener = { listen: () => void } & OnDestroy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters