From ea4afebeb930a720f32cfa689216c12e6eff9c70 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Tue, 11 Apr 2017 08:34:58 -0700 Subject: [PATCH] refactor(router): drop the `InternalRoute` interface --- packages/router/src/apply_redirects.ts | 7 +++--- packages/router/src/config.ts | 10 ++++---- packages/router/src/recognize.ts | 4 ++-- packages/router/src/router.ts | 6 ++--- packages/router/src/router_preloader.ts | 7 +++--- packages/router/test/apply_redirects.spec.ts | 24 +++++++++---------- packages/router/test/integration.spec.ts | 2 +- packages/router/test/router_preloader.spec.ts | 18 +++++++------- 8 files changed, 37 insertions(+), 41 deletions(-) diff --git a/packages/router/src/apply_redirects.ts b/packages/router/src/apply_redirects.ts index cee485c375..0805761d1b 100644 --- a/packages/router/src/apply_redirects.ts +++ b/packages/router/src/apply_redirects.ts @@ -18,7 +18,7 @@ import {map} from 'rxjs/operator/map'; import {mergeMap} from 'rxjs/operator/mergeMap'; import {EmptyError} from 'rxjs/util/EmptyError'; -import {InternalRoute, LoadedRouterConfig, Route, Routes} from './config'; +import {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'; @@ -247,7 +247,7 @@ class ApplyRedirects { } private matchSegmentAgainstRoute( - ngModule: NgModuleRef, rawSegmentGroup: UrlSegmentGroup, route: InternalRoute, + ngModule: NgModuleRef, rawSegmentGroup: UrlSegmentGroup, route: Route, segments: UrlSegment[]): Observable { if (route.path === '**') { if (route.loadChildren) { @@ -292,8 +292,7 @@ class ApplyRedirects { }); } - private getChildConfig(ngModule: NgModuleRef, route: InternalRoute): - Observable { + private getChildConfig(ngModule: NgModuleRef, route: Route): Observable { if (route.children) { // The children belong to the same module return of (new LoadedRouterConfig(route.children, ngModule)); diff --git a/packages/router/src/config.ts b/packages/router/src/config.ts index b80bf3eb85..de5ef3dbb0 100644 --- a/packages/router/src/config.ts +++ b/packages/router/src/config.ts @@ -357,7 +357,10 @@ export interface Route { children?: Routes; loadChildren?: LoadChildren; runGuardsAndResolvers?: RunGuardsAndResolvers; - /** @internal */ + /** + * Filled for routes with `loadChildren` once the module has been loaded + * @internal + */ _loadedConfig?: LoadedRouterConfig; } @@ -365,11 +368,6 @@ export class LoadedRouterConfig { constructor(public routes: Route[], public module: NgModuleRef) {} } -export interface InternalRoute extends Route { - // `LoadedRouterConfig` loaded via a Route `loadChildren` - _loadedConfig?: LoadedRouterConfig; -} - export function validateConfig(config: Routes, parentPath: string = ''): void { // forEach doesn't iterate undefined values for (let i = 0; i < config.length; i++) { diff --git a/packages/router/src/recognize.ts b/packages/router/src/recognize.ts index 169bd84484..684e2c6a1b 100644 --- a/packages/router/src/recognize.ts +++ b/packages/router/src/recognize.ts @@ -11,7 +11,7 @@ import {Observable} from 'rxjs/Observable'; import {Observer} from 'rxjs/Observer'; import {of } from 'rxjs/observable/of'; -import {Data, InternalRoute, ResolveData, Route, Routes} from './config'; +import {Data, ResolveData, Route, Routes} from './config'; import {ActivatedRouteSnapshot, RouterStateSnapshot, inheritedParamsDataResolve} from './router_state'; import {PRIMARY_OUTLET, defaultUrlMatcher} from './shared'; import {UrlSegment, UrlSegmentGroup, UrlTree, mapChildrenIntoArray} from './url_tree'; @@ -154,7 +154,7 @@ function sortActivatedRouteSnapshots(nodes: TreeNode[]): }); } -function getChildConfig(route: InternalRoute): Route[] { +function getChildConfig(route: Route): Route[] { if (route.children) { return route.children; } diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index f059b39e67..92a815e854 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -22,7 +22,7 @@ import {mergeMap} from 'rxjs/operator/mergeMap'; import {reduce} from 'rxjs/operator/reduce'; import {applyRedirects} from './apply_redirects'; -import {InternalRoute, LoadedRouterConfig, QueryParamsHandling, ResolveData, Route, Routes, RunGuardsAndResolvers, validateConfig} from './config'; +import {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'; @@ -1168,7 +1168,7 @@ function advanceActivatedRouteNodeAndItsChildren(node: TreeNode) function parentLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig { for (let s = snapshot.parent; s; s = s.parent) { - const route: InternalRoute = s._routeConfig; + const route = s._routeConfig; if (route && route._loadedConfig) return route._loadedConfig; if (route && route.component) return null; } @@ -1180,7 +1180,7 @@ function closestLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConf if (!snapshot) return null; for (let s = snapshot.parent; s; s = s.parent) { - const route: InternalRoute = s._routeConfig; + const route = s._routeConfig; if (route && route._loadedConfig) return route._loadedConfig; } diff --git a/packages/router/src/router_preloader.ts b/packages/router/src/router_preloader.ts index 7c897c87f8..a4a9177064 100644 --- a/packages/router/src/router_preloader.ts +++ b/packages/router/src/router_preloader.ts @@ -16,7 +16,7 @@ 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, LoadedRouterConfig, Route, Routes} from './config'; +import {LoadedRouterConfig, Route, Routes} from './config'; import {Event, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart} from './events'; import {Router} from './router'; import {RouterConfigLoader} from './router_config_loader'; @@ -100,8 +100,7 @@ export class RouterPreloader implements OnDestroy { private processRoutes(ngModule: NgModuleRef, routes: Routes): Observable { const res: Observable[] = []; - for (const r of routes) { - const route: InternalRoute = r; + for (const route of routes) { // we already have the config loaded, just recurse if (route.loadChildren && !route.canLoad && route._loadedConfig) { const childConfig = route._loadedConfig; @@ -119,7 +118,7 @@ export class RouterPreloader implements OnDestroy { return mergeAll.call(from(res)); } - private preloadConfig(ngModule: NgModuleRef, route: InternalRoute): Observable { + private preloadConfig(ngModule: NgModuleRef, route: Route): Observable { return this.preloadingStrategy.preload(route, () => { const loaded$ = this.loader.load(ngModule.injector, route); return mergeMap.call(loaded$, (config: LoadedRouterConfig) => { diff --git a/packages/router/test/apply_redirects.spec.ts b/packages/router/test/apply_redirects.spec.ts index fb06c2d0cd..5e1e044b1e 100644 --- a/packages/router/test/apply_redirects.spec.ts +++ b/packages/router/test/apply_redirects.spec.ts @@ -162,12 +162,12 @@ describe('applyRedirects', () => { return of (loadedConfig); } }; - const config = [{path: 'a', component: ComponentA, loadChildren: 'children'}]; + const config: Routes = [{path: 'a', component: ComponentA, loadChildren: 'children'}]; applyRedirects(testModule.injector, loader, serializer, tree('a/b'), config) .forEach(r => { compareTrees(r, tree('/a/b')); - expect((config[0])._loadedConfig).toBe(loadedConfig); + expect(config[0]._loadedConfig).toBe(loadedConfig); }); }); @@ -289,12 +289,12 @@ describe('applyRedirects', () => { const loader = {load: (injector: any, p: any) => of (loadedConfig)}; - const config = + const config: Routes = [{path: '', pathMatch: 'full', redirectTo: '/a'}, {path: 'a', loadChildren: 'children'}]; applyRedirects(testModule.injector, loader, serializer, tree(''), config).forEach(r => { compareTrees(r, tree('a')); - expect((config[1])._loadedConfig).toBe(loadedConfig); + expect(config[1]._loadedConfig).toBe(loadedConfig); }); }); @@ -310,7 +310,7 @@ describe('applyRedirects', () => { } }; - const config = [{path: 'a', loadChildren: 'children'}]; + const config: Routes = [{path: 'a', loadChildren: 'children'}]; applyRedirects(testModule.injector, loader, serializer, tree('a?k1'), config) .subscribe(r => {}); @@ -319,7 +319,7 @@ describe('applyRedirects', () => { .subscribe( r => { compareTrees(r, tree('a?k2')); - expect((config[0])._loadedConfig).toBe(loadedConfig); + expect(config[0]._loadedConfig).toBe(loadedConfig); }, (e) => { throw 'Should not reach'; }); }); @@ -329,10 +329,10 @@ describe('applyRedirects', () => { const loader = {load: (injector: any, p: any) => of (loadedConfig)}; - const config = [{path: '**', loadChildren: 'children'}]; + const config: Routes = [{path: '**', loadChildren: 'children'}]; applyRedirects(testModule.injector, loader, serializer, tree('xyz'), config) - .forEach(r => { expect((config[0])._loadedConfig).toBe(loadedConfig); }); + .forEach(r => { expect(config[0]._loadedConfig).toBe(loadedConfig); }); }); it('should load the configuration after a local redirect from a wildcard route', () => { @@ -340,11 +340,11 @@ describe('applyRedirects', () => { const loader = {load: (injector: any, p: any) => of (loadedConfig)}; - const config = + const config: Routes = [{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: 'not-found'}]; applyRedirects(testModule.injector, loader, serializer, tree('xyz'), config) - .forEach(r => { expect((config[0])._loadedConfig).toBe(loadedConfig); }); + .forEach(r => { expect(config[0]._loadedConfig).toBe(loadedConfig); }); }); it('should load the configuration after an absolute redirect from a wildcard route', () => { @@ -352,11 +352,11 @@ describe('applyRedirects', () => { const loader = {load: (injector: any, p: any) => of (loadedConfig)}; - const config = + const config: Routes = [{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: '/not-found'}]; applyRedirects(testModule.injector, loader, serializer, tree('xyz'), config) - .forEach(r => { expect((config[0])._loadedConfig).toBe(loadedConfig); }); + .forEach(r => { expect(config[0]._loadedConfig).toBe(loadedConfig); }); }); }); diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index c62a56ef55..68612befd8 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -3049,7 +3049,7 @@ describe('Integration', () => { router.navigateByUrl('/blank'); advance(fixture); - const config: any = router.config; + const config = router.config; const firstConfig = config[1]._loadedConfig; expect(firstConfig).toBeDefined(); diff --git a/packages/router/test/router_preloader.spec.ts b/packages/router/test/router_preloader.spec.ts index be48638ffe..c1b8d2afd4 100644 --- a/packages/router/test/router_preloader.spec.ts +++ b/packages/router/test/router_preloader.spec.ts @@ -10,7 +10,7 @@ import {Compiler, Component, NgModule, NgModuleFactoryLoader, NgModuleRef} from import {TestBed, fakeAsync, inject, tick} from '@angular/core/testing'; import {Route, RouteConfigLoadEnd, RouteConfigLoadStart, Router, RouterModule} from '../index'; -import {LoadedRouterConfig} from '../src/router_config_loader'; +import {LoadedRouterConfig} from '../src/config'; import {PreloadAllModules, PreloadingStrategy, RouterPreloader} from '../src/router_preloader'; import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing'; @@ -46,7 +46,7 @@ describe('RouterPreloader', () => { tick(); const c = router.config; - expect(!!((c[0])._loadedConfig)).toBe(false); + expect(c[0]._loadedConfig).not.toBeDefined(); }))); }); @@ -97,12 +97,12 @@ describe('RouterPreloader', () => { const c = router.config; expect(c[0].loadChildren).toEqual('expected'); - const loadedConfig: LoadedRouterConfig = (c[0])._loadedConfig; + const loadedConfig: LoadedRouterConfig = c[0]._loadedConfig; const module: any = loadedConfig.module; expect(loadedConfig.routes[0].path).toEqual('LoadedModule1'); expect(module.parent).toBe(testModule); - const loadedConfig2: LoadedRouterConfig = (loadedConfig.routes[0])._loadedConfig; + const loadedConfig2: LoadedRouterConfig = loadedConfig.routes[0]._loadedConfig; const module2: any = loadedConfig2.module; expect(loadedConfig2.routes[0].path).toEqual('LoadedModule2'); expect(module2.parent).toBe(module); @@ -165,12 +165,12 @@ describe('RouterPreloader', () => { const c = router.config; - const loadedConfig: LoadedRouterConfig = (c[0])._loadedConfig; + const loadedConfig: LoadedRouterConfig = c[0]._loadedConfig; const module: any = loadedConfig.module; expect(module.parent).toBe(testModule); - const loadedConfig2: LoadedRouterConfig = (loadedConfig.routes[0])._loadedConfig; - const loadedConfig3: LoadedRouterConfig = (loadedConfig2.routes[0])._loadedConfig; + const loadedConfig2: LoadedRouterConfig = loadedConfig.routes[0]._loadedConfig; + const loadedConfig3: LoadedRouterConfig = loadedConfig2.routes[0]._loadedConfig; const module3: any = loadedConfig3.module; expect(module3.parent).toBe(module2); }))); @@ -204,8 +204,8 @@ describe('RouterPreloader', () => { tick(); const c = router.config; - expect(!!((c[0])._loadedConfig)).toBe(false); - expect(!!((c[1])._loadedConfig)).toBe(true); + expect(c[0]._loadedConfig).not.toBeDefined(); + expect(c[1]._loadedConfig).toBeDefined(); }))); }); });