-
Notifications
You must be signed in to change notification settings - Fork 26.2k
fix(router): cache route handle if found #22475
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
Conversation
We found a Contributor License Agreement for you (the sender of this pull request), but were unable to find agreements for the commit author(s). If you authored these, maybe you used a different email address in the git commits than was used to sign the CLA (login here to double check)? If these were authored by someone else, then they will need to sign a CLA as well, and confirm that they're okay with these being contributed to Google. |
CLAs look good, thanks! |
best pull request ever |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick review for now. Can you please provide a test? You should be able to run the router tests using:
./test.sh router
This will set up a watcher, and you can go ahead and add a test to create_router_state.spec.ts
.
Overall looks like a great change. Thanks!
Hello @jasonaden, what should the test do? Assert that
Thanks 😊 |
@jasonaden I'm really not sure what the test should do? My commit removes an unnecessary call to the |
@jasonaden I'm looking at the The spec file itself contains 5 helper functions that are used in almost every test. I don't know what they do but they look so complicated that it makes me wonder why they aren't under test. I'm gonna need some pointers if you want me to write a test for this change. Basically all my commit does is reorder some brackets so that the |
When asking the route reuse strategy to retrieve a detached route handle, store the return value in a local variable for further processing instead of asking again later. resolves #22474
@jasonaden I spent about 2 hours debugging and looking through the sources. I have a pretty good idea now of how
What I tried: it('should retrieve detached route handles', () => {
const config = [
{path: 'a', component: ComponentA},
{path: 'b', component: ComponentB, outlet: 'left'},
{path: 'c', component: ComponentC, outlet: 'left'}
];
const fakeDetachedHandle: DetachedRouteHandle = {};
spyOn(reuseStrategy, 'retrieve').and.callFake((route: ActivatedRouteSnapshot) => {
return fakeDetachedHandle;
});
let routerState = emptyState();
let snapshot1 = createState(config, 'a(left:b)');
let snapshot2 = createState(config, 'a(left:c)');
routerState = createRouterState(reuseStrategy, snapshot1, routerState);
advanceState(routerState);
routerState = createRouterState(reuseStrategy, snapshot2, routerState);
expect(reuseStrategy.retrieve).toHaveBeenCalledTimes(1);
}); This crashes because |
No problem. I created a test for this and I'll push it to your branch. |
{path: 'a', component: ComponentA}, {path: 'b', component: ComponentB, outlet: 'left'}, | ||
{path: 'c', component: ComponentC, outlet: 'left'} | ||
]; | ||
spyOn(reuseStrategy, 'retrieve').and.callThrough(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't that return null
or undefined
? Because then it skips the code path that I changed. (see line 36)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @StevenLiekens, I've been out for a little bit.
The real test here is line 110 below, where we verify that after the activated route has been created, the previously created version gets used the next time reuseStrategy.retrieve
gets called.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jasonaden but how? The DefaultRouteReuseStrategy doesn't ever retrieve anything.
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle|null { return null; }
https://github.com/angular/angular/blob/master/packages/router/src/route_reuse_strategy.ts#L63-L73
The code path that I changed only runs when retrieve
returns a truthy value so this test would pass without my fix.
When asking the route reuse strategy to retrieve a detached route handle, store the return value in a local variable for further processing instead of asking again later. resolves angular#22474 PR Close angular#22475
Can somebody tell me why this |
|
My experiments show that Wouldn't this sequence make more sense? // pseudocode
if (shouldDetach()) store();
else destroy();
if (shouldAttach()) retrieve();
else create(); It seems to work like this now, except that it additionaly calls |
Makes more sense to me, yes.
Well did they change it since I opened this issue? I haven't been following this very closely. /edit: no, they haven't changed this: if /edit2 apparently all this does is update the angular/packages/router/src/create_router_state.ts Lines 32 to 39 in f45aedc
The actual activation of the detached routes happens elsewhere. angular/packages/router/src/operators/activate_routes.ts Lines 159 to 191 in 9523991
|
Not sure why this is needed. I ignore those |
@thorn0 how do you check if shouldAttach is called first? trying to do exactly the same thing as you with ngOnAttach/Detach! |
@denu5 Please keep in mind that that code is just experiments. In no way is it production ready. E.g. I've just discovered that reused components sometimes don't get fresh resolved data ( |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
When asking the route reuse strategy to retrieve a detached route handle, store the
return value in a local variable for further processing instead of asking again later.
resolves #22474
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: #22474
What is the new behavior?
Does this PR introduce a breaking change?
Other information