From 1681787b434c115d7216f137f462c74b48e3d628 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 2 Jun 2019 22:22:44 +0800 Subject: [PATCH] wip: optimize children shapeFlag --- packages/runtime-core/src/component.ts | 2 +- packages/runtime-core/src/createRenderer.ts | 2 +- packages/runtime-core/src/index.ts | 10 ++--- .../src/{shapeFlags.ts => typeFlags.ts} | 0 packages/runtime-core/src/vnode.ts | 44 +++++++++---------- 5 files changed, 27 insertions(+), 31 deletions(-) rename packages/runtime-core/src/{shapeFlags.ts => typeFlags.ts} (100%) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 4a36227c4..b9bd41eb1 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -10,7 +10,7 @@ import { RenderProxyHandlers } from './componentProxy' import { ComponentPropsOptions, ExtractPropTypes } from './componentProps' import { PROPS, DYNAMIC_SLOTS, FULL_PROPS } from './patchFlags' import { Slots } from './componentSlots' -import { STATEFUL_COMPONENT } from './shapeFlags' +import { STATEFUL_COMPONENT } from './typeFlags' export type Data = { [key: string]: any } diff --git a/packages/runtime-core/src/createRenderer.ts b/packages/runtime-core/src/createRenderer.ts index 7923465ae..e01a84278 100644 --- a/packages/runtime-core/src/createRenderer.ts +++ b/packages/runtime-core/src/createRenderer.ts @@ -34,7 +34,7 @@ import { FUNCTIONAL_COMPONENT, TEXT_CHILDREN, ARRAY_CHILDREN -} from './shapeFlags' +} from './typeFlags' const prodEffectOptions = { scheduler: queueJob diff --git a/packages/runtime-core/src/index.ts b/packages/runtime-core/src/index.ts index cc12c13ed..ce76e3fe8 100644 --- a/packages/runtime-core/src/index.ts +++ b/packages/runtime-core/src/index.ts @@ -9,14 +9,12 @@ export { Portal } from './vnode' -export { FunctionalComponent, createComponent } from './component' - +export { createComponent, FunctionalComponent } from './component' +export { createRenderer, RendererOptions } from './createRenderer' export { Slot, Slots } from './componentSlots' - export { PropType, ComponentPropsOptions } from './componentProps' export * from './reactivity' export * from './componentLifecycle' -export { createRenderer, RendererOptions } from './createRenderer' - -export { TEXT, CLASS, STYLE, PROPS, KEYED, UNKEYED } from './patchFlags' +export * from './patchFlags' +export * from './typeFlags' diff --git a/packages/runtime-core/src/shapeFlags.ts b/packages/runtime-core/src/typeFlags.ts similarity index 100% rename from packages/runtime-core/src/shapeFlags.ts rename to packages/runtime-core/src/typeFlags.ts diff --git a/packages/runtime-core/src/vnode.ts b/packages/runtime-core/src/vnode.ts index 56951a259..f5b603344 100644 --- a/packages/runtime-core/src/vnode.ts +++ b/packages/runtime-core/src/vnode.ts @@ -10,7 +10,7 @@ import { TEXT_CHILDREN, ARRAY_CHILDREN, SLOTS_CHILDREN -} from './shapeFlags' +} from './typeFlags' export const Fragment = Symbol('Fragment') export const Text = Symbol('Text') @@ -102,40 +102,33 @@ export function createVNode( ): VNode { // Allow passing 0 for props, this can save bytes on generated code. props = props || null - // normalize children - children = normalizeChildren(children) as NormalizedChildren + // encode the vnode type information into a bitmap const typeFlag = isString(type) ? ELEMENT - : isFunction(type) - ? FUNCTIONAL_COMPONENT - : isObject(type) - ? STATEFUL_COMPONENT - : 0 - - const childFlag = isString(children) - ? TEXT_CHILDREN - : isArray(children) - ? ARRAY_CHILDREN - : isObject(children) - ? SLOTS_CHILDREN + : isObject(type) + ? STATEFUL_COMPONENT + : isFunction(type) + ? FUNCTIONAL_COMPONENT : 0 const vnode: VNode = { type, props, key: props && props.key, - children, + children: null, component: null, el: null, anchor: null, target: null, - shapeFlag: typeFlag | childFlag, + shapeFlag: typeFlag, patchFlag, dynamicProps, dynamicChildren: null } + normalizeChildren(vnode, children) + // class & style normalization. if (props !== null) { // class normalization only needed if the vnode isn't generated by @@ -193,18 +186,23 @@ export function normalizeVNode(child: VNodeChild): VNode { } } -export function normalizeChildren(children: unknown): NormalizedChildren { +export function normalizeChildren(vnode: VNode, children: unknown) { + let type = 0 if (children == null) { - return null + children = null } else if (isArray(children)) { - return children + type = ARRAY_CHILDREN } else if (typeof children === 'object') { - return children as RawSlots + type = SLOTS_CHILDREN } else if (isFunction(children)) { - return { default: children } + children = { default: children } + type = SLOTS_CHILDREN } else { - return isString(children) ? children : children + '' + children = isString(children) ? children : children + '' + type = TEXT_CHILDREN } + vnode.children = children as NormalizedChildren + vnode.shapeFlag |= type } function normalizeStyle(