Browse Source

finish annotating vdom

Evan You 10 years ago
parent
commit
59fcd4e7a7
4 changed files with 37 additions and 11 deletions
  1. 12 0
      flow/vnode.js
  2. 5 3
      src/core/vdom/create-component.js
  3. 16 8
      src/core/vdom/modules/directives.js
  4. 4 0
      src/core/vdom/patch.js

+ 12 - 0
flow/vnode.js

@@ -13,6 +13,18 @@ declare interface MountedComponentVNode {
   child: Component;
 }
 
+// interface for vnodes in update modules
+declare interface VNodeWithData {
+  tag: string;
+  data: VNodeData;
+  children: Array<VNode> | void;
+  text: void;
+  elm: Node;
+  ns: string | void;
+  context: Component;
+  key: string | number | void;
+}
+
 declare interface VNodeData {
   pre?: true;
   key?: string | number;

+ 5 - 3
src/core/vdom/create-component.js

@@ -80,8 +80,10 @@ export function createComponent (
   return vnode
 }
 
-export function createComponentInstanceForVnode (vnode: VNode): Component {
-  const { Ctor, propsData, listeners, parent, children } = vnode.componentOptions || {}
+export function createComponentInstanceForVnode (
+  vnode: any // we know it's MountedComponentVNode but flow doesn't
+): Component {
+  const { Ctor, propsData, listeners, parent, children } = vnode.componentOptions
   const options: {
     parent: Component,
     propsData: ?Object,
@@ -98,7 +100,7 @@ export function createComponentInstanceForVnode (vnode: VNode): Component {
     _renderChildren: children
   }
   // check inline-template render functions
-  const inlineTemplate = vnode.data && vnode.data.inlineTemplate
+  const inlineTemplate = vnode.data.inlineTemplate
   if (inlineTemplate) {
     options.render = inlineTemplate.render
     options.staticRenderFns = inlineTemplate.staticRenderFns

+ 16 - 8
src/core/vdom/modules/directives.js

@@ -1,19 +1,27 @@
+/* @flow */
+
 import { resolveAsset } from 'core/util/options'
 
 export default {
-  create: function bindDirectives (oldVnode, vnode) {
+  create: function bindDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) {
     applyDirectives(oldVnode, vnode, 'bind')
   },
-  update: function updateDirectives (oldVnode, vnode) {
-    applyDirectives(oldVnode, vnode, 'update', true)
+  update: function updateDirectives (oldVnode: VNodeWithData, vnode: VNodeWithData) {
+    applyDirectives(oldVnode, vnode, 'update')
   },
-  destroy: function unbindDirectives (vnode) {
-    applyDirectives(null, vnode, 'unbind')
+  destroy: function unbindDirectives (vnode: VNodeWithData) {
+    applyDirectives(vnode, vnode, 'unbind')
   }
 }
 
-function applyDirectives (oldVnode, vnode, hook, update) {
+function applyDirectives (
+  oldVnode: VNodeWithData,
+  vnode: VNodeWithData,
+  hook: string
+) {
   const dirs = vnode.data.directives
+  const oldDirs = oldVnode.data.directives
+  const isUpdate = hook === 'update'
   if (dirs) {
     for (let i = 0; i < dirs.length; i++) {
       const dir = dirs[i]
@@ -21,8 +29,8 @@ function applyDirectives (oldVnode, vnode, hook, update) {
       const fn = def && def[hook]
       if (fn) {
         // only call update if value has changed
-        if (update) {
-          const oldValue = oldVnode.data.directives[i].value
+        if (isUpdate && oldDirs) {
+          const oldValue = oldDirs[i].value
           if (oldValue === dir.value) {
             continue
           }

+ 4 - 0
src/core/vdom/patch.js

@@ -2,7 +2,11 @@
  * Virtual DOM implementation based on Snabbdom by
  * Simon Friis Vindum (@paldepind)
  * with custom modifications.
+ *
+ * Not type-checking this because this file is perf-critical and the cost
+ * of making flow understand it is not worth it.
  */
+
 import VNode from './vnode'
 import { isPrimitive, renderString, warn } from '../util/index'