Skip to content

Commit

Permalink
improve DOM listener update performance
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 5, 2016
1 parent 4120d1a commit 25f8c50
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/platforms/web/runtime/modules/events.js
Original file line number Diff line number Diff line change
@@ -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)
}

Expand Down

0 comments on commit 25f8c50

Please sign in to comment.