From 886cca028f31ed9dbaed97e7116a15f78b76cb6f Mon Sep 17 00:00:00 2001 From: Dzmitry Shylovich Date: Wed, 5 Apr 2017 02:00:40 +0300 Subject: [PATCH] refactor(router): misc refactoring --- packages/router/src/apply_redirects.ts | 4 ++-- packages/router/src/config.ts | 12 ++++++++---- packages/router/src/router.ts | 4 ++-- packages/router/src/router_config_loader.ts | 6 +----- packages/router/src/router_preloader.ts | 16 ++++++++-------- packages/router/test/apply_redirects.spec.ts | 3 +-- tools/public_api_guard/router/router.d.ts | 2 +- 7 files changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/router/src/apply_redirects.ts b/packages/router/src/apply_redirects.ts index 3a801227ca..cee485c375 100644 --- a/packages/router/src/apply_redirects.ts +++ b/packages/router/src/apply_redirects.ts @@ -18,8 +18,8 @@ import {map} from 'rxjs/operator/map'; import {mergeMap} from 'rxjs/operator/mergeMap'; import {EmptyError} from 'rxjs/util/EmptyError'; -import {InternalRoute, Route, Routes} from './config'; -import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader'; +import {InternalRoute, LoadedRouterConfig, Route, Routes} from './config'; +import {RouterConfigLoader} from './router_config_loader'; import {PRIMARY_OUTLET, Params, defaultUrlMatcher, navigationCancelingError} from './shared'; import {UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree'; import {andObservables, forEach, waitForMap, wrapIntoObservable} from './utils/collection'; diff --git a/packages/router/src/config.ts b/packages/router/src/config.ts index 6e0207d952..b80bf3eb85 100644 --- a/packages/router/src/config.ts +++ b/packages/router/src/config.ts @@ -6,13 +6,11 @@ * found in the LICENSE file at https://angular.io/license */ -import {NgModuleFactory, Type} from '@angular/core'; +import {NgModuleFactory, NgModuleRef, Type} from '@angular/core'; import {Observable} from 'rxjs/Observable'; - import {PRIMARY_OUTLET} from './shared'; import {UrlSegment, UrlSegmentGroup} from './url_tree'; - /** * @whatItDoes Represents router configuration. * @@ -359,11 +357,17 @@ export interface Route { children?: Routes; loadChildren?: LoadChildren; runGuardsAndResolvers?: RunGuardsAndResolvers; + /** @internal */ + _loadedConfig?: LoadedRouterConfig; +} + +export class LoadedRouterConfig { + constructor(public routes: Route[], public module: NgModuleRef) {} } export interface InternalRoute extends Route { // `LoadedRouterConfig` loaded via a Route `loadChildren` - _loadedConfig?: any; + _loadedConfig?: LoadedRouterConfig; } export function validateConfig(config: Routes, parentPath: string = ''): void { diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index d4ad6aa330..f059b39e67 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -22,14 +22,14 @@ import {mergeMap} from 'rxjs/operator/mergeMap'; import {reduce} from 'rxjs/operator/reduce'; import {applyRedirects} from './apply_redirects'; -import {InternalRoute, QueryParamsHandling, ResolveData, Route, Routes, RunGuardsAndResolvers, validateConfig} from './config'; +import {InternalRoute, LoadedRouterConfig, QueryParamsHandling, ResolveData, Route, Routes, RunGuardsAndResolvers, validateConfig} from './config'; import {createRouterState} from './create_router_state'; import {createUrlTree} from './create_url_tree'; import {RouterOutlet} from './directives/router_outlet'; import {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events'; import {recognize} from './recognize'; import {DetachedRouteHandle, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy'; -import {LoadedRouterConfig, RouterConfigLoader} from './router_config_loader'; +import {RouterConfigLoader} from './router_config_loader'; import {RouterOutletMap} from './router_outlet_map'; import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState, equalParamsAndUrlSegments, inheritedParamsDataResolve} from './router_state'; import {PRIMARY_OUTLET, Params, isNavigationCancelingError} from './shared'; diff --git a/packages/router/src/router_config_loader.ts b/packages/router/src/router_config_loader.ts index 943ea62111..b91bd28b5c 100644 --- a/packages/router/src/router_config_loader.ts +++ b/packages/router/src/router_config_loader.ts @@ -12,7 +12,7 @@ import {fromPromise} from 'rxjs/observable/fromPromise'; import {of } from 'rxjs/observable/of'; import {map} from 'rxjs/operator/map'; import {mergeMap} from 'rxjs/operator/mergeMap'; -import {LoadChildren, Route} from './config'; +import {LoadChildren, LoadedRouterConfig, Route} from './config'; import {flatten, wrapIntoObservable} from './utils/collection'; /** @@ -21,10 +21,6 @@ import {flatten, wrapIntoObservable} from './utils/collection'; */ export const ROUTES = new InjectionToken('ROUTES'); -export class LoadedRouterConfig { - constructor(public routes: Route[], public module: NgModuleRef) {} -} - export class RouterConfigLoader { constructor( private loader: NgModuleFactoryLoader, private compiler: Compiler, diff --git a/packages/router/src/router_preloader.ts b/packages/router/src/router_preloader.ts index c8ed0a2f0f..7c897c87f8 100644 --- a/packages/router/src/router_preloader.ts +++ b/packages/router/src/router_preloader.ts @@ -6,7 +6,7 @@ *found in the LICENSE file at https://angular.io/license */ -import {Compiler, Injectable, Injector, NgModuleFactoryLoader, NgModuleRef} from '@angular/core'; +import {Compiler, Injectable, Injector, NgModuleFactoryLoader, NgModuleRef, OnDestroy} from '@angular/core'; import {Observable} from 'rxjs/Observable'; import {Subscription} from 'rxjs/Subscription'; import {from} from 'rxjs/observable/from'; @@ -16,8 +16,8 @@ import {concatMap} from 'rxjs/operator/concatMap'; import {filter} from 'rxjs/operator/filter'; import {mergeAll} from 'rxjs/operator/mergeAll'; import {mergeMap} from 'rxjs/operator/mergeMap'; -import {InternalRoute, Route, Routes} from './config'; -import {NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart} from './events'; +import {InternalRoute, LoadedRouterConfig, Route, Routes} from './config'; +import {Event, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart} from './events'; import {Router} from './router'; import {RouterConfigLoader} from './router_config_loader'; @@ -73,7 +73,7 @@ export class NoPreloading implements PreloadingStrategy { * @stable */ @Injectable() -export class RouterPreloader { +export class RouterPreloader implements OnDestroy { private loader: RouterConfigLoader; private subscription: Subscription; @@ -87,8 +87,8 @@ export class RouterPreloader { }; setUpPreloading(): void { - const navigations = filter.call(this.router.events, (e: any) => e instanceof NavigationEnd); - this.subscription = concatMap.call(navigations, () => this.preload()).subscribe(() => {}); + const navigations$ = filter.call(this.router.events, (e: Event) => e instanceof NavigationEnd); + this.subscription = concatMap.call(navigations$, () => this.preload()).subscribe(() => {}); } preload(): Observable { @@ -121,8 +121,8 @@ export class RouterPreloader { private preloadConfig(ngModule: NgModuleRef, route: InternalRoute): Observable { return this.preloadingStrategy.preload(route, () => { - const loaded = this.loader.load(ngModule.injector, route); - return mergeMap.call(loaded, (config: any): any => { + const loaded$ = this.loader.load(ngModule.injector, route); + return mergeMap.call(loaded$, (config: LoadedRouterConfig) => { route._loadedConfig = config; return this.processRoutes(config.module, config.routes); }); diff --git a/packages/router/test/apply_redirects.spec.ts b/packages/router/test/apply_redirects.spec.ts index 4212f203f8..fb06c2d0cd 100644 --- a/packages/router/test/apply_redirects.spec.ts +++ b/packages/router/test/apply_redirects.spec.ts @@ -12,8 +12,7 @@ import {Observable} from 'rxjs/Observable'; import {of } from 'rxjs/observable/of'; import {applyRedirects} from '../src/apply_redirects'; -import {Routes} from '../src/config'; -import {LoadedRouterConfig} from '../src/router_config_loader'; +import {LoadedRouterConfig, Routes} from '../src/config'; import {DefaultUrlSerializer, UrlSegmentGroup, UrlTree, equalSegments} from '../src/url_tree'; describe('applyRedirects', () => { diff --git a/tools/public_api_guard/router/router.d.ts b/tools/public_api_guard/router/router.d.ts index 80b574e659..0387427f58 100644 --- a/tools/public_api_guard/router/router.d.ts +++ b/tools/public_api_guard/router/router.d.ts @@ -354,7 +354,7 @@ export declare class RouterOutletMap { } /** @stable */ -export declare class RouterPreloader { +export declare class RouterPreloader implements OnDestroy { constructor(router: Router, moduleLoader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, preloadingStrategy: PreloadingStrategy); ngOnDestroy(): void; preload(): Observable;