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

Do lifecycle callbacks even on the same route #456

Merged
merged 3 commits into from
Apr 29, 2020
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
10 changes: 10 additions & 0 deletions demo/vaadin-router-lifecycle-callbacks-demos.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ <h3><code>onBeforeEnter(location, commands, router)</code></h3>
See the <a target="_parent" href="../#/classes/WebComponentInterface#method-onBeforeEnter">
API documentation</a> for more details.
</p>
<p>
<strong>Note: </strong>Navigating to the same route also triggers this callback,
e.g., click on the same link multiple times will trigger the <code>onBeforeEnter</code>
callback on each click.
</p>
<vaadin-demo-snippet id="vaadin-router-lifecycle-events-demos-1" iframe-src="iframe.html">
<template preserve-content>
<a href="/">Home</a>
Expand Down Expand Up @@ -214,6 +219,11 @@ <h3><code>onBeforeLeave(location, commands, router)</code></h3>
See the <a target="_parent" href="../#/classes/WebComponentInterface#method-onBeforeLeave">
API documentation</a> for more details.
</p>
<p>
<strong>Note: </strong>Navigating to the same route also triggers this callback,
e.g., click on the same link multiple times will trigger the <code>onBeforeLeave</code>
callback on each click.
</p>
<vaadin-demo-snippet id="vaadin-router-lifecycle-events-demos-3" iframe-src="iframe.html">
<template preserve-content>
<dom-module id="x-user-manage">
Expand Down
8 changes: 2 additions & 6 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -650,15 +650,11 @@ export class Router extends Resolver {
if (newContext.__skipAttach) {
// execute onBeforeLeave for changed segment element when skipping attach
for (let i = newChain.length - 1; i >= 0; i--) {
if (previousChain[i].path !== newChain[i].path || newContext.search !== previousContext.search) {
callbacks = this.__runOnBeforeLeaveCallbacks(callbacks, newContext, {prevent}, previousChain[i]);
}
callbacks = this.__runOnBeforeLeaveCallbacks(callbacks, newContext, {prevent}, previousChain[i]);
}
// execute onBeforeEnter for changed segment element when skipping attach
for (let i = 0; i < newChain.length; i++) {
if (previousChain[i].path !== newChain[i].path || newContext.search !== previousContext.search) {
callbacks = this.__runOnBeforeEnterCallbacks(callbacks, newContext, {prevent, redirect}, newChain[i]);
}
callbacks = this.__runOnBeforeEnterCallbacks(callbacks, newContext, {prevent, redirect}, newChain[i]);
previousChain[i].element.location = createLocation(newContext, previousChain[i].route);
}

Expand Down
28 changes: 22 additions & 6 deletions test/router/lifecycle-events.spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@
verifyCallbacks([
'x-a.action',
'x-b.action',
'x-b.onBeforeLeave',
'x-a.onBeforeLeave',
'x-a.onBeforeEnter',
'x-b.onBeforeEnter'
]);
checkOutlet(['x-a', 'x-b']);
});
Expand Down Expand Up @@ -1307,8 +1311,10 @@
'x-user.action',
'x-edit.action',
// only call changed segment events
'x-edit.onBeforeLeave',
'x-user.onBeforeLeave',
'x-user.onBeforeEnter',
'x-edit.onBeforeEnter'
]);
});

Expand Down Expand Up @@ -1355,6 +1361,8 @@
'x-layout.action',
'x-foo.action',
'x-foo.onBeforeLeave',
'x-layout.onBeforeLeave',
'x-layout.onBeforeEnter',
'x-foo.onBeforeEnter'
// stop calling any other callbacks if result is the same
// stop detaching/re-attaching the element
Expand Down Expand Up @@ -1508,7 +1516,7 @@
]);
});

it('do not remove content when reusing element for same path (#362, #311, #331)', async() => {
it('Make lifecycle callbacks when reusing element for same path (#362, #311, #331)', async() => {
const view = document.createElement('x-spy');
view.textContent = 'view';
view.name = 'view';
Expand Down Expand Up @@ -1542,8 +1550,8 @@
'view.onAfterEnter',
// Action is always called
'view.action.1',
// No call events since nothing changed
// No detach/re-attach (#311 #331)
'view.onBeforeLeave',
'view.onBeforeEnter'
]);
});

Expand All @@ -1567,6 +1575,8 @@

// Skip detach/re-attach and notifications (#311 #331)
verifyCallbacks([
'x-spy.onBeforeLeave',
'x-spy.onBeforeEnter'
]);
});

Expand Down Expand Up @@ -1656,7 +1666,8 @@
expect(outlet.innerHTML.toLowerCase()).to.be.equal('<span><a>layout-link</a><h1>client</h1></span>');
});

it('should skip lifecycle when path remains same and search string remains empty', async() => {
// https://github.com/vaadin/flow/issues/8081
it('should keep lifecycle even when path remains same and search string remains empty', async() => {
router.setRoutes([
{path: '/a', component: 'x-spy'}
], true);
Expand All @@ -1671,16 +1682,21 @@
await router.render({pathname: '/a'});
expect(outlet.children[0]).to.be.equal(view);

// Search remains empty, skip lifecycle
verifyCallbacks([
'x-spy.onBeforeLeave',
'x-spy.onBeforeEnter',
]);

// Explicit empty search string
await router.render({pathname: '/a', search: ''});
expect(outlet.children[0]).to.be.equal(view);

// Search remains empty, skip lifecycle
// Search remains empty, stil lifecycle
verifyCallbacks([
'x-spy.onBeforeLeave',
'x-spy.onBeforeEnter',
'x-spy.onBeforeLeave',
'x-spy.onBeforeEnter'
]);
});

Expand Down