refactor(router): move routing into a single Observable stream (#25740)

This is a major refactor of how the router previously worked. There are a couple major advantages of this refactor, and future work will be built on top of it.

First, we will no longer have multiple navigations running at the same time. Previously, a new navigation wouldn't cause the old navigation to be cancelled and cleaned up. Instead, multiple navigations could be going at once, and we imperatively checked that we were operating on the most current `router.navigationId` as we progressed through the Observable streams. This had some major faults, the biggest of which was async races where an ongoing async action could result in a redirect once the async action completed, but there was no way to guarantee there weren't also other redirects that would be queued up by other async actions. After this refactor, there's a single Observable stream that will get cleaned up each time a new navigation is requested.

Additionally, the individual pieces of routing have been pulled out into their own operators. While this was needed in order to create one continuous stream, it also will allow future improvements to the testing APIs as things such as Guards or Resolvers should now be able to be tested in much more isolation.

* Add the new `router.transitions` observable of the new `NavigationTransition` type to contain the transition information
* Update `router.navigations` to pipe off of `router.transitions`
* Re-write navigation Observable flow to a single configured stream
* Refactor `switchMap` instead of the previous `mergeMap` to ensure new navigations cause a cancellation and cleanup of already running navigations
* Wire in existing error and cancellation logic so cancellation matches previous behavior

PR Close #25740
This commit is contained in:
Jason Aden
2018-08-15 15:27:18 -07:00
committed by Alex Rickabaugh
parent 4c0d4fc649
commit b7baf632c0
12 changed files with 415 additions and 364 deletions

View File

@ -291,41 +291,49 @@ describe('Integration', () => {
});
it('should execute navigations serialy',
fakeAsync(inject([Router, Location], (router: Router) => {
const fixture = createRoot(router, RootCmp);
// TODO(jasonaden): This test now fails because it relies on waiting on a guard to finish
// executing even after a new navigation has been scheduled. The previous implementation
// would do so, but ignore the result of any guards that are executing when a new navigation
// is scheduled.
router.resetConfig([
{path: 'a', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']},
{path: 'b', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']}
]);
// With new implementation, the current navigation will be unrolled and cleaned up so the
// new navigation can start immediately. This test therefore fails as it relies on that
// previous incorrect behavior.
xit('should execute navigations serialy',
fakeAsync(inject([Router, Location], (router: Router) => {
const fixture = createRoot(router, RootCmp);
router.navigateByUrl('/a');
tick(100);
fixture.detectChanges();
router.resetConfig([
{path: 'a', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']},
{path: 'b', component: SimpleCmp, canActivate: ['trueRightAway', 'trueIn2Seconds']}
]);
router.navigateByUrl('/b');
tick(100); // 200
fixture.detectChanges();
router.navigateByUrl('/a');
tick(100);
fixture.detectChanges();
expect(log).toEqual(['trueRightAway', 'trueIn2Seconds-start']);
router.navigateByUrl('/b');
tick(100); // 200
fixture.detectChanges();
tick(2000); // 2200
fixture.detectChanges();
expect(log).toEqual(['trueRightAway', 'trueIn2Seconds-start']);
expect(log).toEqual([
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
'trueIn2Seconds-start'
]);
tick(2000); // 2200
fixture.detectChanges();
tick(2000); // 4200
fixture.detectChanges();
expect(log).toEqual([
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
'trueIn2Seconds-start'
]);
expect(log).toEqual([
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
'trueIn2Seconds-start', 'trueIn2Seconds-end'
]);
})));
tick(2000); // 4200
fixture.detectChanges();
expect(log).toEqual([
'trueRightAway', 'trueIn2Seconds-start', 'trueIn2Seconds-end', 'trueRightAway',
'trueIn2Seconds-start', 'trueIn2Seconds-end'
]);
})));
});
it('Should work inside ChangeDetectionStrategy.OnPush components', fakeAsync(() => {
@ -962,7 +970,6 @@ describe('Integration', () => {
locationUrlBeforeEmittingError = location.path();
}
});
router.navigateByUrl('/throwing').catch(() => null);
advance(fixture);