Skip to content

Commit

Permalink
refactor(router): drop the InternalRoute interface
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb authored and tbosch committed Apr 14, 2017
1 parent 886cca0 commit ea4afeb
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 41 deletions.
7 changes: 3 additions & 4 deletions packages/router/src/apply_redirects.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -247,7 +247,7 @@ class ApplyRedirects {
}

private matchSegmentAgainstRoute(
ngModule: NgModuleRef<any>, rawSegmentGroup: UrlSegmentGroup, route: InternalRoute,
ngModule: NgModuleRef<any>, rawSegmentGroup: UrlSegmentGroup, route: Route,
segments: UrlSegment[]): Observable<UrlSegmentGroup> {
if (route.path === '**') {
if (route.loadChildren) {
Expand Down Expand Up @@ -292,8 +292,7 @@ class ApplyRedirects {
});
}

private getChildConfig(ngModule: NgModuleRef<any>, route: InternalRoute):
Observable<LoadedRouterConfig> {
private getChildConfig(ngModule: NgModuleRef<any>, route: Route): Observable<LoadedRouterConfig> {
if (route.children) {
// The children belong to the same module
return of (new LoadedRouterConfig(route.children, ngModule));
Expand Down
10 changes: 4 additions & 6 deletions packages/router/src/config.ts
Expand Up @@ -357,19 +357,17 @@ export interface Route {
children?: Routes;
loadChildren?: LoadChildren;
runGuardsAndResolvers?: RunGuardsAndResolvers;
/** @internal */
/**
* Filled for routes with `loadChildren` once the module has been loaded
* @internal
*/
_loadedConfig?: LoadedRouterConfig;
}

export class LoadedRouterConfig {
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 {
// forEach doesn't iterate undefined values
for (let i = 0; i < config.length; i++) {
Expand Down
4 changes: 2 additions & 2 deletions packages/router/src/recognize.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -154,7 +154,7 @@ function sortActivatedRouteSnapshots(nodes: TreeNode<ActivatedRouteSnapshot>[]):
});
}

function getChildConfig(route: InternalRoute): Route[] {
function getChildConfig(route: Route): Route[] {
if (route.children) {
return route.children;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/router/src/router.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -1168,7 +1168,7 @@ function advanceActivatedRouteNodeAndItsChildren(node: TreeNode<ActivatedRoute>)

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;
}
Expand All @@ -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;
}

Expand Down
7 changes: 3 additions & 4 deletions packages/router/src/router_preloader.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -100,8 +100,7 @@ export class RouterPreloader implements OnDestroy {

private processRoutes(ngModule: NgModuleRef<any>, routes: Routes): Observable<void> {
const res: Observable<any>[] = [];
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;
Expand All @@ -119,7 +118,7 @@ export class RouterPreloader implements OnDestroy {
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, () => {
const loaded$ = this.loader.load(ngModule.injector, route);
return mergeMap.call(loaded$, (config: LoadedRouterConfig) => {
Expand Down
24 changes: 12 additions & 12 deletions packages/router/test/apply_redirects.spec.ts
Expand Up @@ -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, <any>loader, serializer, tree('a/b'), config)
.forEach(r => {
compareTrees(r, tree('/a/b'));
expect((<any>config[0])._loadedConfig).toBe(loadedConfig);
expect(config[0]._loadedConfig).toBe(loadedConfig);
});
});

Expand Down Expand Up @@ -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, <any>loader, serializer, tree(''), config).forEach(r => {
compareTrees(r, tree('a'));
expect((<any>config[1])._loadedConfig).toBe(loadedConfig);
expect(config[1]._loadedConfig).toBe(loadedConfig);
});
});

Expand All @@ -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)
.subscribe(r => {});
Expand All @@ -319,7 +319,7 @@ describe('applyRedirects', () => {
.subscribe(
r => {
compareTrees(r, tree('a?k2'));
expect((<any>config[0])._loadedConfig).toBe(loadedConfig);
expect(config[0]._loadedConfig).toBe(loadedConfig);
},
(e) => { throw 'Should not reach'; });
});
Expand All @@ -329,34 +329,34 @@ 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, <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', () => {
const loadedConfig = new LoadedRouterConfig([{path: '', component: ComponentB}], testModule);

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, <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', () => {
const loadedConfig = new LoadedRouterConfig([{path: '', component: ComponentB}], testModule);

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, <any>loader, serializer, tree('xyz'), config)
.forEach(r => { expect((<any>config[0])._loadedConfig).toBe(loadedConfig); });
.forEach(r => { expect(config[0]._loadedConfig).toBe(loadedConfig); });
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/router/test/integration.spec.ts
Expand Up @@ -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();
Expand Down
18 changes: 9 additions & 9 deletions packages/router/test/router_preloader.spec.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -46,7 +46,7 @@ describe('RouterPreloader', () => {
tick();

const c = router.config;
expect(!!((<any>c[0])._loadedConfig)).toBe(false);
expect(c[0]._loadedConfig).not.toBeDefined();
})));
});

Expand Down Expand Up @@ -97,12 +97,12 @@ describe('RouterPreloader', () => {
const c = router.config;
expect(c[0].loadChildren).toEqual('expected');

const loadedConfig: LoadedRouterConfig = (<any>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 = (<any>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);
Expand Down Expand Up @@ -165,12 +165,12 @@ describe('RouterPreloader', () => {

const c = router.config;

const loadedConfig: LoadedRouterConfig = (<any>c[0])._loadedConfig;
const loadedConfig: LoadedRouterConfig = c[0]._loadedConfig;
const module: any = loadedConfig.module;
expect(module.parent).toBe(testModule);

const loadedConfig2: LoadedRouterConfig = (<any>loadedConfig.routes[0])._loadedConfig;
const loadedConfig3: LoadedRouterConfig = (<any>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);
})));
Expand Down Expand Up @@ -204,8 +204,8 @@ describe('RouterPreloader', () => {
tick();

const c = router.config;
expect(!!((<any>c[0])._loadedConfig)).toBe(false);
expect(!!((<any>c[1])._loadedConfig)).toBe(true);
expect(c[0]._loadedConfig).not.toBeDefined();
expect(c[1]._loadedConfig).toBeDefined();
})));
});
});

0 comments on commit ea4afeb

Please sign in to comment.