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
This commit is contained in:
Andrew Scott
2022-02-22 16:26:53 -08:00
committed by Jessica Janiuk
parent c6e64aa8ad
commit a4debf14e3
3 changed files with 11 additions and 13 deletions
+4 -6
View File
@@ -259,7 +259,7 @@ class ApplyRedirects {
private expandRegularSegmentAgainstRouteUsingRedirect(
ngModule: NgModuleRef<any>, segmentGroup: UrlSegmentGroup, routes: Route[], route: Route,
segments: UrlSegment[], outlet: string): Observable<UrlSegmentGroup> {
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);
+3 -3
View File
@@ -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`.
+4 -4
View File
@@ -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 ?? {}