8
8
9
9
import { Injector , runInInjectionContext , ɵRuntimeError as RuntimeError } from '@angular/core' ;
10
10
import { Observable , of , throwError } from 'rxjs' ;
11
+ import { map } from 'rxjs/operators' ;
11
12
12
13
import { RuntimeErrorCode } from './errors' ;
13
14
import { NavigationCancellationCode } from './events' ;
@@ -16,6 +17,7 @@ import {navigationCancelingError} from './navigation_canceling_error';
16
17
import { ActivatedRouteSnapshot } from './router_state' ;
17
18
import { Params , PRIMARY_OUTLET } from './shared' ;
18
19
import { UrlSegment , UrlSegmentGroup , UrlSerializer , UrlTree } from './url_tree' ;
20
+ import { wrapIntoObservable } from './utils/collection' ;
19
21
20
22
export class NoMatch {
21
23
public segmentGroup : UrlSegmentGroup | null ;
@@ -88,31 +90,26 @@ export class ApplyRedirects {
88
90
posParams : { [ k : string ] : UrlSegment } ,
89
91
currentSnapshot : ActivatedRouteSnapshot ,
90
92
injector : Injector ,
91
- ) : UrlTree {
92
- if ( typeof redirectTo !== 'string' ) {
93
- const redirectToFn = redirectTo ;
94
- const { queryParams, fragment, routeConfig, url, outlet, params, data, title} =
95
- currentSnapshot ;
96
- const newRedirect = runInInjectionContext ( injector , ( ) =>
97
- redirectToFn ( { params, data, queryParams, fragment, routeConfig, url, outlet, title} ) ,
98
- ) ;
99
- if ( newRedirect instanceof UrlTree ) {
100
- throw new AbsoluteRedirect ( newRedirect ) ;
101
- }
102
-
103
- redirectTo = newRedirect ;
104
- }
105
-
106
- const newTree = this . applyRedirectCreateUrlTree (
107
- redirectTo ,
108
- this . urlSerializer . parse ( redirectTo ) ,
109
- segments ,
110
- posParams ,
93
+ ) : Observable < UrlTree > {
94
+ return getRedirectResult ( redirectTo , currentSnapshot , injector ) . pipe (
95
+ map ( ( redirect ) => {
96
+ if ( redirect instanceof UrlTree
8000
) {
97
+ throw new AbsoluteRedirect ( redirect ) ;
98
+ }
99
+
100
+ const newTree = this . applyRedirectCreateUrlTree (
101
+ redirect ,
102
+ this . urlSerializer . parse ( redirect ) ,
103
+ segments ,
104
+ posParams ,
105
+ ) ;
106
+
107
+ if ( redirect [ 0 ] === '/' ) {
108
+ throw new AbsoluteRedirect ( newTree ) ;
109
+ }
110
+ return newTree ;
111
+ } ) ,
111
112
) ;
112
- if ( redirectTo [ 0 ] === '/' ) {
113
- throw new AbsoluteRedirect ( newTree ) ;
114
- }
115
- return newTree ;
116
113
}
117
114
118
115
applyRedirectCreateUrlTree (
@@ -199,3 +196,20 @@ export class ApplyRedirects {
199
196
return redirectToUrlSegment ;
200
197
}
201
198
}
199
+
200
+ function getRedirectResult (
201
+ redirectTo : string | RedirectFunction ,
202
+ currentSnapshot : ActivatedRouteSnapshot ,
203
+ injector : Injector ,
204
+ ) : Observable < string | UrlTree > {
205
+ if ( typeof redirectTo === 'string' ) {
206
+ return of ( redirectTo ) ;
207
+ }
208
+ const redirectToFn = redirectTo ;
209
+ const { queryParams, fragment, routeConfig, url, outlet, params, data, title} = currentSnapshot ;
210
+ return wrapIntoObservable (
211
+ runInInjectionContext ( injector , ( ) =>
212
+ redirectToFn ( { params, data, queryParams, fragment, routeConfig, url, outlet, title} ) ,
213
+ ) ,
214
+ ) ;
215
+ }
0 commit comments