Skip to content

Commit 25f8c50

Browse files
committed
improve DOM listener update performance
1 parent 4120d1a commit 25f8c50

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/platforms/web/runtime/modules/events.js

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
// skip type checking this file because we need to attach private properties
2-
// to elements
1+
/* @flow */
32

43
import { updateListeners } from 'core/vdom/helpers/index'
54

6-
function updateDOMListeners (oldVnode, vnode) {
5+
let target: HTMLElement
6+
7+
function add (event: string, handler: Function, once: boolean, capture: boolean) {
8+
if (once) {
9+
const oldHandler = handler
10+
handler = function (ev) {
11+
remove(event, handler, capture)
12+
arguments.length === 1
13+
? oldHandler(ev)
14+
: oldHandler.apply(null, arguments)
15+
}
16+
}
17+
target.addEventListener(event, handler, capture)
18+
}
19+
20+
function remove (event: string, handler: Function, capture: boolean) {
21+
target.removeEventListener(event, handler, capture)
22+
}
23+
24+
function updateDOMListeners (oldVnode: VNodeWithData, vnode: VNodeWithData) {
725
if (!oldVnode.data.on && !vnode.data.on) {
826
return
927
}
1028
const on = vnode.data.on || {}
1129
const oldOn = oldVnode.data.on || {}
12-
const add = vnode.elm._v_add || (
13-
vnode.elm._v_add = (event, handler, once, capture) => {
14-
if (once) {
15-
const oldHandler = handler
16-
handler = function (ev) {
17-
remove(event, handler, capture)
18-
arguments.length === 1
19-
? oldHandler(ev)
20-
: oldHandler.apply(null, arguments)
21-
}
22-
}
23-
vnode.elm.addEventListener(event, handler, capture)
24-
}
25-
)
26-
const remove = vnode.elm._v_remove || (
27-
vnode.elm._v_remove = (event, handler, capture) => {
28-
vnode.elm.removeEventListener(event, handler, capture)
29-
}
30-
)
30+
target = vnode.elm
3131
updateListeners(on, oldOn, add, remove, vnode.context)
3232
}
3333

0 commit comments

Comments
 (0)