mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-05 05:27:59 +08:00
improve DOM listener update performance
This commit is contained in:
parent
4120d1adc2
commit
25f8c50d95
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user