88
99import { Injector , runInInjectionContext , ɵRuntimeError as RuntimeError } from '@angular/core' ;
1010import { Observable , of , throwError } from 'rxjs' ;
11+ import { map } from 'rxjs/operators' ;
1112
1213import { RuntimeErrorCode } from './errors' ;
1314import { NavigationCancellationCode } from './events' ;
@@ -16,6 +17,7 @@ import {navigationCancelingError} from './navigation_canceling_error';
1617import { ActivatedRouteSnapshot } from './router_state' ;
1718import { Params , PRIMARY_OUTLET } from './shared' ;
1819import { UrlSegment , UrlSegmentGroup , UrlSerializer , UrlTree } from './url_tree' ;
20+ import { wrapIntoObservable } from './utils/collection' ;
1921
2022export class NoMatch {
2123 public segmentGroup : UrlSegmentGroup | null ;
@@ -88,31 +90,26 @@ export class ApplyRedirects {
8890 posParams : { [ k : string ] : UrlSegment } ,
8991 currentSnapshot : ActivatedRouteSnapshot ,
9092 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 ) {
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+ } ) ,
111112 ) ;
112- if ( redirectTo [ 0 ] === '/' ) {
113- throw new AbsoluteRedirect ( newTree ) ;
114- }
115- return newTree ;
116113 }
117114
118115 applyRedirectCreateUrlTree (
@@ -199,3 +196,20 @@ export class ApplyRedirects {
199196 return redirectToUrlSegment ;
200197 }
201198}
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