Skip to content

Commit c350ba2

Browse files
vsavkinvicb
authored andcommitted
fix(router): do not use rx/add/operator
1 parent 6e40ef0 commit c350ba2

File tree

6 files changed

+177
-164
lines changed

6 files changed

+177
-164
lines changed

modules/@angular/router/rollup.config.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,28 @@ export default {
1010
'@angular/platform-browser': 'ng.platformBrowser',
1111
'@angular/platform-browser-dynamic': 'ng.platformBrowserDynamic',
1212

13+
'rxjs/BehaviorSubject': 'Rx',
1314
'rxjs/Observable': 'Rx',
1415
'rxjs/Subject': 'Rx',
15-
'rxjs/BehaviorSubject': 'Rx',
16-
'rxjs/Observer': 'Rx',
1716
'rxjs/Subscription': 'Rx',
17+
'rxjs/util/EmptyError': 'Rx',
1818

19-
'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.
20-
'rxjs/add/operator/map': 'Rx.Observable.prototype',
21-
'rxjs/add/operator/mergeAll': 'Rx.Observable.prototype',
22-
'rxjs/add/operator/concatAll': 'Rx.Observable.prototype',
23-
'rxjs/add/operator/mergeMap': 'Rx.Observable.prototype',
24-
'rxjs/add/operator/reduce': 'Rx.Observable.prototype',
25-
'rxjs/add/operator/every': 'Rx.Observable.prototype',
26-
'rxjs/add/operator/first': 'Rx.Observable.prototype',
27-
'rxjs/add/operator/catch': 'Rx.Observable.prototype',
28-
'rxjs/add/operator/last': 'Rx.Observable.prototype',
29-
'rxjs/add/operator/toPromise': 'Rx.Observable.prototype',
3019
'rxjs/observable/from': 'Rx.Observable',
3120
'rxjs/observable/fromPromise': 'Rx.Observable',
3221
'rxjs/observable/forkJoin': 'Rx.Observable',
3322
'rxjs/observable/of': 'Rx.Observable',
34-
'rxjs/util/EmptyError': 'Rx.EmptyError'
35-
}
23+
24+
'rxjs/operator/toPromise': 'Rx.Observable.prototype',
25+
'rxjs/operator/map': 'Rx.Observable.prototype',
26+
'rxjs/operator/mergeAll': 'Rx.Observable.prototype',
27+
'rxjs/operator/concatAll': 'Rx.Observable.prototype',
28+
'rxjs/operator/mergeMap': 'Rx.Observable.prototype',
29+
'rxjs/operator/reduce': 'Rx.Observable.prototype',
30+
'rxjs/operator/every': 'Rx.Observable.prototype',
31+
'rxjs/operator/first': 'Rx.Observable.prototype',
32+
'rxjs/operator/catch': 'Rx.Observable.prototype',
33+
'rxjs/operator/last': 'Rx.Observable.prototype'
34+
},
35+
plugins: [
36+
]
3637
}

modules/@angular/router/src/apply_redirects.ts

Lines changed: 64 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import 'rxjs/add/operator/first';
10-
import 'rxjs/add/operator/catch';
11-
import 'rxjs/add/operator/concatAll';
12-
139
import {Injector} from '@angular/core';
1410
import {Observable} from 'rxjs/Observable';
1511
import {Observer} from 'rxjs/Observer';
1612
import {from} from 'rxjs/observable/from';
1713
import {of } from 'rxjs/observable/of';
14+
import {_catch} from 'rxjs/operator/catch';
15+
import {concatAll} from 'rxjs/operator/concatAll';
16+
import {first} from 'rxjs/operator/first';
17+
import {map} from 'rxjs/operator/map';
18+
import {mergeMap} from 'rxjs/operator/mergeMap';
1819
import {EmptyError} from 'rxjs/util/EmptyError';
1920

2021
import {Route, Routes} from './config';
@@ -62,34 +63,38 @@ class ApplyRedirects {
6263
private urlTree: UrlTree, private config: Routes) {}
6364

6465
apply(): Observable<UrlTree> {
65-
return this.expandSegmentGroup(this.injector, this.config, this.urlTree.root, PRIMARY_OUTLET)
66-
.map(rootSegmentGroup => this.createUrlTree(rootSegmentGroup))
67-
.catch(e => {
68-
if (e instanceof AbsoluteRedirect) {
69-
// after an absolute redirect we do not apply any more redirects!
70-
this.allowRedirects = false;
71-
const group =
72-
new UrlSegmentGroup([], {[PRIMARY_OUTLET]: new UrlSegmentGroup(e.segments, {})});
73-
// we need to run matching, so we can fetch all lazy-loaded modules
74-
return this.match(group);
75-
} else if (e instanceof NoMatch) {
76-
throw this.noMatchError(e);
77-
} else {
78-
throw e;
79-
}
80-
});
66+
const expanded$ =
67+
this.expandSegmentGroup(this.injector, this.config, this.urlTree.root, PRIMARY_OUTLET);
68+
const urlTrees$ = map.call(
69+
expanded$, (rootSegmentGroup: UrlSegmentGroup) => this.createUrlTree(rootSegmentGroup));
70+
return _catch.call(urlTrees$, (e: any) => {
71+
if (e instanceof AbsoluteRedirect) {
72+
// after an absolute redirect we do not apply any more redirects!
73+
this.allowRedirects = false;
74+
const group =
75+
new UrlSegmentGroup([], {[PRIMARY_OUTLET]: new UrlSegmentGroup(e.segments, {})});
76+
// we need to run matching, so we can fetch all lazy-loaded modules
77+
return this.match(group);
78+
} else if (e instanceof NoMatch) {
79+
throw this.noMatchError(e);
80+
} else {
81+
throw e;
82+
}
83+
});
8184
}
8285

8386
private match(segmentGroup: UrlSegmentGroup): Observable<UrlTree> {
84-
return this.expandSegmentGroup(this.injector, this.config, segmentGroup, PRIMARY_OUTLET)
85-
.map(rootSegmentGroup => this.createUrlTree(rootSegmentGroup))
86-
.catch((e): Observable<UrlTree> => {
87-
if (e instanceof NoMatch) {
88-
throw this.noMatchError(e);
89-
} else {
90-
throw e;
91-
}
92-
});
87+
const expanded$ =
88+
this.expandSegmentGroup(this.injector, this.config, segmentGroup, PRIMARY_OUTLET);
89+
const mapped$ = map.call(
90+
expanded$, (rootSegmentGroup: UrlSegmentGroup) => this.createUrlTree(rootSegmentGroup));
91+
return _catch.call(mapped$, (e: any): Observable<UrlTree> => {
92+
if (e instanceof NoMatch) {
93+
throw this.noMatchError(e);
94+
} else {
95+
throw e;
96+
}
97+
});
9398
}
9499

95100
private noMatchError(e: NoMatch): any {
@@ -107,8 +112,9 @@ class ApplyRedirects {
107112
injector: Injector, routes: Route[], segmentGroup: UrlSegmentGroup,
108113
outlet: string): Observable<UrlSegmentGroup> {
109114
if (segmentGroup.segments.length === 0 && segmentGroup.hasChildren()) {
110-
return this.expandChildren(injector, routes, segmentGroup)
111-
.map(children => new UrlSegmentGroup([], children));
115+
return map.call(
116+
this.expandChildren(injector, routes, segmentGroup),
117+
(children: any) => new UrlSegmentGroup([], children));
112118
} else {
113119
return this.expandSegment(
114120
injector, segmentGroup, routes, segmentGroup.segments, outlet, true);
@@ -125,22 +131,20 @@ class ApplyRedirects {
125131
private expandSegment(
126132
injector: Injector, segmentGroup: UrlSegmentGroup, routes: Route[], segments: UrlSegment[],
127133
outlet: string, allowRedirects: boolean): Observable<UrlSegmentGroup> {
128-
const processRoutes =
129-
of (...routes)
130-
.map(r => {
131-
return this
132-
.expandSegmentAgainstRoute(
133-
injector, segmentGroup, routes, r, segments, outlet, allowRedirects)
134-
.catch((e) => {
135-
if (e instanceof NoMatch)
136-
return of (null);
137-
else
138-
throw e;
139-
});
140-
})
141-
.concatAll();
142-
143-
return processRoutes.first(s => !!s).catch((e: any, _: any): Observable<UrlSegmentGroup> => {
134+
const routes$ = of (...routes);
135+
const processedRoutes$ = map.call(routes$, (r: any) => {
136+
const expanded$ = this.expandSegmentAgainstRoute(
137+
injector, segmentGroup, routes, r, segments, outlet, allowRedirects);
138+
return _catch.call(expanded$, (e: any) => {
139+
if (e instanceof NoMatch)
140+
return of (null);
141+
else
142+
throw e;
143+
});
144+
});
145+
const concattedProcessedRoutes$ = concatAll.call(processedRoutes$);
146+
const first$ = first.call(concattedProcessedRoutes$, (s: any) => !!s);
147+
return _catch.call(first$, (e: any, _: any): Observable<UrlSegmentGroup> => {
144148
if (e instanceof EmptyError) {
145149
throw new NoMatch(segmentGroup);
146150
} else {
@@ -214,25 +218,27 @@ class ApplyRedirects {
214218
if (!matched) return noMatch(rawSegmentGroup);
215219

216220
const rawSlicedSegments = segments.slice(lastChild);
217-
218-
return this.getChildConfig(injector, route).mergeMap(routerConfig => {
221+
const childConfig$ = this.getChildConfig(injector, route);
222+
return mergeMap.call(childConfig$, (routerConfig: any) => {
219223
const childInjector = routerConfig.injector;
220224
const childConfig = routerConfig.routes;
221225
const {segmentGroup, slicedSegments} =
222226
split(rawSegmentGroup, consumedSegments, rawSlicedSegments, childConfig);
223227

224228
if (slicedSegments.length === 0 && segmentGroup.hasChildren()) {
225-
return this.expandChildren(childInjector, childConfig, segmentGroup)
226-
.map(children => new UrlSegmentGroup(consumedSegments, children));
229+
const expanded$ = this.expandChildren(childInjector, childConfig, segmentGroup);
230+
return map.call(
231+
expanded$, (children: any) => new UrlSegmentGroup(consumedSegments, children));
227232

228233
} else if (childConfig.length === 0 && slicedSegments.length === 0) {
229234
return of (new UrlSegmentGroup(consumedSegments, {}));
230235

231236
} else {
232-
return this
233-
.expandSegment(
234-
childInjector, segmentGroup, childConfig, slicedSegments, PRIMARY_OUTLET, true)
235-
.map(cs => new UrlSegmentGroup(consumedSegments.concat(cs.segments), cs.children));
237+
const expanded$ = this.expandSegment(
238+
childInjector, segmentGroup, childConfig, slicedSegments, PRIMARY_OUTLET, true);
239+
return map.call(
240+
expanded$,
241+
(cs: any) => new UrlSegmentGroup(consumedSegments.concat(cs.segments), cs.children));
236242
}
237243
});
238244
}
@@ -242,12 +248,12 @@ class ApplyRedirects {
242248
if (route.children) {
243249
return of (new LoadedRouterConfig(route.children, injector, null));
244250
} else if (route.loadChildren) {
245-
return runGuards(injector, route).mergeMap(shouldLoad => {
251+
return mergeMap.call(runGuards(injector, route), (shouldLoad: any) => {
246252
if (shouldLoad) {
247253
if ((<any>route)._loadedConfig) {
248254
return of ((<any>route)._loadedConfig);
249255
} else {
250-
return this.configLoader.load(injector, route.loadChildren).map(r => {
256+
return map.call(this.configLoader.load(injector, route.loadChildren), (r: any) => {
251257
(<any>route)._loadedConfig = r;
252258
return r;
253259
});
@@ -265,7 +271,7 @@ class ApplyRedirects {
265271
function runGuards(injector: Injector, route: Route): Observable<boolean> {
266272
const canLoad = route.canLoad;
267273
if (!canLoad || canLoad.length === 0) return of (true);
268-
const obs = from(canLoad).map(c => {
274+
const obs = map.call(from(canLoad), (c: any) => {
269275
const guard = injector.get(c);
270276
if (guard.canLoad) {
271277
return wrapIntoObservable(guard.canLoad(route));

0 commit comments

Comments
 (0)