improve DOM listener update performance

This commit is contained in:
Evan You 2016-12-02 21:50:05 -05:00
parent 4120d1adc2
commit 25f8c50d95

View File

@ -1,33 +1,33 @@
// skip type checking this file because we need to attach private properties
// to elements
/* @flow */
import { updateListeners } from 'core/vdom/helpers/index'
function updateDOMListeners (oldVnode, vnode) {
let target: HTMLElement
function add (event: string, handler: Function, once: boolean, capture: boolean) {
if (once) {
const oldHandler = handler
handler = function (ev) {
remove(event, handler, capture)
arguments.length === 1
? oldHandler(ev)
: oldHandler.apply(null, arguments)
}
}
target.addEventListener(event, handler, capture)
}
function remove (event: string, handler: Function, capture: boolean) {
target.removeEventListener(event, handler, capture)
}
function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
if (!oldVnode.data.on && !vnode.data.on) {
return
}
const on = vnode.data.on || {}
const oldOn = oldVnode.data.on || {}
const add = vnode.elm._v_add || (
vnode.elm._v_add = (event, handler, once, capture) => {
if (once) {
const oldHandler = handler
handler = function (ev) {
remove(event, handler, capture)
arguments.length === 1
? oldHandler(ev)
: oldHandler.apply(null, arguments)
}
}
vnode.elm.addEventListener(event, handler, capture)
}
)
const remove = vnode.elm._v_remove || (
vnode.elm._v_remove = (event, handler, capture) => {
vnode.elm.removeEventListener(event, handler, capture)
}
)
target = vnode.elm
updateListeners(on, oldOn, add, remove, vnode.context)
}