From 8edcac597a412ea9de38544801d2a872fd644d1b Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Wed, 9 Sep 2020 12:46:42 +0300 Subject: [PATCH] refactor: move reloadRoute function to route-utils.ts --- .../services/replaceable-components.service.ts | 17 ++--------------- .../packages/core/src/lib/utils/route-utils.ts | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/npm/ng-packs/packages/core/src/lib/services/replaceable-components.service.ts b/npm/ng-packs/packages/core/src/lib/services/replaceable-components.service.ts index fd04c1c511..7dfd1e5062 100644 --- a/npm/ng-packs/packages/core/src/lib/services/replaceable-components.service.ts +++ b/npm/ng-packs/packages/core/src/lib/services/replaceable-components.service.ts @@ -5,6 +5,7 @@ import { BehaviorSubject, Observable } from 'rxjs'; import { noop } from '../utils/common-utils'; import { map, filter } from 'rxjs/operators'; import { InternalStore } from '../utils/internal-store-utils'; +import { reloadRoute } from '../utils/route-utils'; @Injectable({ providedIn: 'root' }) export class ReplaceableComponentsService { @@ -27,20 +28,6 @@ export class ReplaceableComponentsService { } // TODO: Create a shared service for route reload and more - private reloadRoute() { - const { shouldReuseRoute } = this.router.routeReuseStrategy; - const setRouteReuse = (reuse: typeof shouldReuseRoute) => { - this.router.routeReuseStrategy.shouldReuseRoute = reuse; - }; - - setRouteReuse(() => false); - this.router.navigated = false; - - this.ngZone.run(async () => { - await this.router.navigateByUrl(this.router.url).catch(noop); - setRouteReuse(shouldReuseRoute); - }); - } add(replaceableComponent: ReplaceableComponents.ReplaceableComponent, reload?: boolean): void { const replaceableComponents = [...this.store.state]; @@ -57,7 +44,7 @@ export class ReplaceableComponentsService { this.store.patch(replaceableComponents); - if (reload) this.reloadRoute(); + if (reload) reloadRoute(this.router, this.ngZone); } get(replaceableComponentKey: string): ReplaceableComponents.ReplaceableComponent { diff --git a/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts b/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts index a749090695..388e76cbf8 100644 --- a/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts +++ b/npm/ng-packs/packages/core/src/lib/utils/route-utils.ts @@ -2,6 +2,8 @@ import { PRIMARY_OUTLET, Router, UrlSegmentGroup } from '@angular/router'; import { ABP } from '../models/common'; import { RoutesService } from '../services/routes.service'; import { TreeNode } from './tree-utils'; +import { noop } from './common-utils'; +import { NgZone } from '@angular/core'; export function findRoute(routes: RoutesService, path: string): TreeNode { const node = routes.find(route => route.path === path); @@ -23,3 +25,18 @@ export function getRoutePath(router: Router, url = router.url) { return '/' + (primaryGroup || emptyGroup).segments.map(({ path }) => path).join('/'); } + +export function reloadRoute(router: Router, ngZone: NgZone) { + const { shouldReuseRoute } = router.routeReuseStrategy; + const setRouteReuse = (reuse: typeof shouldReuseRoute) => { + router.routeReuseStrategy.shouldReuseRoute = reuse; + }; + + setRouteReuse(() => false); + router.navigated = false; + + ngZone.run(async () => { + await router.navigateByUrl(router.url).catch(noop); + setRouteReuse(shouldReuseRoute); + }); +}