Skip to content

Commit 045ff5d

Browse files
committed
refactor create-component
1 parent 4964b25 commit 045ff5d

File tree

1 file changed

+58
-59
lines changed

1 file changed

+58
-59
lines changed

src/core/vdom/create-component.js

Lines changed: 58 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,63 @@ import {
2121
deactivateChildComponent
2222
} from '../instance/lifecycle'
2323

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)
2681

2782
export function createComponent (
2883
Ctor: Class<Component> | Function | Object | void,
@@ -170,62 +225,6 @@ export function createComponentInstanceForVnode (
170225
return new vnodeComponentOptions.Ctor(options)
171226
}
172227

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-
229228
function resolveAsyncComponent (
230229
factory: Function,
231230
baseCtor: Class<Component>,
@@ -327,7 +326,7 @@ function mergeHooks (data: VNodeData) {
327326
for (let i = 0; i < hooksToMerge.length; i++) {
328327
const key = hooksToMerge[i]
329328
const fromParent = data.hook[key]
330-
const ours = hooks[key]
329+
const ours = componentVNodeHooks[key]
331330
data.hook[key] = fromParent ? mergeHook(ours, fromParent) : ours
332331
}
333332
}

0 commit comments

Comments
 (0)