2016-09-16 18:08:15 -04:00
|
|
|
/**
|
|
|
|
*@license
|
|
|
|
*Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
*Use of this source code is governed by an MIT-style license that can be
|
|
|
|
*found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2017-04-04 19:00:40 -04:00
|
|
|
import {Compiler, Injectable, Injector, NgModuleFactoryLoader, NgModuleRef, OnDestroy} from '@angular/core';
|
2016-09-16 18:08:15 -04:00
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Subscription} from 'rxjs/Subscription';
|
|
|
|
import {from} from 'rxjs/observable/from';
|
|
|
|
import {of } from 'rxjs/observable/of';
|
|
|
|
import {_catch} from 'rxjs/operator/catch';
|
|
|
|
import {concatMap} from 'rxjs/operator/concatMap';
|
|
|
|
import {filter} from 'rxjs/operator/filter';
|
|
|
|
import {mergeAll} from 'rxjs/operator/mergeAll';
|
|
|
|
import {mergeMap} from 'rxjs/operator/mergeMap';
|
2017-04-11 11:34:58 -04:00
|
|
|
import {LoadedRouterConfig, Route, Routes} from './config';
|
2017-04-04 19:00:40 -04:00
|
|
|
import {Event, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart} from './events';
|
2017-02-01 16:13:57 -05:00
|
|
|
import {Router} from './router';
|
2016-09-16 18:08:15 -04:00
|
|
|
import {RouterConfigLoader} from './router_config_loader';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Provides a preloading strategy.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export abstract class PreloadingStrategy {
|
|
|
|
abstract preload(route: Route, fn: () => Observable<any>): Observable<any>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-03 14:23:17 -04:00
|
|
|
* @whatItDoes Provides a preloading strategy that preloads all modules as quickly as possible.
|
2016-09-16 18:08:15 -04:00
|
|
|
*
|
|
|
|
* @howToUse
|
|
|
|
*
|
|
|
|
* ```
|
|
|
|
* RouteModule.forRoot(ROUTES, {preloadingStrategy: PreloadAllModules})
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class PreloadAllModules implements PreloadingStrategy {
|
|
|
|
preload(route: Route, fn: () => Observable<any>): Observable<any> {
|
|
|
|
return _catch.call(fn(), () => of (null));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @whatItDoes Provides a preloading strategy that does not preload any modules.
|
|
|
|
*
|
|
|
|
* @description
|
|
|
|
*
|
|
|
|
* This strategy is enabled by default.
|
|
|
|
*
|
|
|
|
* @experimental
|
|
|
|
*/
|
|
|
|
export class NoPreloading implements PreloadingStrategy {
|
|
|
|
preload(route: Route, fn: () => Observable<any>): Observable<any> { return of (null); }
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The preloader optimistically loads all router configurations to
|
|
|
|
* make navigations into lazily-loaded sections of the application faster.
|
|
|
|
*
|
|
|
|
* The preloader runs in the background. When the router bootstraps, the preloader
|
|
|
|
* starts listening to all navigation events. After every such event, the preloader
|
|
|
|
* will check if any configurations can be loaded lazily.
|
|
|
|
*
|
|
|
|
* If a route is protected by `canLoad` guards, the preloaded will not load it.
|
2016-11-09 16:33:33 -05:00
|
|
|
*
|
|
|
|
* @stable
|
2016-09-16 18:08:15 -04:00
|
|
|
*/
|
|
|
|
@Injectable()
|
2017-04-04 19:00:40 -04:00
|
|
|
export class RouterPreloader implements OnDestroy {
|
2016-09-16 18:08:15 -04:00
|
|
|
private loader: RouterConfigLoader;
|
|
|
|
private subscription: Subscription;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private router: Router, moduleLoader: NgModuleFactoryLoader, compiler: Compiler,
|
|
|
|
private injector: Injector, private preloadingStrategy: PreloadingStrategy) {
|
2017-02-15 13:57:03 -05:00
|
|
|
const onStartLoad = (r: Route) => router.triggerEvent(new RouteConfigLoadStart(r));
|
|
|
|
const onEndLoad = (r: Route) => router.triggerEvent(new RouteConfigLoadEnd(r));
|
|
|
|
|
|
|
|
this.loader = new RouterConfigLoader(moduleLoader, compiler, onStartLoad, onEndLoad);
|
2017-09-22 13:51:03 -04:00
|
|
|
}
|
2016-09-16 18:08:15 -04:00
|
|
|
|
|
|
|
setUpPreloading(): void {
|
2017-04-04 19:00:40 -04:00
|
|
|
const navigations$ = filter.call(this.router.events, (e: Event) => e instanceof NavigationEnd);
|
|
|
|
this.subscription = concatMap.call(navigations$, () => this.preload()).subscribe(() => {});
|
2016-09-16 18:08:15 -04:00
|
|
|
}
|
|
|
|
|
2017-03-14 19:26:17 -04:00
|
|
|
preload(): Observable<any> {
|
|
|
|
const ngModule = this.injector.get(NgModuleRef);
|
|
|
|
return this.processRoutes(ngModule, this.router.config);
|
|
|
|
}
|
2016-09-16 18:08:15 -04:00
|
|
|
|
2017-07-01 13:30:17 -04:00
|
|
|
// TODO(jasonaden): This class relies on code external to the class to call setUpPreloading. If
|
|
|
|
// this hasn't been done, ngOnDestroy will fail as this.subscription will be undefined. This
|
|
|
|
// should be refactored.
|
2017-02-01 16:13:57 -05:00
|
|
|
ngOnDestroy(): void { this.subscription.unsubscribe(); }
|
2016-09-16 18:08:15 -04:00
|
|
|
|
2017-03-14 19:26:17 -04:00
|
|
|
private processRoutes(ngModule: NgModuleRef<any>, routes: Routes): Observable<void> {
|
2016-09-16 18:08:15 -04:00
|
|
|
const res: Observable<any>[] = [];
|
2017-04-11 11:34:58 -04:00
|
|
|
for (const route of routes) {
|
2017-01-31 17:00:20 -05:00
|
|
|
// we already have the config loaded, just recurse
|
2017-03-29 12:44:04 -04:00
|
|
|
if (route.loadChildren && !route.canLoad && route._loadedConfig) {
|
|
|
|
const childConfig = route._loadedConfig;
|
2017-04-05 00:45:53 -04:00
|
|
|
res.push(this.processRoutes(childConfig.module, childConfig.routes));
|
2016-09-16 18:08:15 -04:00
|
|
|
|
|
|
|
// no config loaded, fetch the config
|
2017-03-29 12:44:04 -04:00
|
|
|
} else if (route.loadChildren && !route.canLoad) {
|
|
|
|
res.push(this.preloadConfig(ngModule, route));
|
2016-09-16 18:08:15 -04:00
|
|
|
|
|
|
|
// recurse into children
|
2017-03-29 12:44:04 -04:00
|
|
|
} else if (route.children) {
|
|
|
|
res.push(this.processRoutes(ngModule, route.children));
|
2016-09-16 18:08:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mergeAll.call(from(res));
|
|
|
|
}
|
|
|
|
|
2017-04-11 11:34:58 -04:00
|
|
|
private preloadConfig(ngModule: NgModuleRef<any>, route: Route): Observable<void> {
|
2016-09-16 18:08:15 -04:00
|
|
|
return this.preloadingStrategy.preload(route, () => {
|
2017-04-04 19:00:40 -04:00
|
|
|
const loaded$ = this.loader.load(ngModule.injector, route);
|
|
|
|
return mergeMap.call(loaded$, (config: LoadedRouterConfig) => {
|
2017-03-29 12:44:04 -04:00
|
|
|
route._loadedConfig = config;
|
2017-03-14 19:26:17 -04:00
|
|
|
return this.processRoutes(config.module, config.routes);
|
2016-09-16 18:08:15 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-01-31 17:00:20 -05:00
|
|
|
}
|