UNPKG

9.89 kBJavaScriptView Raw
1/* @flow */
2
3import config from '../config'
4import Watcher from '../observer/watcher'
5import { mark, measure } from '../util/perf'
6import { createEmptyVNode } from '../vdom/vnode'
7import { updateComponentListeners } from './events'
8import { resolveSlots } from './render-helpers/resolve-slots'
9import { toggleObserving } from '../observer/index'
10import { pushTarget, popTarget } from '../observer/dep'
11
12import {
13 warn,
14 noop,
15 remove,
16 emptyObject,
17 validateProp,
18 invokeWithErrorHandling
19} from '../util/index'
20
21export let activeInstance: any = null
22export let isUpdatingChildComponent: boolean = false
23
24export function setActiveInstance(vm: Component) {
25 const prevActiveInstance = activeInstance
26 activeInstance = vm
27 return () => {
28 activeInstance = prevActiveInstance
29 }
30}
31
32export function initLifecycle (vm: Component) {
33 const options = vm.$options
34
35 // locate first non-abstract parent
36 let parent = options.parent
37 if (parent && !options.abstract) {
38 while (parent.$options.abstract && parent.$parent) {
39 parent = parent.$parent
40 }
41 parent.$children.push(vm)
42 }
43
44 vm.$parent = parent
45 vm.$root = parent ? parent.$root : vm
46
47 vm.$children = []
48 vm.$refs = {}
49
50 vm._watcher = null
51 vm._inactive = null
52 vm._directInactive = false
53 vm._isMounted = false
54 vm._isDestroyed = false
55 vm._isBeingDestroyed = false
56}
57
58export function lifecycleMixin (Vue: Class<Component>) {
59 Vue.prototype._update = function (vnode: VNode, hydrating?: boolean) {
60 const vm: Component = this
61 const prevEl = vm.$el
62 const prevVnode = vm._vnode
63 const restoreActiveInstance = setActiveInstance(vm)
64 vm._vnode = vnode
65 // Vue.prototype.__patch__ is injected in entry points
66 // based on the rendering backend used.
67 if (!prevVnode) {
68 // initial render
69 vm.$el = vm.__patch__(vm.$el, vnode, hydrating, false /* removeOnly */)
70 } else {
71 // updates
72 vm.$el = vm.__patch__(prevVnode, vnode)
73 }
74 restoreActiveInstance()
75 // update __vue__ reference
76 if (prevEl) {
77 prevEl.__vue__ = null
78 }
79 if (vm.$el) {
80 vm.$el.__vue__ = vm
81 }
82 // if parent is an HOC, update its $el as well
83 if (vm.$vnode && vm.$parent && vm.$vnode === vm.$parent._vnode) {
84 vm.$parent.$el = vm.$el
85 }
86 // updated hook is called by the scheduler to ensure that children are
87 // updated in a parent's updated hook.
88 }
89
90 Vue.prototype.$forceUpdate = function () {
91 const vm: Component = this
92 if (vm._watcher) {
93 vm._watcher.update()
94 }
95 }
96
97 Vue.prototype.$destroy = function () {
98 const vm: Component = this
99 if (vm._isBeingDestroyed) {
100 return
101 }
102 callHook(vm, 'beforeDestroy')
103 vm._isBeingDestroyed = true
104 // remove self from parent
105 const parent = vm.$parent
106 if (parent && !parent._isBeingDestroyed && !vm.$options.abstract) {
107 remove(parent.$children, vm)
108 }
109 // teardown watchers
110 if (vm._watcher) {
111 vm._watcher.teardown()
112 }
113 let i = vm._watchers.length
114 while (i--) {
115 vm._watchers[i].teardown()
116 }
117 // remove reference from data ob
118 // frozen object may not have observer.
119 if (vm._data.__ob__) {
120 vm._data.__ob__.vmCount--
121 }
122 // call the last hook...
123 vm._isDestroyed = true
124 // invoke destroy hooks on current rendered tree
125 vm.__patch__(vm._vnode, null)
126 // fire destroyed hook
127 callHook(vm, 'destroyed')
128 // turn off all instance listeners.
129 vm.$off()
130 // remove __vue__ reference
131 if (vm.$el) {
132 vm.$el.__vue__ = null
133 }
134 // release circular reference (#6759)
135 if (vm.$vnode) {
136 vm.$vnode.parent = null
137 }
138 }
139}
140
141export function mountComponent (
142 vm: Component,
143 el: ?Element,
144 hydrating?: boolean
145): Component {
146 vm.$el = el
147 if (!vm.$options.render) {
148 vm.$options.render = createEmptyVNode
149 if (process.env.NODE_ENV !== 'production') {
150 /* istanbul ignore if */
151 if ((vm.$options.template && vm.$options.template.charAt(0) !== '#') ||
152 vm.$options.el || el) {
153 warn(
154 'You are using the runtime-only build of Vue where the template ' +
155 'compiler is not available. Either pre-compile the templates into ' +
156 'render functions, or use the compiler-included build.',
157 vm
158 )
159 } else {
160 warn(
161 'Failed to mount component: template or render function not defined.',
162 vm
163 )
164 }
165 }
166 }
167 callHook(vm, 'beforeMount')
168
169 let updateComponent
170 /* istanbul ignore if */
171 if (process.env.NODE_ENV !== 'production' && config.performance && mark) {
172 updateComponent = () => {
173 const name = vm._name
174 const id = vm._uid
175 const startTag = `vue-perf-start:${id}`
176 const endTag = `vue-perf-end:${id}`
177
178 mark(startTag)
179 const vnode = vm._render()
180 mark(endTag)
181 measure(`vue ${name} render`, startTag, endTag)
182
183 mark(startTag)
184 vm._update(vnode, hydrating)
185 mark(endTag)
186 measure(`vue ${name} patch`, startTag, endTag)
187 }
188 } else {
189 updateComponent = () => {
190 vm._update(vm._render(), hydrating)
191 }
192 }
193
194 // we set this to vm._watcher inside the watcher's constructor
195 // since the watcher's initial patch may call $forceUpdate (e.g. inside child
196 // component's mounted hook), which relies on vm._watcher being already defined
197 new Watcher(vm, updateComponent, noop, {
198 before () {
199 if (vm._isMounted && !vm._isDestroyed) {
200 callHook(vm, 'beforeUpdate')
201 }
202 }
203 }, true /* isRenderWatcher */)
204 hydrating = false
205
206 // manually mounted instance, call mounted on self
207 // mounted is called for render-created child components in its inserted hook
208 if (vm.$vnode == null) {
209 vm._isMounted = true
210 callHook(vm, 'mounted')
211 }
212 return vm
213}
214
215export function updateChildComponent (
216 vm: Component,
217 propsData: ?Object,
218 listeners: ?Object,
219 parentVnode: MountedComponentVNode,
220 renderChildren: ?Array<VNode>
221) {
222 if (process.env.NODE_ENV !== 'production') {
223 isUpdatingChildComponent = true
224 }
225
226 // determine whether component has slot children
227 // we need to do this before overwriting $options._renderChildren.
228
229 // check if there are dynamic scopedSlots (hand-written or compiled but with
230 // dynamic slot names). Static scoped slots compiled from template has the
231 // "$stable" marker.
232 const newScopedSlots = parentVnode.data.scopedSlots
233 const oldScopedSlots = vm.$scopedSlots
234 const hasDynamicScopedSlot = !!(
235 (newScopedSlots && !newScopedSlots.$stable) ||
236 (oldScopedSlots !== emptyObject && !oldScopedSlots.$stable) ||
237 (newScopedSlots && vm.$scopedSlots.$key !== newScopedSlots.$key)
238 )
239
240 // Any static slot children from the parent may have changed during parent's
241 // update. Dynamic scoped slots may also have changed. In such cases, a forced
242 // update is necessary to ensure correctness.
243 const needsForceUpdate = !!(
244 renderChildren || // has new static slots
245 vm.$options._renderChildren || // has old static slots
246 hasDynamicScopedSlot
247 )
248
249 vm.$options._parentVnode = parentVnode
250 vm.$vnode = parentVnode // update vm's placeholder node without re-render
251
252 if (vm._vnode) { // update child tree's parent
253 vm._vnode.parent = parentVnode
254 }
255 vm.$options._renderChildren = renderChildren
256
257 // update $attrs and $listeners hash
258 // these are also reactive so they may trigger child update if the child
259 // used them during render
260 vm.$attrs = parentVnode.data.attrs || emptyObject
261 vm.$listeners = listeners || emptyObject
262
263 // update props
264 if (propsData && vm.$options.props) {
265 toggleObserving(false)
266 const props = vm._props
267 const propKeys = vm.$options._propKeys || []
268 for (let i = 0; i < propKeys.length; i++) {
269 const key = propKeys[i]
270 const propOptions: any = vm.$options.props // wtf flow?
271 props[key] = validateProp(key, propOptions, propsData, vm)
272 }
273 toggleObserving(true)
274 // keep a copy of raw propsData
275 vm.$options.propsData = propsData
276 }
277
278 // update listeners
279 listeners = listeners || emptyObject
280 const oldListeners = vm.$options._parentListeners
281 vm.$options._parentListeners = listeners
282 updateComponentListeners(vm, listeners, oldListeners)
283
284 // resolve slots + force update if has children
285 if (needsForceUpdate) {
286 vm.$slots = resolveSlots(renderChildren, parentVnode.context)
287 vm.$forceUpdate()
288 }
289
290 if (process.env.NODE_ENV !== 'production') {
291 isUpdatingChildComponent = false
292 }
293}
294
295function isInInactiveTree (vm) {
296 while (vm && (vm = vm.$parent)) {
297 if (vm._inactive) return true
298 }
299 return false
300}
301
302export function activateChildComponent (vm: Component, direct?: boolean) {
303 if (direct) {
304 vm._directInactive = false
305 if (isInInactiveTree(vm)) {
306 return
307 }
308 } else if (vm._directInactive) {
309 return
310 }
311 if (vm._inactive || vm._inactive === null) {
312 vm._inactive = false
313 for (let i = 0; i < vm.$children.length; i++) {
314 activateChildComponent(vm.$children[i])
315 }
316 callHook(vm, 'activated')
317 }
318}
319
320export function deactivateChildComponent (vm: Component, direct?: boolean) {
321 if (direct) {
322 vm._directInactive = true
323 if (isInInactiveTree(vm)) {
324 return
325 }
326 }
327 if (!vm._inactive) {
328 vm._inactive = true
329 for (let i = 0; i < vm.$children.length; i++) {
330 deactivateChildComponent(vm.$children[i])
331 }
332 callHook(vm, 'deactivated')
333 }
334}
335
336export function callHook (vm: Component, hook: string) {
337 // #7573 disable dep collection when invoking lifecycle hooks
338 pushTarget()
339 const handlers = vm.$options[hook]
340 const info = `${hook} hook`
341 if (handlers) {
342 for (let i = 0, j = handlers.length; i < j; i++) {
343 invokeWithErrorHandling(handlers[i], vm, null, vm, info)
344 }
345 }
346 if (vm._hasHookEvent) {
347 vm.$emit('hook:' + hook)
348 }
349 popTarget()
350}