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

fix #6931: Clean up target variables to avoid memory leaks #6932

Merged
merged 1 commit into from
Oct 27, 2017
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
3 changes: 2 additions & 1 deletion src/core/instance/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function initEvents (vm: Component) {
}
}

let target: Component
let target: any
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love removing the type annotation here, but if I left it as Component then Flow would see the assignment to undefined and error due to "Possible undefined value".

In typescript you can tell the type checker to trust you by doing target!.method() but I couldn't find an equivilent in Flow. The Flow docs recommend if (target !== undefined), however I didn't think we'd want to add a branch in the VDom code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about just binding the add and remove methods to the vm so you don't need this closure variable at all?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriscasola You won't wanna do it that way. function.bind is just create a closure.


function add (event, fn, once) {
if (once) {
Expand All @@ -40,6 +40,7 @@ export function updateComponentListeners (
) {
target = vm
updateListeners(listeners, oldListeners || {}, add, remove, vm)
target = undefined
}

export function eventsMixin (Vue: Class<Component>) {
Expand Down
3 changes: 2 additions & 1 deletion src/platforms/web/runtime/modules/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function normalizeEvents (on) {
}
}

let target: HTMLElement
let target: any

function createOnceHandler (handler, event, capture) {
const _target = target // save current target element in closure
Expand Down Expand Up @@ -78,6 +78,7 @@ function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
target = vnode.elm
normalizeEvents(on)
updateListeners(on, oldOn, add, remove, vnode.context)
target = undefined
}

export default {
Expand Down
1 change: 1 addition & 0 deletions src/platforms/weex/runtime/modules/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
const oldOn = oldVnode.data.on || {}
target = vnode.elm
updateListeners(on, oldOn, add, remove, vnode.context)
target = undefined
}

export default {
Expand Down