Skip to content

Commit 65ea7b6

Browse files
authored
feat: use deep compare for checking params (#57)
1 parent b70a11f commit 65ea7b6

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

libs/angular-routing/src/lib/route-params.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ export class RoutePath<T extends string = string> extends Observable<T> {}
99
export class RouteParams<T extends Params = Params> extends Observable<T> {}
1010

1111
export class QueryParams<T extends Params = Params> extends Observable<T> {}
12+
13+
export function compareParams(previous: Params, current: Params): boolean {
14+
return (
15+
previous === current || JSON.stringify(previous) === JSON.stringify(current)
16+
);
17+
}

libs/angular-routing/src/lib/router.component.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { pathToRegexp, match } from 'path-to-regexp';
1919

2020
import { Route, ActiveRoute } from './route';
2121
import { Router } from './router.service';
22-
import { Params } from './route-params.service';
22+
import { compareParams, Params } from './route-params.service';
2323

2424
@Component({
2525
// tslint:disable-next-line:component-selector
@@ -30,7 +30,9 @@ export class RouterComponent implements OnInit, OnDestroy {
3030
private destroy$ = new Subject();
3131

3232
private _activeRoute$ = new BehaviorSubject<ActiveRoute>(null);
33-
readonly activeRoute$ = this._activeRoute$.pipe(distinctUntilChanged());
33+
readonly activeRoute$ = this._activeRoute$.pipe(
34+
distinctUntilChanged(this.compareActiveRoutes)
35+
);
3436

3537
private _routes$ = new BehaviorSubject<Route[]>([]);
3638
readonly routes$ = this._routes$.pipe(
@@ -125,4 +127,22 @@ export class RouterComponent implements OnInit, OnDestroy {
125127
ngOnDestroy() {
126128
this.destroy$.next();
127129
}
130+
131+
private compareActiveRoutes(
132+
previous: ActiveRoute,
133+
current: ActiveRoute
134+
): boolean {
135+
if (previous === current) {
136+
return true;
137+
}
138+
if (!previous) {
139+
return false;
140+
}
141+
return (
142+
previous.path === current.path &&
143+
compareParams(previous.params, current.params) &&
144+
previous.route.path === current.route.path &&
145+
previous.route.options.exact === current.route.options.exact
146+
);
147+
}
128148
}

libs/angular-routing/src/lib/router.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { distinctUntilChanged } from 'rxjs/operators';
77
import * as queryString from 'query-string';
88

99
import { UrlParser } from './url-parser';
10-
import { Params } from './route-params.service';
10+
import { Params, compareParams } from './route-params.service';
1111

1212
@Injectable({
1313
providedIn: 'root',
@@ -17,7 +17,9 @@ export class Router {
1717
readonly url$ = this._url$.pipe(distinctUntilChanged());
1818

1919
private _queryParams$ = new BehaviorSubject<Params>({});
20-
readonly queryParams$ = this._queryParams$.pipe(distinctUntilChanged());
20+
readonly queryParams$ = this._queryParams$.pipe(
21+
distinctUntilChanged(compareParams)
22+
);
2123

2224
private _hash$ = new BehaviorSubject<string>('');
2325
readonly hash$ = this._hash$.pipe(distinctUntilChanged());

0 commit comments

Comments
 (0)