Skip to content

Commit de7764a

Browse files
committed
refactor patch createElm function, fix component hook merging
1 parent dde0454 commit de7764a

File tree

3 files changed

+33
-28
lines changed

3 files changed

+33
-28
lines changed

src/core/vdom/create-component.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ function init (
172172
// kept-alive components, treat as a patch
173173
const mountedNode: any = vnode // work around flow
174174
prepatch(mountedNode, mountedNode)
175-
return true // let the patcher know this is a reactivated component
176175
}
177176
}
178177

@@ -318,11 +317,9 @@ function mergeHooks (data: VNodeData) {
318317
}
319318
}
320319

321-
function mergeHook (a: Function, b: Function): Function {
322-
// since all hooks have at most two args, use fixed args
323-
// to avoid having to use fn.apply().
324-
return (_, __) => {
325-
a(_, __)
326-
b(_, __)
320+
function mergeHook (one: Function, two: Function): Function {
321+
return function (a, b, c, d) {
322+
one(a, b, c, d)
323+
two(a, b, c, d)
327324
}
328325
}

src/core/vdom/patch.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,27 +86,12 @@ export function createPatchFunction (backend) {
8686

8787
let inPre = 0
8888
function createElm (vnode, insertedVnodeQueue, parentElm, refElm, nested) {
89-
let i, isReactivated
90-
const data = vnode.data
91-
vnode.isRootInsert = !nested
92-
if (isDef(data)) {
93-
if (isDef(i = data.hook) && isDef(i = i.init)) {
94-
isReactivated = i(vnode, false /* hydrating */, parentElm, refElm)
95-
}
96-
// after calling the init hook, if the vnode is a child component
97-
// it should've created a child instance and mounted it. the child
98-
// component also has set the placeholder vnode's elm.
99-
// in that case we can just return the element and be done.
100-
if (isDef(i = vnode.child)) {
101-
initComponent(vnode, insertedVnodeQueue)
102-
if (isReactivated) {
103-
// unlike a newly created component,
104-
// a reactivated keep-alive component doesn't insert itself
105-
insert(parentElm, vnode.child.$el, refElm)
106-
}
107-
return
108-
}
89+
vnode.isRootInsert = !nested // for transition enter check
90+
if (createComponent(vnode, insertedVnodeQueue, parentElm, refElm)) {
91+
return
10992
}
93+
94+
const data = vnode.data
11095
const children = vnode.children
11196
const tag = vnode.tag
11297
if (isDef(tag)) {
@@ -172,6 +157,29 @@ export function createPatchFunction (backend) {
172157
}
173158
}
174159

160+
function createComponent (vnode, insertedVnodeQueue, parentElm, refElm) {
161+
let i = vnode.data
162+
if (isDef(i)) {
163+
const isReactivated = isDef(vnode.child) && i.keepAlive
164+
if (isDef(i = i.hook) && isDef(i = i.init)) {
165+
i(vnode, false /* hydrating */, parentElm, refElm)
166+
}
167+
// after calling the init hook, if the vnode is a child component
168+
// it should've created a child instance and mounted it. the child
169+
// component also has set the placeholder vnode's elm.
170+
// in that case we can just return the element and be done.
171+
if (isDef(vnode.child)) {
172+
initComponent(vnode, insertedVnodeQueue)
173+
if (isReactivated) {
174+
// unlike a newly created component,
175+
// a reactivated keep-alive component doesn't insert itself
176+
insert(parentElm, vnode.elm, refElm)
177+
}
178+
return true
179+
}
180+
}
181+
}
182+
175183
function insert (parent, elm, ref) {
176184
if (parent) {
177185
nodeOps.insertBefore(parent, elm, ref)

test/unit/modules/vdom/create-component.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('create-component', () => {
4040
expect(vnode.context).toEqual(vm)
4141

4242
vnode.data.hook.init(vnode)
43-
expect(init).toHaveBeenCalledWith(vnode, undefined)
43+
expect(init.calls.argsFor(0)[0]).toBe(vnode)
4444
})
4545

4646
it('create a component when resolved with async loading', done => {

0 commit comments

Comments
 (0)