Explorar el Código

refactor patch createElm function, fix component hook merging

Evan You hace 9 años
padre
commit
de7764a385

+ 4 - 7
src/core/vdom/create-component.js

@@ -172,7 +172,6 @@ function init (
     // kept-alive components, treat as a patch
     const mountedNode: any = vnode // work around flow
     prepatch(mountedNode, mountedNode)
-    return true // let the patcher know this is a reactivated component
   }
 }
 
@@ -318,11 +317,9 @@ function mergeHooks (data: VNodeData) {
   }
 }
 
-function mergeHook (a: Function, b: Function): Function {
-  // since all hooks have at most two args, use fixed args
-  // to avoid having to use fn.apply().
-  return (_, __) => {
-    a(_, __)
-    b(_, __)
+function mergeHook (one: Function, two: Function): Function {
+  return function (a, b, c, d) {
+    one(a, b, c, d)
+    two(a, b, c, d)
   }
 }

+ 28 - 20
src/core/vdom/patch.js

@@ -86,27 +86,12 @@ export function createPatchFunction (backend) {
 
   let inPre = 0
   function createElm (vnode, insertedVnodeQueue, parentElm, refElm, nested) {
-    let i, isReactivated
-    const data = vnode.data
-    vnode.isRootInsert = !nested
-    if (isDef(data)) {
-      if (isDef(i = data.hook) && isDef(i = i.init)) {
-        isReactivated = i(vnode, false /* hydrating */, parentElm, refElm)
-      }
-      // after calling the init hook, if the vnode is a child component
-      // it should've created a child instance and mounted it. the child
-      // component also has set the placeholder vnode's elm.
-      // in that case we can just return the element and be done.
-      if (isDef(i = vnode.child)) {
-        initComponent(vnode, insertedVnodeQueue)
-        if (isReactivated) {
-          // unlike a newly created component,
-          // a reactivated keep-alive component doesn't insert itself
-          insert(parentElm, vnode.child.$el, refElm)
-        }
-        return
-      }
+    vnode.isRootInsert = !nested // for transition enter check
+    if (createComponent(vnode, insertedVnodeQueue, parentElm, refElm)) {
+      return
     }
+
+    const data = vnode.data
     const children = vnode.children
     const tag = vnode.tag
     if (isDef(tag)) {
@@ -172,6 +157,29 @@ export function createPatchFunction (backend) {
     }
   }
 
+  function createComponent (vnode, insertedVnodeQueue, parentElm, refElm) {
+    let i = vnode.data
+    if (isDef(i)) {
+      const isReactivated = isDef(vnode.child) && i.keepAlive
+      if (isDef(i = i.hook) && isDef(i = i.init)) {
+        i(vnode, false /* hydrating */, parentElm, refElm)
+      }
+      // after calling the init hook, if the vnode is a child component
+      // it should've created a child instance and mounted it. the child
+      // component also has set the placeholder vnode's elm.
+      // in that case we can just return the element and be done.
+      if (isDef(vnode.child)) {
+        initComponent(vnode, insertedVnodeQueue)
+        if (isReactivated) {
+          // unlike a newly created component,
+          // a reactivated keep-alive component doesn't insert itself
+          insert(parentElm, vnode.elm, refElm)
+        }
+        return true
+      }
+    }
+  }
+
   function insert (parent, elm, ref) {
     if (parent) {
       nodeOps.insertBefore(parent, elm, ref)

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

@@ -40,7 +40,7 @@ describe('create-component', () => {
     expect(vnode.context).toEqual(vm)
 
     vnode.data.hook.init(vnode)
-    expect(init).toHaveBeenCalledWith(vnode, undefined)
+    expect(init.calls.argsFor(0)[0]).toBe(vnode)
   })
 
   it('create a component when resolved with async loading', done => {