Skip to content

Commit dae1f3d

Browse files
committed
optimize instance initialization
1 parent 7c9e81e commit dae1f3d

File tree

4 files changed

+71
-78
lines changed

4 files changed

+71
-78
lines changed

src/core/instance/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export function eventsMixin (Vue: Class<Component>) {
8787
let cbs = vm._events[event]
8888
if (cbs) {
8989
cbs = cbs.length > 1 ? toArray(cbs) : cbs
90-
const args = toArray(arguments, 1)
90+
const args = arguments.length > 1 ? toArray(arguments, 1) : void 0
9191
for (let i = 0, l = cbs.length; i < l; i++) {
9292
cbs[i].apply(vm, args)
9393
}

src/core/instance/lifecycle.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,5 +211,7 @@ export function callHook (vm: Component, hook: string) {
211211
handlers[i].call(vm)
212212
}
213213
}
214-
vm.$emit('hook:' + hook)
214+
// if (vm._events['hook:' + hook]) {
215+
// vm.$emit('hook:' + hook)
216+
// }
215217
}

src/core/instance/state.js

Lines changed: 64 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,48 @@ import {
2323

2424
export function initState (vm: Component) {
2525
vm._watchers = []
26-
initProps(vm)
27-
initMethods(vm)
26+
const opts = vm.$options
27+
if (opts.props) initProps(vm, opts.props)
28+
if (opts.methods) initMethods(vm, opts.methods)
2829
initData(vm)
29-
initComputed(vm)
30-
initWatch(vm)
30+
if (opts.computed) initComputed(vm, opts.computed)
31+
if (opts.watch) initWatch(vm, opts.watch)
3132
}
3233

3334
const isReservedProp = { key: 1, ref: 1, slot: 1 }
3435

35-
function initProps (vm: Component) {
36-
const props = vm.$options.props
37-
if (props) {
38-
const propsData = vm.$options.propsData || {}
39-
const keys = vm.$options._propKeys = Object.keys(props)
40-
const isRoot = !vm.$parent
41-
// root instance props should be converted
42-
observerState.shouldConvert = isRoot
43-
for (let i = 0; i < keys.length; i++) {
44-
const key = keys[i]
45-
/* istanbul ignore else */
46-
if (process.env.NODE_ENV !== 'production') {
47-
if (isReservedProp[key]) {
36+
function initProps (vm: Component, props: Object) {
37+
const propsData = vm.$options.propsData || {}
38+
const keys = vm.$options._propKeys = Object.keys(props)
39+
const isRoot = !vm.$parent
40+
// root instance props should be converted
41+
observerState.shouldConvert = isRoot
42+
for (let i = 0; i < keys.length; i++) {
43+
const key = keys[i]
44+
/* istanbul ignore else */
45+
if (process.env.NODE_ENV !== 'production') {
46+
if (isReservedProp[key]) {
47+
warn(
48+
`"${key}" is a reserved attribute and cannot be used as component prop.`,
49+
vm
50+
)
51+
}
52+
defineReactive(vm, key, validateProp(key, props, propsData, vm), () => {
53+
if (vm.$parent && !observerState.isSettingProps) {
4854
warn(
49-
`"${key}" is a reserved attribute and cannot be used as component prop.`,
55+
`Avoid mutating a prop directly since the value will be ` +
56+
`overwritten whenever the parent component re-renders. ` +
57+
`Instead, use a data or computed property based on the prop's ` +
58+
`value. Prop being mutated: "${key}"`,
5059
vm
5160
)
5261
}
53-
defineReactive(vm, key, validateProp(key, props, propsData, vm), () => {
54-
if (vm.$parent && !observerState.isSettingProps) {
55-
warn(
56-
`Avoid mutating a prop directly since the value will be ` +
57-
`overwritten whenever the parent component re-renders. ` +
58-
`Instead, use a data or computed property based on the prop's ` +
59-
`value. Prop being mutated: "${key}"`,
60-
vm
61-
)
62-
}
63-
})
64-
} else {
65-
defineReactive(vm, key, validateProp(key, props, propsData, vm))
66-
}
62+
})
63+
} else {
64+
defineReactive(vm, key, validateProp(key, props, propsData, vm))
6765
}
68-
observerState.shouldConvert = true
6966
}
67+
observerState.shouldConvert = true
7068
}
7169

7270
function initData (vm: Component) {
@@ -109,26 +107,23 @@ const computedSharedDefinition = {
109107
set: noop
110108
}
111109

112-
function initComputed (vm: Component) {
113-
const computed = vm.$options.computed
114-
if (computed) {
115-
for (const key in computed) {
116-
const userDef = computed[key]
117-
if (typeof userDef === 'function') {
118-
computedSharedDefinition.get = makeComputedGetter(userDef, vm)
119-
computedSharedDefinition.set = noop
120-
} else {
121-
computedSharedDefinition.get = userDef.get
122-
? userDef.cache !== false
123-
? makeComputedGetter(userDef.get, vm)
124-
: bind(userDef.get, vm)
125-
: noop
126-
computedSharedDefinition.set = userDef.set
127-
? bind(userDef.set, vm)
128-
: noop
129-
}
130-
Object.defineProperty(vm, key, computedSharedDefinition)
110+
function initComputed (vm: Component, computed: Object) {
111+
for (const key in computed) {
112+
const userDef = computed[key]
113+
if (typeof userDef === 'function') {
114+
computedSharedDefinition.get = makeComputedGetter(userDef, vm)
115+
computedSharedDefinition.set = noop
116+
} else {
117+
computedSharedDefinition.get = userDef.get
118+
? userDef.cache !== false
119+
? makeComputedGetter(userDef.get, vm)
120+
: bind(userDef.get, vm)
121+
: noop
122+
computedSharedDefinition.set = userDef.set
123+
? bind(userDef.set, vm)
124+
: noop
131125
}
126+
Object.defineProperty(vm, key, computedSharedDefinition)
132127
}
133128
}
134129

@@ -147,34 +142,28 @@ function makeComputedGetter (getter: Function, owner: Component): Function {
147142
}
148143
}
149144

150-
function initMethods (vm: Component) {
151-
const methods = vm.$options.methods
152-
if (methods) {
153-
for (const key in methods) {
154-
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
155-
if (process.env.NODE_ENV !== 'production' && methods[key] == null) {
156-
warn(
157-
`method "${key}" has an undefined value in the component definition. ` +
158-
`Did you reference the function correctly?`,
159-
vm
160-
)
161-
}
145+
function initMethods (vm: Component, methods: Object) {
146+
for (const key in methods) {
147+
vm[key] = methods[key] == null ? noop : bind(methods[key], vm)
148+
if (process.env.NODE_ENV !== 'production' && methods[key] == null) {
149+
warn(
150+
`method "${key}" has an undefined value in the component definition. ` +
151+
`Did you reference the function correctly?`,
152+
vm
153+
)
162154
}
163155
}
164156
}
165157

166-
function initWatch (vm: Component) {
167-
const watch = vm.$options.watch
168-
if (watch) {
169-
for (const key in watch) {
170-
const handler = watch[key]
171-
if (Array.isArray(handler)) {
172-
for (let i = 0; i < handler.length; i++) {
173-
createWatcher(vm, key, handler[i])
174-
}
175-
} else {
176-
createWatcher(vm, key, handler)
158+
function initWatch (vm: Component, watch: Object) {
159+
for (const key in watch) {
160+
const handler = watch[key]
161+
if (Array.isArray(handler)) {
162+
for (let i = 0; i < handler.length; i++) {
163+
createWatcher(vm, key, handler[i])
177164
}
165+
} else {
166+
createWatcher(vm, key, handler)
178167
}
179168
}
180169
}

src/core/observer/watcher.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ export default class Watcher {
4949
this.user = !!options.user
5050
this.lazy = !!options.lazy
5151
this.sync = !!options.sync
52-
this.expression = expOrFn.toString()
5352
this.cb = cb
5453
this.id = ++uid // uid for batching
5554
this.active = true
@@ -58,6 +57,9 @@ export default class Watcher {
5857
this.newDeps = []
5958
this.depIds = new Set()
6059
this.newDepIds = new Set()
60+
this.expression = process.env.NODE_ENV !== 'production'
61+
? expOrFn.toString()
62+
: ''
6163
// parse expression for getter
6264
if (typeof expOrFn === 'function') {
6365
this.getter = expOrFn

0 commit comments

Comments
 (0)