refactor(router): drop the `InternalRoute` interface

This commit is contained in:
Victor Berchet 2017-04-11 08:34:58 -07:00 committed by Tobias Bosch
parent 886cca028f
commit ea4afebeb9
8 changed files with 37 additions and 41 deletions

View File

@ -18,7 +18,7 @@ import {map} from 'rxjs/operator/map';
import {mergeMap} from 'rxjs/operator/mergeMap'; import {mergeMap} from 'rxjs/operator/mergeMap';
import {EmptyError} from 'rxjs/util/EmptyError'; 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 {RouterConfigLoader} from './router_config_loader';
import {PRIMARY_OUTLET, Params, defaultUrlMatcher, navigationCancelingError} from './shared'; import {PRIMARY_OUTLET, Params, defaultUrlMatcher, navigationCancelingError} from './shared';
import {UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree'; import {UrlSegment, UrlSegmentGroup, UrlSerializer, UrlTree} from './url_tree';
@ -247,7 +247,7 @@ class ApplyRedirects {
} }
private matchSegmentAgainstRoute( private matchSegmentAgainstRoute(
ngModule: NgModuleRef<any>, rawSegmentGroup: UrlSegmentGroup, route: InternalRoute, ngModule: NgModuleRef<any>, rawSegmentGroup: UrlSegmentGroup, route: Route,
segments: UrlSegment[]): Observable<UrlSegmentGroup> { segments: UrlSegment[]): Observable<UrlSegmentGroup> {
if (route.path === '**') { if (route.path === '**') {
if (route.loadChildren) { if (route.loadChildren) {
@ -292,8 +292,7 @@ class ApplyRedirects {
}); });
} }
private getChildConfig(ngModule: NgModuleRef<any>, route: InternalRoute): private getChildConfig(ngModule: NgModuleRef<any>, route: Route): Observable<LoadedRouterConfig> {
Observable<LoadedRouterConfig> {
if (route.children) { if (route.children) {
// The children belong to the same module // The children belong to the same module
return of (new LoadedRouterConfig(route.children, ngModule)); return of (new LoadedRouterConfig(route.children, ngModule));

View File

@ -357,7 +357,10 @@ export interface Route {
children?: Routes; children?: Routes;
loadChildren?: LoadChildren; loadChildren?: LoadChildren;
runGuardsAndResolvers?: RunGuardsAndResolvers; runGuardsAndResolvers?: RunGuardsAndResolvers;
/** @internal */ /**
* Filled for routes with `loadChildren` once the module has been loaded
* @internal
*/
_loadedConfig?: LoadedRouterConfig; _loadedConfig?: LoadedRouterConfig;
} }
@ -365,11 +368,6 @@ export class LoadedRouterConfig {
constructor(public routes: Route[], public module: NgModuleRef<any>) {} constructor(public routes: Route[], public module: NgModuleRef<any>) {}
} }
export interface InternalRoute extends Route {
// `LoadedRouterConfig` loaded via a Route `loadChildren`
_loadedConfig?: LoadedRouterConfig;
}
export function validateConfig(config: Routes, parentPath: string = ''): void { export function validateConfig(config: Routes, parentPath: string = ''): void {
// forEach doesn't iterate undefined values // forEach doesn't iterate undefined values
for (let i = 0; i < config.length; i++) { for (let i = 0; i < config.length; i++) {

View File

@ -11,7 +11,7 @@ import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer'; import {Observer} from 'rxjs/Observer';
import {of } from 'rxjs/observable/of'; 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 {ActivatedRouteSnapshot, RouterStateSnapshot, inheritedParamsDataResolve} from './router_state';
import {PRIMARY_OUTLET, defaultUrlMatcher} from './shared'; import {PRIMARY_OUTLET, defaultUrlMatcher} from './shared';
import {UrlSegment, UrlSegmentGroup, UrlTree, mapChildrenIntoArray} from './url_tree'; import {UrlSegment, UrlSegmentGroup, UrlTree, mapChildrenIntoArray} from './url_tree';
@ -154,7 +154,7 @@ function sortActivatedRouteSnapshots(nodes: TreeNode<ActivatedRouteSnapshot>[]):
}); });
} }
function getChildConfig(route: InternalRoute): Route[] { function getChildConfig(route: Route): Route[] {
if (route.children) { if (route.children) {
return route.children; return route.children;
} }

View File

@ -22,7 +22,7 @@ import {mergeMap} from 'rxjs/operator/mergeMap';
import {reduce} from 'rxjs/operator/reduce'; import {reduce} from 'rxjs/operator/reduce';
import {applyRedirects} from './apply_redirects'; 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 {createRouterState} from './create_router_state';
import {createUrlTree} from './create_url_tree'; import {createUrlTree} from './create_url_tree';
import {RouterOutlet} from './directives/router_outlet'; import {RouterOutlet} from './directives/router_outlet';
@ -1168,7 +1168,7 @@ function advanceActivatedRouteNodeAndItsChildren(node: TreeNode<ActivatedRoute>)
function parentLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig { function parentLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConfig {
for (let s = snapshot.parent; s; s = s.parent) { 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._loadedConfig) return route._loadedConfig;
if (route && route.component) return null; if (route && route.component) return null;
} }
@ -1180,7 +1180,7 @@ function closestLoadedConfig(snapshot: ActivatedRouteSnapshot): LoadedRouterConf
if (!snapshot) return null; if (!snapshot) return null;
for (let s = snapshot.parent; s; s = s.parent) { 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._loadedConfig) return route._loadedConfig;
} }

View File

@ -16,7 +16,7 @@ import {concatMap} from 'rxjs/operator/concatMap';
import {filter} from 'rxjs/operator/filter'; import {filter} from 'rxjs/operator/filter';
import {mergeAll} from 'rxjs/operator/mergeAll'; import {mergeAll} from 'rxjs/operator/mergeAll';
import {mergeMap} from 'rxjs/operator/mergeMap'; 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 {Event, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart} from './events';
import {Router} from './router'; import {Router} from './router';
import {RouterConfigLoader} from './router_config_loader'; import {RouterConfigLoader} from './router_config_loader';
@ -100,8 +100,7 @@ export class RouterPreloader implements OnDestroy {
private processRoutes(ngModule: NgModuleRef<any>, routes: Routes): Observable<void> { private processRoutes(ngModule: NgModuleRef<any>, routes: Routes): Observable<void> {
const res: Observable<any>[] = []; const res: Observable<any>[] = [];
for (const r of routes) { for (const route of routes) {
const route: InternalRoute = r;
// we already have the config loaded, just recurse // we already have the config loaded, just recurse
if (route.loadChildren && !route.canLoad && route._loadedConfig) { if (route.loadChildren && !route.canLoad && route._loadedConfig) {
const childConfig = route._loadedConfig; const childConfig = route._loadedConfig;
@ -119,7 +118,7 @@ export class RouterPreloader implements OnDestroy {
return mergeAll.call(from(res)); return mergeAll.call(from(res));
} }
private preloadConfig(ngModule: NgModuleRef<any>, route: InternalRoute): Observable<void> { private preloadConfig(ngModule: NgModuleRef<any>, route: Route): Observable<void> {
return this.preloadingStrategy.preload(route, () => { return this.preloadingStrategy.preload(route, () => {
const loaded$ = this.loader.load(ngModule.injector, route); const loaded$ = this.loader.load(ngModule.injector, route);
return mergeMap.call(loaded$, (config: LoadedRouterConfig) => { return mergeMap.call(loaded$, (config: LoadedRouterConfig) => {

View File

@ -162,12 +162,12 @@ describe('applyRedirects', () => {
return of (loadedConfig); return of (loadedConfig);
} }
}; };
const config = [{path: 'a', component: ComponentA, loadChildren: 'children'}]; const config: Routes = [{path: 'a', component: ComponentA, loadChildren: 'children'}];
applyRedirects(testModule.injector, <any>loader, serializer, tree('a/b'), config) applyRedirects(testModule.injector, <any>loader, serializer, tree('a/b'), config)
.forEach(r => { .forEach(r => {
compareTrees(r, tree('/a/b')); compareTrees(r, tree('/a/b'));
expect((<any>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 loader = {load: (injector: any, p: any) => of (loadedConfig)};
const config = const config: Routes =
[{path: '', pathMatch: 'full', redirectTo: '/a'}, {path: 'a', loadChildren: 'children'}]; [{path: '', pathMatch: 'full', redirectTo: '/a'}, {path: 'a', loadChildren: 'children'}];
applyRedirects(testModule.injector, <any>loader, serializer, tree(''), config).forEach(r => { applyRedirects(testModule.injector, <any>loader, serializer, tree(''), config).forEach(r => {
compareTrees(r, tree('a')); compareTrees(r, tree('a'));
expect((<any>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, <any>loader, serializer, tree('a?k1'), config) applyRedirects(testModule.injector, <any>loader, serializer, tree('a?k1'), config)
.subscribe(r => {}); .subscribe(r => {});
@ -319,7 +319,7 @@ describe('applyRedirects', () => {
.subscribe( .subscribe(
r => { r => {
compareTrees(r, tree('a?k2')); compareTrees(r, tree('a?k2'));
expect((<any>config[0])._loadedConfig).toBe(loadedConfig); expect(config[0]._loadedConfig).toBe(loadedConfig);
}, },
(e) => { throw 'Should not reach'; }); (e) => { throw 'Should not reach'; });
}); });
@ -329,10 +329,10 @@ describe('applyRedirects', () => {
const loader = {load: (injector: any, p: any) => of (loadedConfig)}; const loader = {load: (injector: any, p: any) => of (loadedConfig)};
const config = [{path: '**', loadChildren: 'children'}]; const config: Routes = [{path: '**', loadChildren: 'children'}];
applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config) applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config)
.forEach(r => { expect((<any>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', () => { 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 loader = {load: (injector: any, p: any) => of (loadedConfig)};
const config = const config: Routes =
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: 'not-found'}]; [{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: 'not-found'}];
applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config) applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config)
.forEach(r => { expect((<any>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', () => { 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 loader = {load: (injector: any, p: any) => of (loadedConfig)};
const config = const config: Routes =
[{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: '/not-found'}]; [{path: 'not-found', loadChildren: 'children'}, {path: '**', redirectTo: '/not-found'}];
applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config) applyRedirects(testModule.injector, <any>loader, serializer, tree('xyz'), config)
.forEach(r => { expect((<any>config[0])._loadedConfig).toBe(loadedConfig); }); .forEach(r => { expect(config[0]._loadedConfig).toBe(loadedConfig); });
}); });
}); });

View File

@ -3049,7 +3049,7 @@ describe('Integration', () => {
router.navigateByUrl('/blank'); router.navigateByUrl('/blank');
advance(fixture); advance(fixture);
const config: any = router.config; const config = router.config;
const firstConfig = config[1]._loadedConfig; const firstConfig = config[1]._loadedConfig;
expect(firstConfig).toBeDefined(); expect(firstConfig).toBeDefined();

View File

@ -10,7 +10,7 @@ import {Compiler, Component, NgModule, NgModuleFactoryLoader, NgModuleRef} from
import {TestBed, fakeAsync, inject, tick} from '@angular/core/testing'; import {TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import {Route, RouteConfigLoadEnd, RouteConfigLoadStart, Router, RouterModule} from '../index'; 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 {PreloadAllModules, PreloadingStrategy, RouterPreloader} from '../src/router_preloader';
import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing'; import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing';
@ -46,7 +46,7 @@ describe('RouterPreloader', () => {
tick(); tick();
const c = router.config; const c = router.config;
expect(!!((<any>c[0])._loadedConfig)).toBe(false); expect(c[0]._loadedConfig).not.toBeDefined();
}))); })));
}); });
@ -97,12 +97,12 @@ describe('RouterPreloader', () => {
const c = router.config; const c = router.config;
expect(c[0].loadChildren).toEqual('expected'); expect(c[0].loadChildren).toEqual('expected');
const loadedConfig: LoadedRouterConfig = (<any>c[0])._loadedConfig; const loadedConfig: LoadedRouterConfig = c[0]._loadedConfig;
const module: any = loadedConfig.module; const module: any = loadedConfig.module;
expect(loadedConfig.routes[0].path).toEqual('LoadedModule1'); expect(loadedConfig.routes[0].path).toEqual('LoadedModule1');
expect(module.parent).toBe(testModule); expect(module.parent).toBe(testModule);
const loadedConfig2: LoadedRouterConfig = (<any>loadedConfig.routes[0])._loadedConfig; const loadedConfig2: LoadedRouterConfig = loadedConfig.routes[0]._loadedConfig;
const module2: any = loadedConfig2.module; const module2: any = loadedConfig2.module;
expect(loadedConfig2.routes[0].path).toEqual('LoadedModule2'); expect(loadedConfig2.routes[0].path).toEqual('LoadedModule2');
expect(module2.parent).toBe(module); expect(module2.parent).toBe(module);
@ -165,12 +165,12 @@ describe('RouterPreloader', () => {
const c = router.config; const c = router.config;
const loadedConfig: LoadedRouterConfig = (<any>c[0])._loadedConfig; const loadedConfig: LoadedRouterConfig = c[0]._loadedConfig;
const module: any = loadedConfig.module; const module: any = loadedConfig.module;
expect(module.parent).toBe(testModule); expect(module.parent).toBe(testModule);
const loadedConfig2: LoadedRouterConfig = (<any>loadedConfig.routes[0])._loadedConfig; const loadedConfig2: LoadedRouterConfig = loadedConfig.routes[0]._loadedConfig;
const loadedConfig3: LoadedRouterConfig = (<any>loadedConfig2.routes[0])._loadedConfig; const loadedConfig3: LoadedRouterConfig = loadedConfig2.routes[0]._loadedConfig;
const module3: any = loadedConfig3.module; const module3: any = loadedConfig3.module;
expect(module3.parent).toBe(module2); expect(module3.parent).toBe(module2);
}))); })));
@ -204,8 +204,8 @@ describe('RouterPreloader', () => {
tick(); tick();
const c = router.config; const c = router.config;
expect(!!((<any>c[0])._loadedConfig)).toBe(false); expect(c[0]._loadedConfig).not.toBeDefined();
expect(!!((<any>c[1])._loadedConfig)).toBe(true); expect(c[1]._loadedConfig).toBeDefined();
}))); })));
}); });
}); });