refactor(router): misc refactoring
This commit is contained in:
parent
4054055d0d
commit
886cca028f
|
@ -18,8 +18,8 @@ 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, Route, Routes} from './config';
|
import {InternalRoute, LoadedRouterConfig, Route, Routes} from './config';
|
||||||
import {LoadedRouterConfig, 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';
|
||||||
import {andObservables, forEach, waitForMap, wrapIntoObservable} from './utils/collection';
|
import {andObservables, forEach, waitForMap, wrapIntoObservable} from './utils/collection';
|
||||||
|
|
|
@ -6,13 +6,11 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* 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 {Observable} from 'rxjs/Observable';
|
||||||
|
|
||||||
import {PRIMARY_OUTLET} from './shared';
|
import {PRIMARY_OUTLET} from './shared';
|
||||||
import {UrlSegment, UrlSegmentGroup} from './url_tree';
|
import {UrlSegment, UrlSegmentGroup} from './url_tree';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @whatItDoes Represents router configuration.
|
* @whatItDoes Represents router configuration.
|
||||||
*
|
*
|
||||||
|
@ -359,11 +357,17 @@ export interface Route {
|
||||||
children?: Routes;
|
children?: Routes;
|
||||||
loadChildren?: LoadChildren;
|
loadChildren?: LoadChildren;
|
||||||
runGuardsAndResolvers?: RunGuardsAndResolvers;
|
runGuardsAndResolvers?: RunGuardsAndResolvers;
|
||||||
|
/** @internal */
|
||||||
|
_loadedConfig?: LoadedRouterConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class LoadedRouterConfig {
|
||||||
|
constructor(public routes: Route[], public module: NgModuleRef<any>) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface InternalRoute extends Route {
|
export interface InternalRoute extends Route {
|
||||||
// `LoadedRouterConfig` loaded via a Route `loadChildren`
|
// `LoadedRouterConfig` loaded via a Route `loadChildren`
|
||||||
_loadedConfig?: any;
|
_loadedConfig?: LoadedRouterConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function validateConfig(config: Routes, parentPath: string = ''): void {
|
export function validateConfig(config: Routes, parentPath: string = ''): void {
|
||||||
|
|
|
@ -22,14 +22,14 @@ 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, 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 {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';
|
||||||
import {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events';
|
import {Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, RouteConfigLoadEnd, RouteConfigLoadStart, RoutesRecognized} from './events';
|
||||||
import {recognize} from './recognize';
|
import {recognize} from './recognize';
|
||||||
import {DetachedRouteHandle, DetachedRouteHandleInternal, RouteReuseStrategy} from './route_reuse_strategy';
|
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 {RouterOutletMap} from './router_outlet_map';
|
||||||
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState, equalParamsAndUrlSegments, inheritedParamsDataResolve} from './router_state';
|
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, advanceActivatedRoute, createEmptyState, equalParamsAndUrlSegments, inheritedParamsDataResolve} from './router_state';
|
||||||
import {PRIMARY_OUTLET, Params, isNavigationCancelingError} from './shared';
|
import {PRIMARY_OUTLET, Params, isNavigationCancelingError} from './shared';
|
||||||
|
|
|
@ -12,7 +12,7 @@ import {fromPromise} from 'rxjs/observable/fromPromise';
|
||||||
import {of } from 'rxjs/observable/of';
|
import {of } from 'rxjs/observable/of';
|
||||||
import {map} from 'rxjs/operator/map';
|
import {map} from 'rxjs/operator/map';
|
||||||
import {mergeMap} from 'rxjs/operator/mergeMap';
|
import {mergeMap} from 'rxjs/operator/mergeMap';
|
||||||
import {LoadChildren, Route} from './config';
|
import {LoadChildren, LoadedRouterConfig, Route} from './config';
|
||||||
import {flatten, wrapIntoObservable} from './utils/collection';
|
import {flatten, wrapIntoObservable} from './utils/collection';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -21,10 +21,6 @@ import {flatten, wrapIntoObservable} from './utils/collection';
|
||||||
*/
|
*/
|
||||||
export const ROUTES = new InjectionToken<Route[][]>('ROUTES');
|
export const ROUTES = new InjectionToken<Route[][]>('ROUTES');
|
||||||
|
|
||||||
export class LoadedRouterConfig {
|
|
||||||
constructor(public routes: Route[], public module: NgModuleRef<any>) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class RouterConfigLoader {
|
export class RouterConfigLoader {
|
||||||
constructor(
|
constructor(
|
||||||
private loader: NgModuleFactoryLoader, private compiler: Compiler,
|
private loader: NgModuleFactoryLoader, private compiler: Compiler,
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
*found in the LICENSE file at https://angular.io/license
|
*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 {Observable} from 'rxjs/Observable';
|
||||||
import {Subscription} from 'rxjs/Subscription';
|
import {Subscription} from 'rxjs/Subscription';
|
||||||
import {from} from 'rxjs/observable/from';
|
import {from} from 'rxjs/observable/from';
|
||||||
|
@ -16,8 +16,8 @@ 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, Route, Routes} from './config';
|
import {InternalRoute, LoadedRouterConfig, Route, Routes} from './config';
|
||||||
import {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';
|
||||||
|
|
||||||
|
@ -73,7 +73,7 @@ export class NoPreloading implements PreloadingStrategy {
|
||||||
* @stable
|
* @stable
|
||||||
*/
|
*/
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RouterPreloader {
|
export class RouterPreloader implements OnDestroy {
|
||||||
private loader: RouterConfigLoader;
|
private loader: RouterConfigLoader;
|
||||||
private subscription: Subscription;
|
private subscription: Subscription;
|
||||||
|
|
||||||
|
@ -87,8 +87,8 @@ export class RouterPreloader {
|
||||||
};
|
};
|
||||||
|
|
||||||
setUpPreloading(): void {
|
setUpPreloading(): void {
|
||||||
const navigations = filter.call(this.router.events, (e: any) => e instanceof NavigationEnd);
|
const navigations$ = filter.call(this.router.events, (e: Event) => e instanceof NavigationEnd);
|
||||||
this.subscription = concatMap.call(navigations, () => this.preload()).subscribe(() => {});
|
this.subscription = concatMap.call(navigations$, () => this.preload()).subscribe(() => {});
|
||||||
}
|
}
|
||||||
|
|
||||||
preload(): Observable<any> {
|
preload(): Observable<any> {
|
||||||
|
@ -121,8 +121,8 @@ export class RouterPreloader {
|
||||||
|
|
||||||
private preloadConfig(ngModule: NgModuleRef<any>, route: InternalRoute): Observable<void> {
|
private preloadConfig(ngModule: NgModuleRef<any>, route: InternalRoute): 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: any): any => {
|
return mergeMap.call(loaded$, (config: LoadedRouterConfig) => {
|
||||||
route._loadedConfig = config;
|
route._loadedConfig = config;
|
||||||
return this.processRoutes(config.module, config.routes);
|
return this.processRoutes(config.module, config.routes);
|
||||||
});
|
});
|
||||||
|
|
|
@ -12,8 +12,7 @@ import {Observable} from 'rxjs/Observable';
|
||||||
import {of } from 'rxjs/observable/of';
|
import {of } from 'rxjs/observable/of';
|
||||||
|
|
||||||
import {applyRedirects} from '../src/apply_redirects';
|
import {applyRedirects} from '../src/apply_redirects';
|
||||||
import {Routes} from '../src/config';
|
import {LoadedRouterConfig, Routes} from '../src/config';
|
||||||
import {LoadedRouterConfig} from '../src/router_config_loader';
|
|
||||||
import {DefaultUrlSerializer, UrlSegmentGroup, UrlTree, equalSegments} from '../src/url_tree';
|
import {DefaultUrlSerializer, UrlSegmentGroup, UrlTree, equalSegments} from '../src/url_tree';
|
||||||
|
|
||||||
describe('applyRedirects', () => {
|
describe('applyRedirects', () => {
|
||||||
|
|
|
@ -354,7 +354,7 @@ export declare class RouterOutletMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @stable */
|
/** @stable */
|
||||||
export declare class RouterPreloader {
|
export declare class RouterPreloader implements OnDestroy {
|
||||||
constructor(router: Router, moduleLoader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, preloadingStrategy: PreloadingStrategy);
|
constructor(router: Router, moduleLoader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, preloadingStrategy: PreloadingStrategy);
|
||||||
ngOnDestroy(): void;
|
ngOnDestroy(): void;
|
||||||
preload(): Observable<any>;
|
preload(): Observable<any>;
|
||||||
|
|
Loading…
Reference in New Issue