-
-
Notifications
You must be signed in to change notification settings - Fork 5k
feat(history): Remove event listeners when all apps are destroyed. #3172
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
Changes from 3 commits
037a643
f923d37
6fdde38
db3e347
9d0ba02
97470df
1c90043
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,37 @@ import { setupScroll, handleScroll } from '../util/scroll' | |
import { pushState, replaceState, supportsPushState } from '../util/push-state' | ||
|
||
export class HTML5History extends History { | ||
+initLocation: string | ||
|
||
constructor (router: Router, base: ?string) { | ||
super(router, base) | ||
|
||
this.initLocation = getLocation(this.base) | ||
} | ||
|
||
setupListeners () { | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (this.listeners.length > 0) { | ||
return | ||
} | ||
|
||
const router = this.router | ||
const expectScroll = router.options.scrollBehavior | ||
const supportsScroll = supportsPushState && expectScroll | ||
|
||
if (supportsScroll) { | ||
setupScroll() | ||
this.listeners.push(setupScroll()) | ||
} | ||
|
||
const initLocation = getLocation(this.base) | ||
posva marked this conversation as resolved.
Show resolved
Hide resolved
|
||
window.addEventListener('popstate', e => { | ||
const handleRoutingEvent = () => { | ||
const expectScroll = router.options.scrollBehavior | ||
const supportsScroll = supportsPushState && expectScroll | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we remove these lines since they are right above? They shouldn't change |
||
|
||
const current = this.current | ||
|
||
// Avoiding first `popstate` event dispatched in some browsers but first | ||
// history route not updated since async guard at the same time. | ||
const location = getLocation(this.base) | ||
if (this.current === START && location === initLocation) { | ||
if (this.current === START && location === this.initLocation) { | ||
return | ||
} | ||
|
||
|
@@ -34,6 +47,10 @@ export class HTML5History extends History { | |
handleScroll(router, route, current, true) | ||
} | ||
}) | ||
} | ||
window.addEventListener('popstate', handleRoutingEvent) | ||
this.listeners.push(() => { | ||
window.removeEventListener('popstate', handleRoutingEvent) | ||
}) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,12 @@ export default class VueRouter { | |
// ensure we still have a main app or null if no apps | ||
// we do not release the router so it can be reused | ||
if (this.app === app) this.app = this.apps[0] || null | ||
|
||
if (this.apps.length === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should be able to just check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 done |
||
// clean up event listeners | ||
// https://github.com/vuejs/vue-router/issues/2341 | ||
this.history.teardownListeners() | ||
} | ||
}) | ||
|
||
// main app previously initialized | ||
|
@@ -110,17 +116,11 @@ export default class VueRouter { | |
|
||
const history = this.history | ||
|
||
if (history instanceof HTML5History) { | ||
history.transitionTo(history.getCurrentLocation()) | ||
} else if (history instanceof HashHistory) { | ||
const setupHashListener = () => { | ||
if (history instanceof HTML5History || history instanceof HashHistory) { | ||
const setupListeners = () => { | ||
history.setupListeners() | ||
} | ||
history.transitionTo( | ||
history.getCurrentLocation(), | ||
setupHashListener, | ||
setupHashListener | ||
) | ||
history.transitionTo(history.getCurrentLocation(), setupListeners, setupListeners) | ||
} | ||
|
||
history.listen(route => { | ||
|
Uh oh!
There was an error while loading. Please reload this page.