From 08806073a152dd27b0100580df14b5885979562d Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 13 Jun 2019 10:25:24 +0800 Subject: [PATCH] chore: comments --- packages/runtime-core/src/component.ts | 63 +++++++++++++++----------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index 3b9854a11..a92951c3b 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -14,15 +14,14 @@ import { STATEFUL_COMPONENT } from './typeFlags' export type Data = { [key: string]: any } +// public properties exposed on the proxy, which is used as the render context +// in templates (as `this` in the render option) export type ComponentRenderProxy

= { $state: S $props: PublicProps $attrs: Data - - // TODO $refs: Data $slots: Data - $root: ComponentInstance | null $parent: ComponentInstance | null } & P & @@ -45,19 +44,6 @@ type RenderFunctionWithThis = < vnode: VNode ) => VNodeChild -interface ComponentOptionsWithProps< - PropsOptions = ComponentPropsOptions, - RawBindings = Data, - Props = ExtractPropTypes -> { - props: PropsOptions - setup?: ( - this: ComponentRenderProxy, - props: Props - ) => RawBindings | RenderFunction - render?: RenderFunctionWithThis -} - interface ComponentOptionsWithoutProps { props?: undefined setup?: ( @@ -80,7 +66,20 @@ interface ComponentOptionsWithArrayProps< render?: RenderFunctionWithThis } -type ComponentOptions = +interface ComponentOptionsWithProps< + PropsOptions = ComponentPropsOptions, + RawBindings = Data, + Props = ExtractPropTypes +> { + props: PropsOptions + setup?: ( + this: ComponentRenderProxy, + props: Props + ) => RawBindings | RenderFunction + render?: RenderFunctionWithThis +} + +export type ComponentOptions = | ComponentOptionsWithProps | ComponentOptionsWithoutProps | ComponentOptionsWithArrayProps @@ -126,25 +125,33 @@ export type ComponentInstance

= { refs: Data } & LifecycleHooks -// no-op, for type inference only +// createComponent +// overload 1: direct setup function +// (uses user defined props interface) export function createComponent( setup: (props: Props) => RenderFunction ): (props: Props) => any +// overload 2: object format with no props +// (uses user defined props interface) +// return type is for Vetur and TSX support +export function createComponent( + options: ComponentOptionsWithoutProps +): { + new (): ComponentRenderProxy> +} +// overload 3: object format with array props declaration +// props inferred as { [key in PropNames]?: any } +// return type is for Vetur and TSX support export function createComponent( options: ComponentOptionsWithArrayProps ): { - // for Vetur and TSX support new (): ComponentRenderProxy< { [key in PropNames]?: any }, UnwrapValue > } -export function createComponent( - options: ComponentOptionsWithoutProps -): { - // for Vetur and TSX support - new (): ComponentRenderProxy> -} +// overload 4: object format with object props declaration +// see `ExtractPropTypes` in ./componentProps.ts export function createComponent( options: ComponentOptionsWithProps ): { @@ -155,6 +162,7 @@ export function createComponent( ExtractPropTypes > } +// implementation, close to no-op export function createComponent(options: any) { return isFunction(options) ? { setup: options } : (options as any) } @@ -222,10 +230,11 @@ export function setupStatefulComponent(instance: ComponentInstance) { : null) const setupResult = setup.call(proxy, propsProxy) if (isFunction(setupResult)) { - // setup returned a render function + // setup returned an inline render function instance.render = setupResult } else { - // setup returned bindings + // setup returned bindings. + // assuming a render function compiled from template is present. instance.state = state(setupResult) if (__DEV__ && !Component.render) { // TODO warn missing render fn