refactor: use util methods

This commit is contained in:
Evan You 2019-05-28 10:28:25 +08:00
parent a4d116e3f7
commit 28a0c50357
5 changed files with 25 additions and 15 deletions

View File

@ -0,0 +1 @@
export class Component {}

View File

@ -16,8 +16,9 @@ import {
createVNode, createVNode,
VNode, VNode,
VNodeChildren VNodeChildren
} from './h.js' } from './vnode.js'
import { TEXT, CLASS, STYLE, PROPS, KEYED, UNKEYED } from './patchFlags' import { TEXT, CLASS, STYLE, PROPS, KEYED, UNKEYED } from './patchFlags'
import { isString, isFunction, isArray } from '@vue/shared'
const emptyArr: any[] = [] const emptyArr: any[] = []
const emptyObj: { [key: string]: any } = {} const emptyObj: { [key: string]: any } = {}
@ -82,8 +83,8 @@ export function createRenderer(options: RendererOptions) {
processEmptyNode(n1, n2, container, anchor) processEmptyNode(n1, n2, container, anchor)
} else if (type === Fragment) { } else if (type === Fragment) {
processFragment(n1, n2, container, anchor, optimized) processFragment(n1, n2, container, anchor, optimized)
} else if (typeof type === 'function') { } else if (isFunction(type)) {
// TODO Component processComponent(n1, n2, container, anchor)
} else { } else {
processElement(n1, n2, container, anchor, optimized) processElement(n1, n2, container, anchor, optimized)
} }
@ -144,7 +145,7 @@ export function createRenderer(options: RendererOptions) {
hostPatchProp(el, key, vnode.props[key], null, false) hostPatchProp(el, key, vnode.props[key], null, false)
} }
} }
if (typeof vnode.children === 'string') { if (isString(vnode.children)) {
hostSetElementText(el, vnode.children) hostSetElementText(el, vnode.children)
} else if (vnode.children != null) { } else if (vnode.children != null) {
mountChildren(vnode.children, el) mountChildren(vnode.children, el)
@ -168,7 +169,7 @@ export function createRenderer(options: RendererOptions) {
if (child == null) { if (child == null) {
// empty placeholder // empty placeholder
return createVNode(Empty) return createVNode(Empty)
} else if (Array.isArray(child)) { } else if (isArray(child)) {
// fragment // fragment
return createVNode(Fragment, null, child) return createVNode(Fragment, null, child)
} else if (typeof child === 'object') { } else if (typeof child === 'object') {
@ -322,6 +323,13 @@ export function createRenderer(options: RendererOptions) {
} }
} }
function processComponent(
n1: VNode | null,
n2: VNode,
container: HostNode,
anchor?: HostNode
) {}
function patchChildren( function patchChildren(
n1: VNode | null, n1: VNode | null,
n2: VNode, n2: VNode,
@ -359,20 +367,20 @@ export function createRenderer(options: RendererOptions) {
} }
} }
if (typeof c2 === 'string') { if (isString(c2)) {
// text children fast path // text children fast path
if (Array.isArray(c1)) { if (isArray(c1)) {
unmountChildren(c1 as VNode[]) unmountChildren(c1 as VNode[])
} }
hostSetElementText(container, c2) hostSetElementText(container, c2)
} else { } else {
if (typeof c1 === 'string') { if (isString(c1)) {
hostSetElementText(container, '') hostSetElementText(container, '')
if (c2 != null) { if (c2 != null) {
mountChildren(c2, container, anchor) mountChildren(c2, container, anchor)
} }
} else if (Array.isArray(c1)) { } else if (isArray(c1)) {
if (Array.isArray(c2)) { if (isArray(c2)) {
// two arrays, cannot assume anything, do full diff // two arrays, cannot assume anything, do full diff
patchKeyedChildren(c1 as VNode[], c2, container, anchor, optimized) patchKeyedChildren(c1 as VNode[], c2, container, anchor, optimized)
} else { } else {
@ -596,7 +604,7 @@ export function createRenderer(options: RendererOptions) {
const shouldRemoveChildren = vnode.type === Fragment && doRemove const shouldRemoveChildren = vnode.type === Fragment && doRemove
if (vnode.dynamicChildren != null) { if (vnode.dynamicChildren != null) {
unmountChildren(vnode.dynamicChildren, shouldRemoveChildren) unmountChildren(vnode.dynamicChildren, shouldRemoveChildren)
} else if (Array.isArray(vnode.children)) { } else if (isArray(vnode.children)) {
unmountChildren(vnode.children as VNode[], shouldRemoveChildren) unmountChildren(vnode.children as VNode[], shouldRemoveChildren)
} }
if (doRemove) { if (doRemove) {

View File

@ -6,7 +6,7 @@ export {
Fragment, Fragment,
Text, Text,
Empty Empty
} from './h' } from './vnode'
export { createRenderer, RendererOptions } from './createRenderer' export { createRenderer, RendererOptions } from './createRenderer'
export * from '@vue/observer' export * from '@vue/observer'
export { TEXT, CLASS, STYLE, PROPS, KEYED, UNKEYED } from './patchFlags' export { TEXT, CLASS, STYLE, PROPS, KEYED, UNKEYED } from './patchFlags'

View File

@ -1,3 +1,5 @@
import { isFunction } from '@vue/shared'
export const Fragment = Symbol('Fragment') export const Fragment = Symbol('Fragment')
export const Text = Symbol('Text') export const Text = Symbol('Text')
export const Empty = Symbol('Empty') export const Empty = Symbol('Empty')
@ -72,7 +74,7 @@ export function createVNode(
dynamicProps, dynamicProps,
dynamicChildren: null dynamicChildren: null
} }
if (patchFlag != null && shouldTrack) { if (shouldTrack && (patchFlag != null || isFunction(type))) {
trackDynamicNode(vnode) trackDynamicNode(vnode)
} }
return vnode return vnode

View File

@ -26,8 +26,7 @@
"@vue/observer": ["packages/observer/src"], "@vue/observer": ["packages/observer/src"],
"@vue/scheduler": ["packages/scheduler/src"], "@vue/scheduler": ["packages/scheduler/src"],
"@vue/compiler-core": ["packages/compiler-core/src"], "@vue/compiler-core": ["packages/compiler-core/src"],
"@vue/server-renderer": ["packages/server-renderer/src"], "@vue/server-renderer": ["packages/server-renderer/src"]
"@vue/decorators": ["packages/decorators/src"]
} }
}, },
"include": [ "include": [