@@ -21,8 +21,63 @@ import {
21
21
deactivateChildComponent
22
22
} from '../instance/lifecycle'
23
23
24
- const hooks = { init, prepatch, insert, destroy }
25
- const hooksToMerge = Object . keys ( hooks )
24
+ // hooks to be invoked on component VNodes during patch
25
+ const componentVNodeHooks = {
26
+ init (
27
+ vnode : VNodeWithData ,
28
+ hydrating : boolean ,
29
+ parentElm : ?Node ,
30
+ refElm : ?Node
31
+ ) : ?boolean {
32
+ if ( ! vnode . componentInstance || vnode . componentInstance . _isDestroyed ) {
33
+ const child = vnode . componentInstance = createComponentInstanceForVnode (
34
+ vnode ,
35
+ activeInstance ,
36
+ parentElm ,
37
+ refElm
38
+ )
39
+ child . $mount ( hydrating ? vnode . elm : undefined , hydrating )
40
+ } else if ( vnode . data . keepAlive ) {
41
+ // kept-alive components, treat as a patch
42
+ const mountedNode : any = vnode // work around flow
43
+ componentVNodeHooks . prepatch ( mountedNode , mountedNode )
44
+ }
45
+ } ,
46
+
47
+ prepatch ( oldVnode : MountedComponentVNode , vnode : MountedComponentVNode ) {
48
+ const options = vnode . componentOptions
49
+ const child = vnode . componentInstance = oldVnode . componentInstance
50
+ updateChildComponent (
51
+ child ,
52
+ options . propsData , // updated props
53
+ options . listeners , // updated listeners
54
+ vnode , // new parent vnode
55
+ options . children // new children
56
+ )
57
+ } ,
58
+
59
+ insert ( vnode : MountedComponentVNode ) {
60
+ if ( ! vnode . componentInstance . _isMounted ) {
61
+ vnode . componentInstance . _isMounted = true
62
+ callHook ( vnode . componentInstance , 'mounted' )
63
+ }
64
+ if ( vnode . data . keepAlive ) {
65
+ activateChildComponent ( vnode . componentInstance , true /* direct */ )
66
+ }
67
+ } ,
68
+
69
+ destroy ( vnode : MountedComponentVNode ) {
70
+ if ( ! vnode . componentInstance . _isDestroyed ) {
71
+ if ( ! vnode . data . keepAlive ) {
72
+ vnode . componentInstance . $destroy ( )
73
+ } else {
74
+ deactivateChildComponent ( vnode . componentInstance , true /* direct */ )
75
+ }
76
+ }
77
+ }
78
+ }
79
+
80
+ const hooksToMerge = Object . keys ( componentVNodeHooks )
26
81
27
82
export function createComponent (
28
83
Ctor : Class < Component > | Function | Object | void ,
@@ -170,62 +225,6 @@ export function createComponentInstanceForVnode (
170
225
return new vnodeComponentOptions . Ctor ( options )
171
226
}
172
227
173
- function init (
174
- vnode : VNodeWithData ,
175
- hydrating : boolean ,
176
- parentElm : ?Node ,
177
- refElm : ?Node
178
- ) : ?boolean {
179
- if ( ! vnode . componentInstance || vnode . componentInstance . _isDestroyed ) {
180
- const child = vnode . componentInstance = createComponentInstanceForVnode (
181
- vnode ,
182
- activeInstance ,
183
- parentElm ,
184
- refElm
185
- )
186
- child . $mount ( hydrating ? vnode . elm : undefined , hydrating )
187
- } else if ( vnode . data . keepAlive ) {
188
- // kept-alive components, treat as a patch
189
- const mountedNode : any = vnode // work around flow
190
- prepatch ( mountedNode , mountedNode )
191
- }
192
- }
193
-
194
- function prepatch (
195
- oldVnode : MountedComponentVNode ,
196
- vnode : MountedComponentVNode
197
- ) {
198
- const options = vnode . componentOptions
199
- const child = vnode . componentInstance = oldVnode . componentInstance
200
- updateChildComponent (
201
- child ,
202
- options . propsData , // updated props
203
- options . listeners , // updated listeners
204
- vnode , // new parent vnode
205
- options . children // new children
206
- )
207
- }
208
-
209
- function insert ( vnode : MountedComponentVNode ) {
210
- if ( ! vnode . componentInstance . _isMounted ) {
211
- vnode . componentInstance . _isMounted = true
212
- callHook ( vnode . componentInstance , 'mounted' )
213
- }
214
- if ( vnode . data . keepAlive ) {
215
- activateChildComponent ( vnode . componentInstance , true /* direct */ )
216
- }
217
- }
218
-
219
- function destroy ( vnode : MountedComponentVNode ) {
220
- if ( ! vnode . componentInstance . _isDestroyed ) {
221
- if ( ! vnode . data . keepAlive ) {
222
- vnode . componentInstance . $destroy ( )
223
- } else {
224
- deactivateChildComponent ( vnode . componentInstance , true /* direct */ )
225
- }
226
- }
227
- }
228
-
229
228
function resolveAsyncComponent (
230
229
factory : Function ,
231
230
baseCtor : Class < Component > ,
@@ -327,7 +326,7 @@ function mergeHooks (data: VNodeData) {
327
326
for ( let i = 0 ; i < hooksToMerge . length ; i ++ ) {
328
327
const key = hooksToMerge [ i ]
329
328
const fromParent = data . hook [ key ]
330
- const ours = hooks [ key ]
329
+ const ours = componentVNodeHooks [ key ]
331
330
data . hook [ key ] = fromParent ? mergeHook ( ours , fromParent ) : ours
332
331
}
333
332
}
0 commit comments