fix(router): do not fire events on 'duplicate' location events

This commit is contained in:
vsavkin 2016-07-12 16:13:16 -07:00
parent 5cf58971f1
commit 0b54e3cf0a
2 changed files with 23 additions and 2 deletions

View File

@ -288,7 +288,14 @@ export class Router {
private setUpLocationChangeListener(): void { private setUpLocationChangeListener(): void {
this.locationSubscription = <any>this.location.subscribe((change) => { this.locationSubscription = <any>this.location.subscribe((change) => {
return this.scheduleNavigation(this.urlSerializer.parse(change['url']), change['pop']); const tree = this.urlSerializer.parse(change['url']);
// we fire multiple events for a single URL change
// we should navigate only once
if (this.currentUrlTree.toString() !== tree.toString()) {
return this.scheduleNavigation(tree, change['pop']);
} else {
return null;
}
}); });
} }

View File

@ -111,13 +111,27 @@ describe('Integration', () => {
children: [{path: 'user/:name', component: UserCmp}] children: [{path: 'user/:name', component: UserCmp}]
}]); }]);
const recordedEvents: any = [];
router.events.forEach(e => recordedEvents.push(e));
router.navigateByUrl('/team/22/user/victor'); router.navigateByUrl('/team/22/user/victor');
advance(fixture); advance(fixture);
(<any>location).simulateHashChange('/team/22/user/fedor'); (<any>location).simulateHashChange('/team/22/user/fedor');
advance(fixture); advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 [ user fedor, right: ]'); (<any>location).simulateUrlPop('/team/22/user/fedor');
advance(fixture);
expect(fixture.debugElement.nativeElement).toHaveText('team 22 { user fedor, right: }');
expectEvents(recordedEvents, [
[NavigationStart, '/team/22/user/victor'], [RoutesRecognized, '/team/22/user/victor'],
[NavigationEnd, '/team/22/user/victor'],
[NavigationStart, '/team/22/user/fedor'], [RoutesRecognized, '/team/22/user/fedor'],
[NavigationEnd, '/team/22/user/fedor']
]);
}))); })));
it('should update the location when the matched route does not change', it('should update the location when the matched route does not change',