2016-07-06 14:02:16 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2016-07-06 14:02:16 -04:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
import {Compiler, InjectionToken, Injector, NgModuleFactory, NgModuleFactoryLoader} from '@angular/core';
|
2020-04-13 19:40:21 -04:00
|
|
|
import {from, Observable, of} from 'rxjs';
|
2018-02-27 17:06:06 -05:00
|
|
|
import {map, mergeMap} from 'rxjs/operators';
|
2020-04-13 19:40:21 -04:00
|
|
|
|
2020-07-24 18:41:44 -04:00
|
|
|
import {LoadChildren, LoadedRouterConfig, Route} from './config';
|
2016-08-16 00:11:09 -04:00
|
|
|
import {flatten, wrapIntoObservable} from './utils/collection';
|
2020-07-24 18:41:44 -04:00
|
|
|
import {standardizeConfig} from './utils/config';
|
2016-07-06 14:02:16 -04:00
|
|
|
|
2016-08-30 21:07:40 -04:00
|
|
|
/**
|
2019-07-03 19:15:15 -04:00
|
|
|
* The [DI token](guide/glossary/#di-token) for a router configuration.
|
|
|
|
* @see `ROUTES`
|
2018-10-19 07:12:20 -04:00
|
|
|
* @publicApi
|
2016-08-30 21:07:40 -04:00
|
|
|
*/
|
2017-01-03 19:54:46 -05:00
|
|
|
export const ROUTES = new InjectionToken<Route[][]>('ROUTES');
|
2016-07-06 14:32:42 -04:00
|
|
|
|
2016-07-06 14:02:16 -04:00
|
|
|
export class RouterConfigLoader {
|
2017-02-01 16:13:57 -05:00
|
|
|
constructor(
|
|
|
|
private loader: NgModuleFactoryLoader, private compiler: Compiler,
|
2017-02-15 13:57:03 -05:00
|
|
|
private onLoadStartListener?: (r: Route) => void,
|
|
|
|
private onLoadEndListener?: (r: Route) => void) {}
|
2017-02-01 16:13:57 -05:00
|
|
|
|
|
|
|
load(parentInjector: Injector, route: Route): Observable<LoadedRouterConfig> {
|
2017-02-15 13:57:03 -05:00
|
|
|
if (this.onLoadStartListener) {
|
|
|
|
this.onLoadStartListener(route);
|
|
|
|
}
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
const moduleFactory$ = this.loadModuleFactory(route.loadChildren!);
|
2017-02-15 13:57:03 -05:00
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
return moduleFactory$.pipe(map((factory: NgModuleFactory<any>) => {
|
2017-02-15 13:57:03 -05:00
|
|
|
if (this.onLoadEndListener) {
|
|
|
|
this.onLoadEndListener(route);
|
|
|
|
}
|
|
|
|
|
2017-02-01 16:13:57 -05:00
|
|
|
const module = factory.create(parentInjector);
|
2017-02-15 13:57:03 -05:00
|
|
|
|
2018-04-06 18:56:36 -04:00
|
|
|
return new LoadedRouterConfig(
|
|
|
|
flatten(module.injector.get(ROUTES)).map(standardizeConfig), module);
|
2018-02-27 17:06:06 -05:00
|
|
|
}));
|
2016-07-06 14:02:16 -04:00
|
|
|
}
|
2016-08-16 00:11:09 -04:00
|
|
|
|
|
|
|
private loadModuleFactory(loadChildren: LoadChildren): Observable<NgModuleFactory<any>> {
|
|
|
|
if (typeof loadChildren === 'string') {
|
2018-02-27 17:06:06 -05:00
|
|
|
return from(this.loader.load(loadChildren));
|
2016-08-16 00:11:09 -04:00
|
|
|
} else {
|
2018-02-27 17:06:06 -05:00
|
|
|
return wrapIntoObservable(loadChildren()).pipe(mergeMap((t: any) => {
|
2017-03-07 17:27:20 -05:00
|
|
|
if (t instanceof NgModuleFactory) {
|
2020-04-13 19:40:21 -04:00
|
|
|
return of(t);
|
2017-03-07 17:27:20 -05:00
|
|
|
} else {
|
2018-02-27 17:06:06 -05:00
|
|
|
return from(this.compiler.compileModuleAsync(t));
|
2017-03-07 17:27:20 -05:00
|
|
|
}
|
2018-02-27 17:06:06 -05:00
|
|
|
}));
|
2016-08-16 00:11:09 -04:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 21:07:40 -04:00
|
|
|
}
|