Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NavigationCancel and NavigationError creates a cycle when used w… #80

Merged
merged 1 commit into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions modules/router-store/spec/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import {
ROUTER_CANCEL,
ROUTER_ERROR,
ROUTER_NAVIGATION,
RouterAction,
routerReducer,
StoreRouterConnectingModule,
RouterNavigationAction,
RouterCancelAction,
RouterAction,
} from '../src/index';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/first';
Expand Down Expand Up @@ -106,15 +104,18 @@ describe('integration spec', () => {
} else if (action.type === ROUTER_CANCEL) {
return {
url: action.payload.routerState.url.toString(),
storeState: action.payload.storeState,
storeState: action.payload.storeState.reducer,
lastAction: ROUTER_CANCEL,
};
} else {
return state;
}
};

createTestModule({ reducers: { reducer }, canActivate: () => false });
createTestModule({
reducers: { reducer, routerReducer },
canActivate: () => false,
});

const router: Router = TestBed.get(Router);
const store = TestBed.get(Store);
Expand All @@ -128,6 +129,7 @@ describe('integration spec', () => {
})
.then(r => {
expect(r).toEqual(false);

expect(log).toEqual([
{ type: 'router', event: 'NavigationStart', url: '/next' },
{ type: 'router', event: 'RoutesRecognized', url: '/next' },
Expand All @@ -140,9 +142,7 @@ describe('integration spec', () => {
state: {
url: '/next',
lastAction: ROUTER_CANCEL,
storeState: {
reducer: { url: '/next', lastAction: ROUTER_NAVIGATION },
},
storeState: { url: '/next', lastAction: ROUTER_NAVIGATION },
},
},
{ type: 'router', event: 'NavigationCancel', url: '/next' },
Expand All @@ -162,7 +162,7 @@ describe('integration spec', () => {
} else if (action.type === ROUTER_ERROR) {
return {
url: action.payload.routerState.url.toString(),
storeState: action.payload.storeState,
storeState: action.payload.storeState.reducer,
lastAction: ROUTER_ERROR,
};
} else {
Expand All @@ -171,7 +171,7 @@ describe('integration spec', () => {
};

createTestModule({
reducers: { reducer },
reducers: { reducer, routerReducer },
canActivate: () => {
throw new Error('BOOM!');
},
Expand Down Expand Up @@ -202,9 +202,7 @@ describe('integration spec', () => {
state: {
url: '/next',
lastAction: ROUTER_ERROR,
storeState: {
reducer: { url: '/next', lastAction: ROUTER_NAVIGATION },
},
storeState: { url: '/next', lastAction: ROUTER_NAVIGATION },
},
},
{ type: 'router', event: 'NavigationError', url: '/next' },
Expand Down
46 changes: 24 additions & 22 deletions modules/router-store/src/router_store_module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ export class StoreRouterConnectingModule {
routerState: RouterStateSnapshot
) => {
this.routerState = routerState;
if (this.shouldDispatch()) this.dispatchEvent();
if (this.shouldDispatchRouterNavigation())
this.dispatchRouterNavigation();
return of(true);
};
}
Expand All @@ -178,21 +179,7 @@ export class StoreRouterConnectingModule {
});
}

private dispatchEvent(): void {
this.dispatchTriggeredByRouter = true;
try {
const payload = {
routerState: this.routerState,
event: this.lastRoutesRecognized,
};
this.store.dispatch({ type: ROUTER_NAVIGATION, payload });
} finally {
this.dispatchTriggeredByRouter = false;
this.navigationTriggeredByDispatch = false;
}
}

private shouldDispatch(): boolean {
private shouldDispatchRouterNavigation(): boolean {
if (!this.storeState['routerReducer']) return true;
return !this.navigationTriggeredByDispatch;
}
Expand All @@ -219,21 +206,36 @@ export class StoreRouterConnectingModule {
});
}

private dispatchRouterNavigation(): void {
this.dispatchRouterAction(ROUTER_NAVIGATION, {
routerState: this.routerState,
event: this.lastRoutesRecognized,
});
}

private dispatchRouterCancel(event: NavigationCancel): void {
const payload = {
this.dispatchRouterAction(ROUTER_CANCEL, {
routerState: this.routerState,
storeState: this.storeState,
event,
};
this.store.dispatch({ type: ROUTER_CANCEL, payload });
});
}

private dispatchRouterError(event: NavigationError): void {
const payload = {
this.dispatchRouterAction(ROUTER_ERROR, {
routerState: this.routerState,
storeState: this.storeState,
event,
};
this.store.dispatch({ type: ROUTER_ERROR, payload });
});
}

private dispatchRouterAction(type: string, payload: any): void {
this.dispatchTriggeredByRouter = true;
try {
this.store.dispatch({ type, payload });
} finally {
this.dispatchTriggeredByRouter = false;
this.navigationTriggeredByDispatch = false;
}
}
}