From a4debf14e38c375b66a0868596fa954efafa281d Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Tue, 22 Feb 2022 16:26:53 -0800 Subject: [PATCH] refactor(router): Update match result to be more explicit (#45170) The `lastChild` property in the match result is only used to compute the remaining segments (i.e. the ones which were not consumed). The updated type here makes it easier to use and more clear. PR Close #45170 --- packages/router/src/apply_redirects.ts | 10 ++++------ packages/router/src/recognize.ts | 6 +++--- packages/router/src/utils/config_matching.ts | 8 ++++---- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/router/src/apply_redirects.ts b/packages/router/src/apply_redirects.ts index 2cc993e10d2..52e8a1d226f 100644 --- a/packages/router/src/apply_redirects.ts +++ b/packages/router/src/apply_redirects.ts @@ -259,7 +259,7 @@ class ApplyRedirects { private expandRegularSegmentAgainstRouteUsingRedirect( ngModule: NgModuleRef, segmentGroup: UrlSegmentGroup, routes: Route[], route: Route, segments: UrlSegment[], outlet: string): Observable { - const {matched, consumedSegments, lastChild, positionalParamSegments} = + const {matched, consumedSegments, remainingSegments, positionalParamSegments} = match(segmentGroup, route, segments); if (!matched) return noMatch(segmentGroup); @@ -271,8 +271,7 @@ class ApplyRedirects { return this.lineralizeSegments(route, newTree).pipe(mergeMap((newSegments: UrlSegment[]) => { return this.expandSegment( - ngModule, segmentGroup, routes, newSegments.concat(segments.slice(lastChild)), outlet, - false); + ngModule, segmentGroup, routes, newSegments.concat(remainingSegments), outlet, false); })); } @@ -292,10 +291,9 @@ class ApplyRedirects { return of(new UrlSegmentGroup(segments, {})); } - const {matched, consumedSegments, lastChild} = match(rawSegmentGroup, route, segments); + const {matched, consumedSegments, remainingSegments} = match(rawSegmentGroup, route, segments); if (!matched) return noMatch(rawSegmentGroup); - const rawSlicedSegments = segments.slice(lastChild); const childConfig$ = this.getChildConfig(ngModule, route, segments); return childConfig$.pipe(mergeMap((routerConfig: LoadedRouterConfig) => { @@ -303,7 +301,7 @@ class ApplyRedirects { const childConfig = routerConfig.routes; const {segmentGroup: splitSegmentGroup, slicedSegments} = - split(rawSegmentGroup, consumedSegments, rawSlicedSegments, childConfig); + split(rawSegmentGroup, consumedSegments, remainingSegments, childConfig); // See comment on the other call to `split` about why this is necessary. const segmentGroup = new UrlSegmentGroup(splitSegmentGroup.segments, splitSegmentGroup.children); diff --git a/packages/router/src/recognize.ts b/packages/router/src/recognize.ts index cb12aa8172f..9df0c138694 100644 --- a/packages/router/src/recognize.ts +++ b/packages/router/src/recognize.ts @@ -155,7 +155,7 @@ export class Recognizer { let snapshot: ActivatedRouteSnapshot; let consumedSegments: UrlSegment[] = []; - let rawSlicedSegments: UrlSegment[] = []; + let remainingSegments: UrlSegment[] = []; if (route.path === '**') { const params = segments.length > 0 ? last(segments)!.parameters : {}; @@ -170,7 +170,7 @@ export class Recognizer { return null; } consumedSegments = result.consumedSegments; - rawSlicedSegments = segments.slice(result.lastChild); + remainingSegments = result.remainingSegments; snapshot = new ActivatedRouteSnapshot( consumedSegments, result.parameters, Object.freeze({...this.urlTree.queryParams}), @@ -182,7 +182,7 @@ export class Recognizer { const childConfig: Route[] = getChildConfig(route); const {segmentGroup, slicedSegments} = split( - rawSegment, consumedSegments, rawSlicedSegments, + rawSegment, consumedSegments, remainingSegments, // Filter out routes with redirectTo because we are trying to create activated route // snapshots and don't handle redirects here. That should have been done in // `applyRedirects`. diff --git a/packages/router/src/utils/config_matching.ts b/packages/router/src/utils/config_matching.ts index 52e625e8a3c..40ace3f9abf 100644 --- a/packages/router/src/utils/config_matching.ts +++ b/packages/router/src/utils/config_matching.ts @@ -16,7 +16,7 @@ import {getOutlet} from './config'; export interface MatchResult { matched: boolean; consumedSegments: UrlSegment[]; - lastChild: number; + remainingSegments: UrlSegment[]; parameters: {[k: string]: string}; positionalParamSegments: {[k: string]: UrlSegment}; } @@ -24,7 +24,7 @@ export interface MatchResult { const noMatch: MatchResult = { matched: false, consumedSegments: [], - lastChild: 0, + remainingSegments: [], parameters: {}, positionalParamSegments: {} }; @@ -39,7 +39,7 @@ export function match( return { matched: true, consumedSegments: [], - lastChild: 0, + remainingSegments: segments, parameters: {}, positionalParamSegments: {} }; @@ -60,7 +60,7 @@ export function match( return { matched: true, consumedSegments: res.consumed, - lastChild: res.consumed.length, + remainingSegments: segments.slice(res.consumed.length), // TODO(atscott): investigate combining parameters and positionalParamSegments parameters, positionalParamSegments: res.posParams ?? {}