fix(router): do not use rx/add/operator

This commit is contained in:
vsavkin 2016-08-30 14:25:46 -07:00 committed by Victor Berchet
parent 6e40ef0f6d
commit c350ba29f6
6 changed files with 177 additions and 164 deletions

View File

@ -10,27 +10,28 @@ export default {
'@angular/platform-browser': 'ng.platformBrowser', '@angular/platform-browser': 'ng.platformBrowser',
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic', '@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
'rxjs/BehaviorSubject': 'Rx',
'rxjs/Observable': 'Rx', 'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx', 'rxjs/Subject': 'Rx',
'rxjs/BehaviorSubject': 'Rx',
'rxjs/Observer': 'Rx',
'rxjs/Subscription': 'Rx', 'rxjs/Subscription': 'Rx',
'rxjs/util/EmptyError': 'Rx',
'rxjs/observable/PromiseObservable': 'Rx', // this is wrong, but this stuff has changed in rxjs b.6 so we need to fix it when we update.
'rxjs/add/operator/map': 'Rx.Observable.prototype',
'rxjs/add/operator/mergeAll': 'Rx.Observable.prototype',
'rxjs/add/operator/concatAll': 'Rx.Observable.prototype',
'rxjs/add/operator/mergeMap': 'Rx.Observable.prototype',
'rxjs/add/operator/reduce': 'Rx.Observable.prototype',
'rxjs/add/operator/every': 'Rx.Observable.prototype',
'rxjs/add/operator/first': 'Rx.Observable.prototype',
'rxjs/add/operator/catch': 'Rx.Observable.prototype',
'rxjs/add/operator/last': 'Rx.Observable.prototype',
'rxjs/add/operator/toPromise': 'Rx.Observable.prototype',
'rxjs/observable/from': 'Rx.Observable', 'rxjs/observable/from': 'Rx.Observable',
'rxjs/observable/fromPromise': 'Rx.Observable', 'rxjs/observable/fromPromise': 'Rx.Observable',
'rxjs/observable/forkJoin': 'Rx.Observable', 'rxjs/observable/forkJoin': 'Rx.Observable',
'rxjs/observable/of': 'Rx.Observable', 'rxjs/observable/of': 'Rx.Observable',
'rxjs/util/EmptyError': 'Rx.EmptyError'
} 'rxjs/operator/toPromise': 'Rx.Observable.prototype',
'rxjs/operator/map': 'Rx.Observable.prototype',
'rxjs/operator/mergeAll': 'Rx.Observable.prototype',
'rxjs/operator/concatAll': 'Rx.Observable.prototype',
'rxjs/operator/mergeMap': 'Rx.Observable.prototype',
'rxjs/operator/reduce': 'Rx.Observable.prototype',
'rxjs/operator/every': 'Rx.Observable.prototype',
'rxjs/operator/first': 'Rx.Observable.prototype',
'rxjs/operator/catch': 'Rx.Observable.prototype',
'rxjs/operator/last': 'Rx.Observable.prototype'
},
plugins: [
]
} }

View File

@ -6,15 +6,16 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import 'rxjs/add/operator/first';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/concatAll';
import {Injector} from '@angular/core'; import {Injector} from '@angular/core';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer'; import {Observer} from 'rxjs/Observer';
import {from} from 'rxjs/observable/from'; import {from} from 'rxjs/observable/from';
import {of } from 'rxjs/observable/of'; import {of } from 'rxjs/observable/of';
import {_catch} from 'rxjs/operator/catch';
import {concatAll} from 'rxjs/operator/concatAll';
import {first} from 'rxjs/operator/first';
import {map} from 'rxjs/operator/map';
import {mergeMap} from 'rxjs/operator/mergeMap';
import {EmptyError} from 'rxjs/util/EmptyError'; import {EmptyError} from 'rxjs/util/EmptyError';
import {Route, Routes} from './config'; import {Route, Routes} from './config';
@ -62,34 +63,38 @@ class ApplyRedirects {
private urlTree: UrlTree, private config: Routes) {} private urlTree: UrlTree, private config: Routes) {}
apply(): Observable<UrlTree> { apply(): Observable<UrlTree> {
return this.expandSegmentGroup(this.injector, this.config, this.urlTree.root, PRIMARY_OUTLET) const expanded$ =
.map(rootSegmentGroup => this.createUrlTree(rootSegmentGroup)) this.expandSegmentGroup(this.injector, this.config, this.urlTree.root, PRIMARY_OUTLET);
.catch(e => { const urlTrees$ = map.call(
if (e instanceof AbsoluteRedirect) { expanded$, (rootSegmentGroup: UrlSegmentGroup) => this.createUrlTree(rootSegmentGroup));
// after an absolute redirect we do not apply any more redirects! return _catch.call(urlTrees$, (e: any) => {
this.allowRedirects = false; if (e instanceof AbsoluteRedirect) {
const group = // after an absolute redirect we do not apply any more redirects!
new UrlSegmentGroup([], {[PRIMARY_OUTLET]: new UrlSegmentGroup(e.segments, {})}); this.allowRedirects = false;
// we need to run matching, so we can fetch all lazy-loaded modules const group =
return this.match(group); new UrlSegmentGroup([], {[PRIMARY_OUTLET]: new UrlSegmentGroup(e.segments, {})});
} else if (e instanceof NoMatch) { // we need to run matching, so we can fetch all lazy-loaded modules
throw this.noMatchError(e); return this.match(group);
} else { } else if (e instanceof NoMatch) {
throw e; throw this.noMatchError(e);
} } else {
}); throw e;
}
});
} }
private match(segmentGroup: UrlSegmentGroup): Observable<UrlTree> { private match(segmentGroup: UrlSegmentGroup): Observable<UrlTree> {
return this.expandSegmentGroup(this.injector, this.config, segmentGroup, PRIMARY_OUTLET) const expanded$ =
.map(rootSegmentGroup => this.createUrlTree(rootSegmentGroup)) this.expandSegmentGroup(this.injector, this.config, segmentGroup, PRIMARY_OUTLET);
.catch((e): Observable<UrlTree> => { const mapped$ = map.call(
if (e instanceof NoMatch) { expanded$, (rootSegmentGroup: UrlSegmentGroup) => this.createUrlTree(rootSegmentGroup));
throw this.noMatchError(e); return _catch.call(mapped$, (e: any): Observable<UrlTree> => {
} else { if (e instanceof NoMatch) {
throw e; throw this.noMatchError(e);
} } else {
}); throw e;
}
});
} }
private noMatchError(e: NoMatch): any { private noMatchError(e: NoMatch): any {
@ -107,8 +112,9 @@ class ApplyRedirects {
injector: Injector, routes: Route[], segmentGroup: UrlSegmentGroup, injector: Injector, routes: Route[], segmentGroup: UrlSegmentGroup,
outlet: string): Observable<UrlSegmentGroup> { outlet: string): Observable<UrlSegmentGroup> {
if (segmentGroup.segments.length === 0 && segmentGroup.hasChildren()) { if (segmentGroup.segments.length === 0 && segmentGroup.hasChildren()) {
return this.expandChildren(injector, routes, segmentGroup) return map.call(
.map(children => new UrlSegmentGroup([], children)); this.expandChildren(injector, routes, segmentGroup),
(children: any) => new UrlSegmentGroup([], children));
} else { } else {
return this.expandSegment( return this.expandSegment(
injector, segmentGroup, routes, segmentGroup.segments, outlet, true); injector, segmentGroup, routes, segmentGroup.segments, outlet, true);
@ -125,22 +131,20 @@ class ApplyRedirects {
private expandSegment( private expandSegment(
injector: Injector, segmentGroup: UrlSegmentGroup, routes: Route[], segments: UrlSegment[], injector: Injector, segmentGroup: UrlSegmentGroup, routes: Route[], segments: UrlSegment[],
outlet: string, allowRedirects: boolean): Observable<UrlSegmentGroup> { outlet: string, allowRedirects: boolean): Observable<UrlSegmentGroup> {
const processRoutes = const routes$ = of (...routes);
of (...routes) const processedRoutes$ = map.call(routes$, (r: any) => {
.map(r => { const expanded$ = this.expandSegmentAgainstRoute(
return this injector, segmentGroup, routes, r, segments, outlet, allowRedirects);
.expandSegmentAgainstRoute( return _catch.call(expanded$, (e: any) => {
injector, segmentGroup, routes, r, segments, outlet, allowRedirects) if (e instanceof NoMatch)
.catch((e) => { return of (null);
if (e instanceof NoMatch) else
return of (null); throw e;
else });
throw e; });
}); const concattedProcessedRoutes$ = concatAll.call(processedRoutes$);
}) const first$ = first.call(concattedProcessedRoutes$, (s: any) => !!s);
.concatAll(); return _catch.call(first$, (e: any, _: any): Observable<UrlSegmentGroup> => {
return processRoutes.first(s => !!s).catch((e: any, _: any): Observable<UrlSegmentGroup> => {
if (e instanceof EmptyError) { if (e instanceof EmptyError) {
throw new NoMatch(segmentGroup); throw new NoMatch(segmentGroup);
} else { } else {
@ -214,25 +218,27 @@ class ApplyRedirects {
if (!matched) return noMatch(rawSegmentGroup); if (!matched) return noMatch(rawSegmentGroup);
const rawSlicedSegments = segments.slice(lastChild); const rawSlicedSegments = segments.slice(lastChild);
const childConfig$ = this.getChildConfig(injector, route);
return this.getChildConfig(injector, route).mergeMap(routerConfig => { return mergeMap.call(childConfig$, (routerConfig: any) => {
const childInjector = routerConfig.injector; const childInjector = routerConfig.injector;
const childConfig = routerConfig.routes; const childConfig = routerConfig.routes;
const {segmentGroup, slicedSegments} = const {segmentGroup, slicedSegments} =
split(rawSegmentGroup, consumedSegments, rawSlicedSegments, childConfig); split(rawSegmentGroup, consumedSegments, rawSlicedSegments, childConfig);
if (slicedSegments.length === 0 && segmentGroup.hasChildren()) { if (slicedSegments.length === 0 && segmentGroup.hasChildren()) {
return this.expandChildren(childInjector, childConfig, segmentGroup) const expanded$ = this.expandChildren(childInjector, childConfig, segmentGroup);
.map(children => new UrlSegmentGroup(consumedSegments, children)); return map.call(
expanded$, (children: any) => new UrlSegmentGroup(consumedSegments, children));
} else if (childConfig.length === 0 && slicedSegments.length === 0) { } else if (childConfig.length === 0 && slicedSegments.length === 0) {
return of (new UrlSegmentGroup(consumedSegments, {})); return of (new UrlSegmentGroup(consumedSegments, {}));
} else { } else {
return this const expanded$ = this.expandSegment(
.expandSegment( childInjector, segmentGroup, childConfig, slicedSegments, PRIMARY_OUTLET, true);
childInjector, segmentGroup, childConfig, slicedSegments, PRIMARY_OUTLET, true) return map.call(
.map(cs => new UrlSegmentGroup(consumedSegments.concat(cs.segments), cs.children)); expanded$,
(cs: any) => new UrlSegmentGroup(consumedSegments.concat(cs.segments), cs.children));
} }
}); });
} }
@ -242,12 +248,12 @@ class ApplyRedirects {
if (route.children) { if (route.children) {
return of (new LoadedRouterConfig(route.children, injector, null)); return of (new LoadedRouterConfig(route.children, injector, null));
} else if (route.loadChildren) { } else if (route.loadChildren) {
return runGuards(injector, route).mergeMap(shouldLoad => { return mergeMap.call(runGuards(injector, route), (shouldLoad: any) => {
if (shouldLoad) { if (shouldLoad) {
if ((<any>route)._loadedConfig) { if ((<any>route)._loadedConfig) {
return of ((<any>route)._loadedConfig); return of ((<any>route)._loadedConfig);
} else { } else {
return this.configLoader.load(injector, route.loadChildren).map(r => { return map.call(this.configLoader.load(injector, route.loadChildren), (r: any) => {
(<any>route)._loadedConfig = r; (<any>route)._loadedConfig = r;
return r; return r;
}); });
@ -265,7 +271,7 @@ class ApplyRedirects {
function runGuards(injector: Injector, route: Route): Observable<boolean> { function runGuards(injector: Injector, route: Route): Observable<boolean> {
const canLoad = route.canLoad; const canLoad = route.canLoad;
if (!canLoad || canLoad.length === 0) return of (true); if (!canLoad || canLoad.length === 0) return of (true);
const obs = from(canLoad).map(c => { const obs = map.call(from(canLoad), (c: any) => {
const guard = injector.get(c); const guard = injector.get(c);
if (guard.canLoad) { if (guard.canLoad) {
return wrapIntoObservable(guard.canLoad(route)); return wrapIntoObservable(guard.canLoad(route));

View File

@ -6,12 +6,6 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/mergeMap';
import 'rxjs/add/operator/mergeAll';
import 'rxjs/add/operator/reduce';
import 'rxjs/add/operator/every';
import {Location} from '@angular/common'; import {Location} from '@angular/common';
import {Compiler, ComponentFactoryResolver, Injector, NgModuleFactoryLoader, ReflectiveInjector, Type} from '@angular/core'; import {Compiler, ComponentFactoryResolver, Injector, NgModuleFactoryLoader, ReflectiveInjector, Type} from '@angular/core';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
@ -19,6 +13,11 @@ import {Subject} from 'rxjs/Subject';
import {Subscription} from 'rxjs/Subscription'; import {Subscription} from 'rxjs/Subscription';
import {from} from 'rxjs/observable/from'; import {from} from 'rxjs/observable/from';
import {of } from 'rxjs/observable/of'; import {of } from 'rxjs/observable/of';
import {every} from 'rxjs/operator/every';
import {map} from 'rxjs/operator/map';
import {mergeAll} from 'rxjs/operator/mergeAll';
import {mergeMap} from 'rxjs/operator/mergeMap';
import {reduce} from 'rxjs/operator/reduce';
import {applyRedirects} from './apply_redirects'; import {applyRedirects} from './apply_redirects';
import {ResolveData, Routes, validateConfig} from './config'; import {ResolveData, Routes, validateConfig} from './config';
@ -476,41 +475,43 @@ export class Router {
const storedState = this.currentRouterState; const storedState = this.currentRouterState;
const storedUrl = this.currentUrlTree; const storedUrl = this.currentUrlTree;
applyRedirects(this.injector, this.configLoader, url, this.config) const redirectsApplied$ = applyRedirects(this.injector, this.configLoader, url, this.config);
.mergeMap(u => {
appliedUrl = u;
return recognize(
this.rootComponentType, this.config, appliedUrl, this.serializeUrl(appliedUrl));
})
.map((newRouterStateSnapshot) => { const snapshot$ = mergeMap.call(redirectsApplied$, (u: UrlTree) => {
this.routerEvents.next(new RoutesRecognized( appliedUrl = u;
id, this.serializeUrl(url), this.serializeUrl(appliedUrl), newRouterStateSnapshot)); return recognize(
return newRouterStateSnapshot; this.rootComponentType, this.config, appliedUrl, this.serializeUrl(appliedUrl));
});
}) const emitRecognzied$ = map.call(snapshot$, (newRouterStateSnapshot: RouterStateSnapshot) => {
.map((routerStateSnapshot) => { this.routerEvents.next(new RoutesRecognized(
return createRouterState(routerStateSnapshot, this.currentRouterState); id, this.serializeUrl(url), this.serializeUrl(appliedUrl), newRouterStateSnapshot));
return newRouterStateSnapshot;
});
}) const routerState$ = map.call(emitRecognzied$, (routerStateSnapshot: RouterStateSnapshot) => {
.map((newState: RouterState) => { return createRouterState(routerStateSnapshot, this.currentRouterState);
state = newState; });
preActivation =
new PreActivation(state.snapshot, this.currentRouterState.snapshot, this.injector);
preActivation.traverse(this.outletMap);
})
.mergeMap(_ => {
return preActivation.checkGuards();
}) const preactivation$ = map.call(routerState$, (newState: RouterState) => {
.mergeMap(shouldActivate => { state = newState;
if (shouldActivate) { preActivation =
return preActivation.resolveData().map(() => shouldActivate); new PreActivation(state.snapshot, this.currentRouterState.snapshot, this.injector);
} else { preActivation.traverse(this.outletMap);
return of (shouldActivate); });
}
}) const preactivation2$ =
mergeMap.call(preactivation$, () => { return preActivation.checkGuards(); });
const resolveData$ = mergeMap.call(preactivation2$, (shouldActivate: boolean) => {
if (shouldActivate) {
return map.call(preActivation.resolveData(), () => shouldActivate);
} else {
return of (shouldActivate);
}
});
resolveData$
.forEach((shouldActivate: boolean) => { .forEach((shouldActivate: boolean) => {
if (!shouldActivate || id !== this.navigationId) { if (!shouldActivate || id !== this.navigationId) {
navigationIsSuccessful = false; navigationIsSuccessful = false;
@ -595,34 +596,34 @@ export class PreActivation {
checkGuards(): Observable<boolean> { checkGuards(): Observable<boolean> {
if (this.checks.length === 0) return of (true); if (this.checks.length === 0) return of (true);
return from(this.checks) const checks$ = from(this.checks);
.map(s => { const runningChecks$ = map.call(checks$, (s: any) => {
if (s instanceof CanActivate) { if (s instanceof CanActivate) {
return andObservables( return andObservables(
from([this.runCanActivateChild(s.path), this.runCanActivate(s.route)])); from([this.runCanActivateChild(s.path), this.runCanActivate(s.route)]));
} else if (s instanceof CanDeactivate) { } else if (s instanceof CanDeactivate) {
// workaround https://github.com/Microsoft/TypeScript/issues/7271 // workaround https://github.com/Microsoft/TypeScript/issues/7271
const s2 = s as CanDeactivate; const s2 = s as CanDeactivate;
return this.runCanDeactivate(s2.component, s2.route); return this.runCanDeactivate(s2.component, s2.route);
} else { } else {
throw new Error('Cannot be reached'); throw new Error('Cannot be reached');
} }
}) });
.mergeAll() const mergedChecks$ = mergeAll.call(runningChecks$);
.every(result => result === true); return every.call(mergedChecks$, (result: any) => result === true);
} }
resolveData(): Observable<any> { resolveData(): Observable<any> {
if (this.checks.length === 0) return of (null); if (this.checks.length === 0) return of (null);
return from(this.checks) const checks$ = from(this.checks);
.mergeMap(s => { const runningChecks$ = mergeMap.call(checks$, (s: any) => {
if (s instanceof CanActivate) { if (s instanceof CanActivate) {
return this.runResolve(s.route); return this.runResolve(s.route);
} else { } else {
return of (null); return of (null);
} }
}) });
.reduce((_, __) => _); return reduce.call(runningChecks$, (_: any, __: any) => _);
} }
private traverseChildRoutes( private traverseChildRoutes(
@ -706,7 +707,7 @@ export class PreActivation {
private runCanActivate(future: ActivatedRouteSnapshot): Observable<boolean> { private runCanActivate(future: ActivatedRouteSnapshot): Observable<boolean> {
const canActivate = future._routeConfig ? future._routeConfig.canActivate : null; const canActivate = future._routeConfig ? future._routeConfig.canActivate : null;
if (!canActivate || canActivate.length === 0) return of (true); if (!canActivate || canActivate.length === 0) return of (true);
const obs = from(canActivate).map(c => { const obs = map.call(from(canActivate), (c: any) => {
const guard = this.getToken(c, future); const guard = this.getToken(c, future);
if (guard.canActivate) { if (guard.canActivate) {
return wrapIntoObservable(guard.canActivate(future, this.future)); return wrapIntoObservable(guard.canActivate(future, this.future));
@ -725,8 +726,8 @@ export class PreActivation {
.map(p => this.extractCanActivateChild(p)) .map(p => this.extractCanActivateChild(p))
.filter(_ => _ !== null); .filter(_ => _ !== null);
return andObservables(from(canActivateChildGuards).map(d => { return andObservables(map.call(from(canActivateChildGuards), (d: any) => {
const obs = from(d.guards).map(c => { const obs = map.call(from(d.guards), (c: any) => {
const guard = this.getToken(c, c.node); const guard = this.getToken(c, c.node);
if (guard.canActivateChild) { if (guard.canActivateChild) {
return wrapIntoObservable(guard.canActivateChild(future, this.future)); return wrapIntoObservable(guard.canActivateChild(future, this.future));
@ -748,22 +749,21 @@ export class PreActivation {
private runCanDeactivate(component: Object, curr: ActivatedRouteSnapshot): Observable<boolean> { private runCanDeactivate(component: Object, curr: ActivatedRouteSnapshot): Observable<boolean> {
const canDeactivate = curr && curr._routeConfig ? curr._routeConfig.canDeactivate : null; const canDeactivate = curr && curr._routeConfig ? curr._routeConfig.canDeactivate : null;
if (!canDeactivate || canDeactivate.length === 0) return of (true); if (!canDeactivate || canDeactivate.length === 0) return of (true);
return from(canDeactivate) const canDeactivate$ = map.call(from(canDeactivate), (c: any) => {
.map(c => { const guard = this.getToken(c, curr);
const guard = this.getToken(c, curr); if (guard.canDeactivate) {
if (guard.canDeactivate) { return wrapIntoObservable(guard.canDeactivate(component, curr, this.curr));
return wrapIntoObservable(guard.canDeactivate(component, curr, this.curr)); } else {
} else { return wrapIntoObservable(guard(component, curr, this.curr));
return wrapIntoObservable(guard(component, curr, this.curr)); }
} });
}) const merged$ = mergeAll.call(canDeactivate$);
.mergeAll() return every.call(merged$, (result: any) => result === true);
.every(result => result === true);
} }
private runResolve(future: ActivatedRouteSnapshot): Observable<any> { private runResolve(future: ActivatedRouteSnapshot): Observable<any> {
const resolve = future._resolve; const resolve = future._resolve;
return this.resolveNode(resolve.current, future).map(resolvedData => { return map.call(this.resolveNode(resolve.current, future), (resolvedData: any): any => {
resolve.resolvedData = resolvedData; resolve.resolvedData = resolvedData;
future.data = merge(future.data, resolve.flattenedResolvedData); future.data = merge(future.data, resolve.flattenedResolvedData);
return null; return null;

View File

@ -10,6 +10,8 @@ import {Compiler, ComponentFactoryResolver, Injector, NgModuleFactory, NgModuleF
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise'; 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 {mergeMap} from 'rxjs/operator/mergeMap';
import {LoadChildren, Route} from './config'; import {LoadChildren, Route} from './config';
import {flatten, wrapIntoObservable} from './utils/collection'; import {flatten, wrapIntoObservable} from './utils/collection';
@ -29,7 +31,7 @@ export class RouterConfigLoader {
constructor(private loader: NgModuleFactoryLoader, private compiler: Compiler) {} constructor(private loader: NgModuleFactoryLoader, private compiler: Compiler) {}
load(parentInjector: Injector, loadChildren: LoadChildren): Observable<LoadedRouterConfig> { load(parentInjector: Injector, loadChildren: LoadChildren): Observable<LoadedRouterConfig> {
return this.loadModuleFactory(loadChildren).map(r => { return map.call(this.loadModuleFactory(loadChildren), (r: any) => {
const ref = r.create(parentInjector); const ref = r.create(parentInjector);
return new LoadedRouterConfig( return new LoadedRouterConfig(
flatten(ref.injector.get(ROUTES)), ref.injector, ref.componentFactoryResolver); flatten(ref.injector.get(ROUTES)), ref.injector, ref.componentFactoryResolver);
@ -41,9 +43,9 @@ export class RouterConfigLoader {
return fromPromise(this.loader.load(loadChildren)); return fromPromise(this.loader.load(loadChildren));
} else { } else {
const offlineMode = this.compiler instanceof Compiler; const offlineMode = this.compiler instanceof Compiler;
return wrapIntoObservable(loadChildren()) return mergeMap.call(
.mergeMap( wrapIntoObservable(loadChildren()),
t => offlineMode ? of (<any>t) : fromPromise(this.compiler.compileModuleAsync(t))); (t: any) => offlineMode ? of (<any>t) : fromPromise(this.compiler.compileModuleAsync(t)));
} }
} }
} }

View File

@ -6,12 +6,14 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import 'rxjs/add/operator/concatAll';
import 'rxjs/add/operator/last';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise'; import {fromPromise} from 'rxjs/observable/fromPromise';
import {of } from 'rxjs/observable/of'; import {of } from 'rxjs/observable/of';
import {concatAll} from 'rxjs/operator/concatAll';
import {every} from 'rxjs/operator/every';
import * as l from 'rxjs/operator/last';
import {map} from 'rxjs/operator/map';
import {mergeAll} from 'rxjs/operator/mergeAll';
import {PRIMARY_OUTLET} from '../shared'; import {PRIMARY_OUTLET} from '../shared';
@ -95,7 +97,7 @@ export function waitForMap<A, B>(
forEach(obj, (a: A, k: string) => { forEach(obj, (a: A, k: string) => {
if (k === PRIMARY_OUTLET) { if (k === PRIMARY_OUTLET) {
waitFor.push(fn(k, a).map((_: B) => { waitFor.push(map.call(fn(k, a), (_: B) => {
res[k] = _; res[k] = _;
return _; return _;
})); }));
@ -104,7 +106,7 @@ export function waitForMap<A, B>(
forEach(obj, (a: A, k: string) => { forEach(obj, (a: A, k: string) => {
if (k !== PRIMARY_OUTLET) { if (k !== PRIMARY_OUTLET) {
waitFor.push(fn(k, a).map((_: B) => { waitFor.push(map.call(fn(k, a), (_: B) => {
res[k] = _; res[k] = _;
return _; return _;
})); }));
@ -112,14 +114,17 @@ export function waitForMap<A, B>(
}); });
if (waitFor.length > 0) { if (waitFor.length > 0) {
return of (...waitFor).concatAll().last().map((last) => res); const concatted$ = concatAll.call(of (...waitFor));
const last$ = l.last.call(concatted$);
return map.call(last$, () => res);
} else { } else {
return of (res); return of (res);
} }
} }
export function andObservables(observables: Observable<Observable<any>>): Observable<boolean> { export function andObservables(observables: Observable<Observable<any>>): Observable<boolean> {
return observables.mergeAll().every(result => result === true); const merged$ = mergeAll.call(observables);
return every.call(merged$, (result: any) => result === true);
} }
export function wrapIntoObservable<T>(value: T | Promise<T>| Observable<T>): Observable<T> { export function wrapIntoObservable<T>(value: T | Promise<T>| Observable<T>): Observable<T> {

View File

@ -6,14 +6,13 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import 'rxjs/add/operator/map';
import {CommonModule, Location} from '@angular/common'; import {CommonModule, Location} from '@angular/common';
import {Component, NgModule, NgModuleFactoryLoader} from '@angular/core'; import {Component, NgModule, NgModuleFactoryLoader} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing'; import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/matchers'; import {expect} from '@angular/platform-browser/testing/matchers';
import {Observable} from 'rxjs/Observable'; import {Observable} from 'rxjs/Observable';
import {of } from 'rxjs/observable/of'; import {of } from 'rxjs/observable/of';
import {map} from 'rxjs/operator/map';
import {ActivatedRoute, ActivatedRouteSnapshot, CanActivate, CanDeactivate, Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Params, Resolve, Router, RouterModule, RouterStateSnapshot, RoutesRecognized} from '../index'; import {ActivatedRoute, ActivatedRouteSnapshot, CanActivate, CanDeactivate, Event, NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Params, Resolve, Router, RouterModule, RouterStateSnapshot, RoutesRecognized} from '../index';
import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing'; import {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing';
@ -1791,7 +1790,7 @@ class TeamCmp {
recordedParams: Params[] = []; recordedParams: Params[] = [];
constructor(public route: ActivatedRoute) { constructor(public route: ActivatedRoute) {
this.id = route.params.map(p => p['id']); this.id = map.call(route.params, (p: any) => p['id']);
route.params.forEach(_ => this.recordedParams.push(_)); route.params.forEach(_ => this.recordedParams.push(_));
} }
} }
@ -1802,7 +1801,7 @@ class UserCmp {
recordedParams: Params[] = []; recordedParams: Params[] = [];
constructor(route: ActivatedRoute) { constructor(route: ActivatedRoute) {
this.name = route.params.map(p => p['name']); this.name = map.call(route.params, (p: any) => p['name']);
route.params.forEach(_ => this.recordedParams.push(_)); route.params.forEach(_ => this.recordedParams.push(_));
} }
} }
@ -1818,7 +1817,7 @@ class QueryParamsAndFragmentCmp {
fragment: Observable<string>; fragment: Observable<string>;
constructor(route: ActivatedRoute) { constructor(route: ActivatedRoute) {
this.name = route.queryParams.map(p => p['name']); this.name = map.call(route.queryParams, (p: any) => p['name']);
this.fragment = route.fragment; this.fragment = route.fragment;
} }
} }