Skip to content

Commit

Permalink
fix(RouterStore): don't navigate on NavigationStart router event
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Oct 24, 2018
1 parent 8e9a167 commit 9530e1e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 7 additions & 0 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Router,
RoutesRecognized,
NavigationStart,
Event,
} from '@angular/router';
import { select, Selector, Store } from '@ngrx/store';
import { withLatestFrom } from 'rxjs/operators';
Expand Down Expand Up @@ -152,6 +153,7 @@ export class StoreRouterConnectingModule {
};
}

private lastEvent: Event | null = null;
private routerState: SerializedRouterStateSnapshot | null;
private storeState: any;
private trigger = RouterTrigger.NONE;
Expand Down Expand Up @@ -192,6 +194,9 @@ export class StoreRouterConnectingModule {
if (this.trigger === RouterTrigger.ROUTER) {
return;
}
if (this.lastEvent instanceof NavigationStart) {
return;
}

const url = routerStoreState.state.url;
if (this.router.url !== url) {
Expand All @@ -212,6 +217,8 @@ export class StoreRouterConnectingModule {
this.router.events
.pipe(withLatestFrom(this.store))
.subscribe(([event, storeState]) => {
this.lastEvent = event;

if (event instanceof NavigationStart) {
this.routerState = this.serializer.serialize(
this.router.routerState.snapshot
Expand Down
10 changes: 5 additions & 5 deletions projects/example-app/src/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export const reducers: ActionReducerMap<State> = {
export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
return (state: State, action: any): any => {
const result = reducer(state, action);
console.groupCollapsed(action.type);
console.log('prev state', state);
console.log('action', action);
console.log('next state', result);
console.groupEnd();
// console.groupCollapsed(action.type);
// console.log('prev state', state);
// console.log('action', action);
// console.log('next state', result);
// console.groupEnd();

return result;
};
Expand Down

0 comments on commit 9530e1e

Please sign in to comment.