Skip to content

Commit 7c9e81e

Browse files
committed
optimize initEvents
1 parent 57f425e commit 7c9e81e

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed

flow/component.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ declare interface Component {
7070
_init: Function;
7171
_mount: (el?: Element | void, hydrating?: boolean) => Component;
7272
_update: (vnode: VNode, hydrating?: boolean) => void;
73-
_updateListeners: (listeners: Object, oldListeners: ?Object) => void;
7473
_updateFromParent: (
7574
propsData: ?Object,
7675
listeners: ?{ [key: string]: Function | Array<Function> },

src/core/instance/events.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
/* @flow */
22

3-
import { bind, toArray } from '../util/index'
3+
import { toArray } from '../util/index'
44
import { updateListeners } from '../vdom/helpers/index'
55

66
export function initEvents (vm: Component) {
77
vm._events = Object.create(null)
88
// init parent attached events
99
const listeners = vm.$options._parentListeners
10-
const add = (event, fn, once) => {
11-
once ? vm.$once(event, fn) : vm.$on(event, fn)
12-
}
13-
const remove = bind(vm.$off, vm)
14-
vm._updateListeners = (listeners, oldListeners) => {
15-
updateListeners(listeners, oldListeners || {}, add, remove, vm)
16-
}
1710
if (listeners) {
18-
vm._updateListeners(listeners)
11+
updateComponentListeners(vm, listeners)
12+
}
13+
}
14+
15+
let target: Component
16+
17+
function add (event, fn, once) {
18+
if (once) {
19+
target.$once(event, fn)
20+
} else {
21+
target.$on(event, fn)
1922
}
2023
}
2124

25+
function remove (event, fn) {
26+
target.$off(event, fn)
27+
}
28+
29+
export function updateComponentListeners (
30+
vm: Component,
31+
listeners: Object,
32+
oldListeners: ?Object
33+
) {
34+
target = vm
35+
updateListeners(listeners, oldListeners || {}, add, remove, vm)
36+
}
37+
2238
export function eventsMixin (Vue: Class<Component>) {
2339
Vue.prototype.$on = function (event: string, fn: Function): Component {
2440
const vm: Component = this

src/core/instance/lifecycle.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createEmptyVNode } from '../vdom/vnode'
55
import { observerState } from '../observer/index'
66
import { warn, validateProp, remove, noop } from '../util/index'
77
import { resolveSlots } from './render'
8+
import { updateComponentListeners } from './events'
89

910
export let activeInstance: any = null
1011

@@ -148,7 +149,7 @@ export function lifecycleMixin (Vue: Class<Component>) {
148149
if (listeners) {
149150
const oldListeners = vm.$options._parentListeners
150151
vm.$options._parentListeners = listeners
151-
vm._updateListeners(listeners, oldListeners)
152+
updateComponentListeners(vm, listeners, oldListeners)
152153
}
153154
// resolve slots + force update if has children
154155
if (hasChildren) {

0 commit comments

Comments
 (0)