Skip to content

Commit

Permalink
feat: add deep comparison of params (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Nov 6, 2020
1 parent 3757f39 commit 1075b43
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 18 deletions.
6 changes: 6 additions & 0 deletions libs/angular-routing/src/lib/route-params.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export class RoutePath<T extends string = string> extends Observable<T> {}
export class RouteParams<T extends Params = Params> extends Observable<T> {}

export class QueryParams<T extends Params = Params> extends Observable<T> {}

export function compareParams(previous: Params, current: Params): boolean {
return (
previous === current || JSON.stringify(previous) === JSON.stringify(current)
);
}
49 changes: 33 additions & 16 deletions libs/angular-routing/src/lib/router.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { pathToRegexp, match } from 'path-to-regexp';

import { Route, ActiveRoute } from './route';
import { Router } from './router.service';
import { Params } from './route-params.service';
import { compareParams, Params } from './route-params.service';

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

private _activeRoute$ = new BehaviorSubject<ActiveRoute>(null);
readonly activeRoute$ = this._activeRoute$.pipe(distinctUntilChanged());
readonly activeRoute$ = this._activeRoute$.pipe(
distinctUntilChanged(this.compareActiveRoutes)
);

private _routes$ = new BehaviorSubject<Route[]>([]);
readonly routes$ = this._routes$.pipe(
Expand Down Expand Up @@ -81,16 +83,6 @@ export class RouterComponent implements OnInit, OnDestroy {
.subscribe();
}

findRouteMatch(route: Route, url: string) {
const matchedRoute = route.matcher ? route.matcher.exec(url) : null;

if (matchedRoute) {
return matchedRoute;
}

return null;
}

setRoute(url: string, route: Route) {
const pathInfo = match(this.normalizePath(route.path), {
end: route.options.exact,
Expand All @@ -114,15 +106,40 @@ export class RouterComponent implements OnInit, OnDestroy {
return route;
}

setActiveRoute(active: ActiveRoute) {
ngOnDestroy() {
this.destroy$.next();
}

private findRouteMatch(route: Route, url: string) {
const matchedRoute = route.matcher ? route.matcher.exec(url) : null;

if (matchedRoute) {
return matchedRoute;
}

return null;
}

private setActiveRoute(active: ActiveRoute) {
this._activeRoute$.next(active);
}

normalizePath(path: string) {
private normalizePath(path: string) {
return this.location.normalize(path);
}

ngOnDestroy() {
this.destroy$.next();
private compareActiveRoutes(
previous: ActiveRoute,
current: ActiveRoute
): boolean {
if (previous === current) {
return true;
}
return (
previous.path === current.path &&
compareParams(previous.params, current.params) &&
previous.route.path === current.route.path &&
previous.route.options.exact === current.route.options.exact
);
}
}
6 changes: 4 additions & 2 deletions libs/angular-routing/src/lib/router.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { distinctUntilChanged } from 'rxjs/operators';
import * as queryString from 'query-string';

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

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

private _queryParams$ = new BehaviorSubject<Params>({});
readonly queryParams$ = this._queryParams$.pipe(distinctUntilChanged());
readonly queryParams$ = this._queryParams$.pipe(
distinctUntilChanged(compareParams)
);

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

0 comments on commit 1075b43

Please sign in to comment.