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-dynamic': 'ng.platformBrowserDynamic',
'rxjs/BehaviorSubject': 'Rx',
'rxjs/Observable': 'Rx',
'rxjs/Subject': 'Rx',
'rxjs/BehaviorSubject': 'Rx',
'rxjs/Observer': '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/fromPromise': 'Rx.Observable',
'rxjs/observable/forkJoin': '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
*/
import 'rxjs/add/operator/first';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/concatAll';
import {Injector} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
import {from} from 'rxjs/observable/from';
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 {Route, Routes} from './config';
@ -62,9 +63,11 @@ class ApplyRedirects {
private urlTree: UrlTree, private config: Routes) {}
apply(): Observable<UrlTree> {
return this.expandSegmentGroup(this.injector, this.config, this.urlTree.root, PRIMARY_OUTLET)
.map(rootSegmentGroup => this.createUrlTree(rootSegmentGroup))
.catch(e => {
const expanded$ =
this.expandSegmentGroup(this.injector, this.config, this.urlTree.root, PRIMARY_OUTLET);
const urlTrees$ = map.call(
expanded$, (rootSegmentGroup: UrlSegmentGroup) => this.createUrlTree(rootSegmentGroup));
return _catch.call(urlTrees$, (e: any) => {
if (e instanceof AbsoluteRedirect) {
// after an absolute redirect we do not apply any more redirects!
this.allowRedirects = false;
@ -81,9 +84,11 @@ class ApplyRedirects {
}
private match(segmentGroup: UrlSegmentGroup): Observable<UrlTree> {
return this.expandSegmentGroup(this.injector, this.config, segmentGroup, PRIMARY_OUTLET)
.map(rootSegmentGroup => this.createUrlTree(rootSegmentGroup))
.catch((e): Observable<UrlTree> => {
const expanded$ =
this.expandSegmentGroup(this.injector, this.config, segmentGroup, PRIMARY_OUTLET);
const mapped$ = map.call(
expanded$, (rootSegmentGroup: UrlSegmentGroup) => this.createUrlTree(rootSegmentGroup));
return _catch.call(mapped$, (e: any): Observable<UrlTree> => {
if (e instanceof NoMatch) {
throw this.noMatchError(e);
} else {
@ -107,8 +112,9 @@ class ApplyRedirects {
injector: Injector, routes: Route[], segmentGroup: UrlSegmentGroup,
outlet: string): Observable<UrlSegmentGroup> {
if (segmentGroup.segments.length === 0 && segmentGroup.hasChildren()) {
return this.expandChildren(injector, routes, segmentGroup)
.map(children => new UrlSegmentGroup([], children));
return map.call(
this.expandChildren(injector, routes, segmentGroup),
(children: any) => new UrlSegmentGroup([], children));
} else {
return this.expandSegment(
injector, segmentGroup, routes, segmentGroup.segments, outlet, true);
@ -125,22 +131,20 @@ class ApplyRedirects {
private expandSegment(
injector: Injector, segmentGroup: UrlSegmentGroup, routes: Route[], segments: UrlSegment[],
outlet: string, allowRedirects: boolean): Observable<UrlSegmentGroup> {
const processRoutes =
of (...routes)
.map(r => {
return this
.expandSegmentAgainstRoute(
injector, segmentGroup, routes, r, segments, outlet, allowRedirects)
.catch((e) => {
const routes$ = of (...routes);
const processedRoutes$ = map.call(routes$, (r: any) => {
const expanded$ = this.expandSegmentAgainstRoute(
injector, segmentGroup, routes, r, segments, outlet, allowRedirects);
return _catch.call(expanded$, (e: any) => {
if (e instanceof NoMatch)
return of (null);
else
throw e;
});
})
.concatAll();
return processRoutes.first(s => !!s).catch((e: any, _: any): Observable<UrlSegmentGroup> => {
});
const concattedProcessedRoutes$ = concatAll.call(processedRoutes$);
const first$ = first.call(concattedProcessedRoutes$, (s: any) => !!s);
return _catch.call(first$, (e: any, _: any): Observable<UrlSegmentGroup> => {
if (e instanceof EmptyError) {
throw new NoMatch(segmentGroup);
} else {
@ -214,25 +218,27 @@ class ApplyRedirects {
if (!matched) return noMatch(rawSegmentGroup);
const rawSlicedSegments = segments.slice(lastChild);
return this.getChildConfig(injector, route).mergeMap(routerConfig => {
const childConfig$ = this.getChildConfig(injector, route);
return mergeMap.call(childConfig$, (routerConfig: any) => {
const childInjector = routerConfig.injector;
const childConfig = routerConfig.routes;
const {segmentGroup, slicedSegments} =
split(rawSegmentGroup, consumedSegments, rawSlicedSegments, childConfig);
if (slicedSegments.length === 0 && segmentGroup.hasChildren()) {
return this.expandChildren(childInjector, childConfig, segmentGroup)
.map(children => new UrlSegmentGroup(consumedSegments, children));
const expanded$ = this.expandChildren(childInjector, childConfig, segmentGroup);
return map.call(
expanded$, (children: any) => new UrlSegmentGroup(consumedSegments, children));
} else if (childConfig.length === 0 && slicedSegments.length === 0) {
return of (new UrlSegmentGroup(consumedSegments, {}));
} else {
return this
.expandSegment(
childInjector, segmentGroup, childConfig, slicedSegments, PRIMARY_OUTLET, true)
.map(cs => new UrlSegmentGroup(consumedSegments.concat(cs.segments), cs.children));
const expanded$ = this.expandSegment(
childInjector, segmentGroup, childConfig, slicedSegments, PRIMARY_OUTLET, true);
return map.call(
expanded$,
(cs: any) => new UrlSegmentGroup(consumedSegments.concat(cs.segments), cs.children));
}
});
}
@ -242,12 +248,12 @@ class ApplyRedirects {
if (route.children) {
return of (new LoadedRouterConfig(route.children, injector, null));
} else if (route.loadChildren) {
return runGuards(injector, route).mergeMap(shouldLoad => {
return mergeMap.call(runGuards(injector, route), (shouldLoad: any) => {
if (shouldLoad) {
if ((<any>route)._loadedConfig) {
return of ((<any>route)._loadedConfig);
} 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;
return r;
});
@ -265,7 +271,7 @@ class ApplyRedirects {
function runGuards(injector: Injector, route: Route): Observable<boolean> {
const canLoad = route.canLoad;
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);
if (guard.canLoad) {
return wrapIntoObservable(guard.canLoad(route));

View File

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

View File

@ -10,6 +10,8 @@ import {Compiler, ComponentFactoryResolver, Injector, NgModuleFactory, NgModuleF
import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise';
import {of } from 'rxjs/observable/of';
import {map} from 'rxjs/operator/map';
import {mergeMap} from 'rxjs/operator/mergeMap';
import {LoadChildren, Route} from './config';
import {flatten, wrapIntoObservable} from './utils/collection';
@ -29,7 +31,7 @@ export class RouterConfigLoader {
constructor(private loader: NgModuleFactoryLoader, private compiler: Compiler) {}
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);
return new LoadedRouterConfig(
flatten(ref.injector.get(ROUTES)), ref.injector, ref.componentFactoryResolver);
@ -41,9 +43,9 @@ export class RouterConfigLoader {
return fromPromise(this.loader.load(loadChildren));
} else {
const offlineMode = this.compiler instanceof Compiler;
return wrapIntoObservable(loadChildren())
.mergeMap(
t => offlineMode ? of (<any>t) : fromPromise(this.compiler.compileModuleAsync(t)));
return mergeMap.call(
wrapIntoObservable(loadChildren()),
(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
*/
import 'rxjs/add/operator/concatAll';
import 'rxjs/add/operator/last';
import {Observable} from 'rxjs/Observable';
import {fromPromise} from 'rxjs/observable/fromPromise';
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';
@ -95,7 +97,7 @@ export function waitForMap<A, B>(
forEach(obj, (a: A, k: string) => {
if (k === PRIMARY_OUTLET) {
waitFor.push(fn(k, a).map((_: B) => {
waitFor.push(map.call(fn(k, a), (_: B) => {
res[k] = _;
return _;
}));
@ -104,7 +106,7 @@ export function waitForMap<A, B>(
forEach(obj, (a: A, k: string) => {
if (k !== PRIMARY_OUTLET) {
waitFor.push(fn(k, a).map((_: B) => {
waitFor.push(map.call(fn(k, a), (_: B) => {
res[k] = _;
return _;
}));
@ -112,14 +114,17 @@ export function waitForMap<A, B>(
});
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 {
return of (res);
}
}
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> {

View File

@ -6,14 +6,13 @@
* found in the LICENSE file at https://angular.io/license
*/
import 'rxjs/add/operator/map';
import {CommonModule, Location} from '@angular/common';
import {Component, NgModule, NgModuleFactoryLoader} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, inject, tick} from '@angular/core/testing';
import {expect} from '@angular/platform-browser/testing/matchers';
import {Observable} from 'rxjs/Observable';
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 {RouterTestingModule, SpyNgModuleFactoryLoader} from '../testing';
@ -1791,7 +1790,7 @@ class TeamCmp {
recordedParams: Params[] = [];
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(_));
}
}
@ -1802,7 +1801,7 @@ class UserCmp {
recordedParams: Params[] = [];
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(_));
}
}
@ -1818,7 +1817,7 @@ class QueryParamsAndFragmentCmp {
fragment: Observable<string>;
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;
}
}