2019-05-28 17:19:47 +08:00
|
|
|
import { VNode, normalizeVNode, VNodeChild } from './vnode'
|
2019-05-30 23:16:15 +08:00
|
|
|
import {
|
|
|
|
ReactiveEffect,
|
|
|
|
UnwrapValue,
|
2019-06-11 23:50:28 +08:00
|
|
|
state,
|
|
|
|
immutableState
|
|
|
|
} from '@vue/reactivity'
|
2019-06-19 16:43:34 +08:00
|
|
|
import { EMPTY_OBJ, isFunction, capitalize, invokeHandlers } from '@vue/shared'
|
2019-05-29 10:43:27 +08:00
|
|
|
import { RenderProxyHandlers } from './componentProxy'
|
2019-05-31 18:07:43 +08:00
|
|
|
import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
|
2019-05-31 12:25:11 +08:00
|
|
|
import { PROPS, DYNAMIC_SLOTS, FULL_PROPS } from './patchFlags'
|
2019-05-31 18:07:43 +08:00
|
|
|
import { Slots } from './componentSlots'
|
2019-06-02 22:22:44 +08:00
|
|
|
import { STATEFUL_COMPONENT } from './typeFlags'
|
2019-05-28 13:27:31 +08:00
|
|
|
|
2019-05-29 10:43:27 +08:00
|
|
|
export type Data = { [key: string]: any }
|
|
|
|
|
2019-06-13 10:25:24 +08:00
|
|
|
// public properties exposed on the proxy, which is used as the render context
|
|
|
|
// in templates (as `this` in the render option)
|
2019-06-12 15:43:19 +08:00
|
|
|
export type ComponentRenderProxy<P = {}, S = {}, PublicProps = P> = {
|
2019-05-28 17:19:47 +08:00
|
|
|
$state: S
|
2019-06-12 15:43:19 +08:00
|
|
|
$props: PublicProps
|
2019-05-28 18:06:00 +08:00
|
|
|
$attrs: Data
|
|
|
|
$refs: Data
|
|
|
|
$slots: Data
|
2019-05-29 13:43:46 +08:00
|
|
|
$root: ComponentInstance | null
|
|
|
|
$parent: ComponentInstance | null
|
2019-06-19 16:43:34 +08:00
|
|
|
$emit: (event: string, ...args: any[]) => void
|
2019-05-30 13:35:50 +08:00
|
|
|
} & P &
|
|
|
|
S
|
2019-05-28 18:06:00 +08:00
|
|
|
|
2019-06-19 16:43:34 +08:00
|
|
|
type SetupFunction<Props, RawBindings> = (
|
|
|
|
props: Props,
|
|
|
|
ctx: SetupContext
|
|
|
|
) => RawBindings | (() => VNodeChild)
|
2019-06-12 15:43:19 +08:00
|
|
|
|
2019-06-19 16:43:34 +08:00
|
|
|
type RenderFunction<Props = {}, RawBindings = {}> = <
|
2019-06-12 15:43:19 +08:00
|
|
|
Bindings extends UnwrapValue<RawBindings>
|
|
|
|
>(
|
|
|
|
this: ComponentRenderProxy<Props, Bindings>,
|
2019-06-19 16:43:34 +08:00
|
|
|
ctx: ComponentRenderProxy<Props, Bindings>
|
2019-06-12 15:43:19 +08:00
|
|
|
) => VNodeChild
|
|
|
|
|
2019-06-13 10:25:24 +08:00
|
|
|
interface ComponentOptionsWithoutProps<Props = Data, RawBindings = Data> {
|
|
|
|
props?: undefined
|
2019-06-19 16:43:34 +08:00
|
|
|
setup?: SetupFunction<Props, RawBindings>
|
|
|
|
render?: RenderFunction<Props, RawBindings>
|
2019-06-12 15:43:19 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 10:25:24 +08:00
|
|
|
interface ComponentOptionsWithArrayProps<
|
|
|
|
PropNames extends string = string,
|
|
|
|
RawBindings = Data,
|
|
|
|
Props = { [key in PropNames]?: any }
|
|
|
|
> {
|
|
|
|
props: PropNames[]
|
2019-06-19 16:43:34 +08:00
|
|
|
setup?: SetupFunction<Props, RawBindings>
|
|
|
|
render?: RenderFunction<Props, RawBindings>
|
2019-06-12 15:43:19 +08:00
|
|
|
}
|
|
|
|
|
2019-06-13 10:25:24 +08:00
|
|
|
interface ComponentOptionsWithProps<
|
|
|
|
PropsOptions = ComponentPropsOptions,
|
2019-06-01 00:47:05 +08:00
|
|
|
RawBindings = Data,
|
2019-06-13 10:25:24 +08:00
|
|
|
Props = ExtractPropTypes<PropsOptions>
|
2019-05-28 17:19:47 +08:00
|
|
|
> {
|
2019-06-13 10:25:24 +08:00
|
|
|
props: PropsOptions
|
2019-06-19 16:43:34 +08:00
|
|
|
setup?: SetupFunction<Props, RawBindings>
|
|
|
|
render?: RenderFunction<Props, RawBindings>
|
2019-05-28 17:19:47 +08:00
|
|
|
}
|
2019-05-28 13:27:31 +08:00
|
|
|
|
2019-06-13 10:25:24 +08:00
|
|
|
export type ComponentOptions =
|
2019-06-12 16:22:52 +08:00
|
|
|
| ComponentOptionsWithProps
|
|
|
|
| ComponentOptionsWithoutProps
|
|
|
|
| ComponentOptionsWithArrayProps
|
2019-06-12 15:43:19 +08:00
|
|
|
|
2019-06-19 16:43:34 +08:00
|
|
|
export interface FunctionalComponent<P = {}> {
|
|
|
|
(props: P, ctx: SetupContext): VNodeChild
|
2019-05-28 18:06:00 +08:00
|
|
|
props?: ComponentPropsOptions<P>
|
|
|
|
displayName?: string
|
|
|
|
}
|
|
|
|
|
2019-05-28 19:36:15 +08:00
|
|
|
type LifecycleHook = Function[] | null
|
|
|
|
|
2019-06-19 16:43:34 +08:00
|
|
|
interface LifecycleHooks {
|
2019-05-28 19:36:15 +08:00
|
|
|
bm: LifecycleHook // beforeMount
|
|
|
|
m: LifecycleHook // mounted
|
|
|
|
bu: LifecycleHook // beforeUpdate
|
|
|
|
u: LifecycleHook // updated
|
|
|
|
bum: LifecycleHook // beforeUnmount
|
|
|
|
um: LifecycleHook // unmounted
|
|
|
|
da: LifecycleHook // deactivated
|
|
|
|
a: LifecycleHook // activated
|
|
|
|
rtg: LifecycleHook // renderTriggered
|
|
|
|
rtc: LifecycleHook // renderTracked
|
|
|
|
ec: LifecycleHook // errorCaptured
|
|
|
|
}
|
|
|
|
|
2019-06-19 16:43:34 +08:00
|
|
|
interface SetupContext {
|
|
|
|
attrs: Data
|
|
|
|
slots: Slots
|
|
|
|
refs: Data
|
|
|
|
emit: ((event: string, ...args: any[]) => void)
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:36:16 +08:00
|
|
|
export type ComponentInstance<P = Data, S = Data> = {
|
2019-05-28 18:06:00 +08:00
|
|
|
type: FunctionalComponent | ComponentOptions
|
2019-06-03 09:43:28 +08:00
|
|
|
parent: ComponentInstance | null
|
|
|
|
root: ComponentInstance
|
2019-05-29 10:43:27 +08:00
|
|
|
vnode: VNode
|
2019-05-28 17:19:47 +08:00
|
|
|
next: VNode | null
|
2019-05-29 10:43:27 +08:00
|
|
|
subTree: VNode
|
2019-05-28 17:19:47 +08:00
|
|
|
update: ReactiveEffect
|
2019-05-29 23:44:59 +08:00
|
|
|
effects: ReactiveEffect[] | null
|
2019-06-19 16:43:34 +08:00
|
|
|
render: RenderFunction<P, S> | null
|
|
|
|
|
2019-05-29 10:43:27 +08:00
|
|
|
// the rest are only for stateful components
|
2019-05-29 11:36:16 +08:00
|
|
|
state: S
|
|
|
|
props: P
|
2019-06-19 16:43:34 +08:00
|
|
|
renderProxy: ComponentRenderProxy | null
|
|
|
|
propsProxy: P | null
|
|
|
|
setupContext: SetupContext | null
|
|
|
|
} & SetupContext &
|
|
|
|
LifecycleHooks
|
2019-05-28 19:36:15 +08:00
|
|
|
|
2019-06-13 10:25:24 +08:00
|
|
|
// createComponent
|
|
|
|
// overload 1: direct setup function
|
|
|
|
// (uses user defined props interface)
|
2019-06-12 15:43:19 +08:00
|
|
|
export function createComponent<Props>(
|
2019-06-19 16:43:34 +08:00
|
|
|
setup: (props: Props, ctx: SetupContext) => (() => any)
|
2019-06-12 15:43:19 +08:00
|
|
|
): (props: Props) => any
|
2019-06-13 10:25:24 +08:00
|
|
|
// overload 2: object format with no props
|
|
|
|
// (uses user defined props interface)
|
|
|
|
// return type is for Vetur and TSX support
|
|
|
|
export function createComponent<Props, RawBindings>(
|
|
|
|
options: ComponentOptionsWithoutProps<Props, RawBindings>
|
|
|
|
): {
|
|
|
|
new (): ComponentRenderProxy<Props, UnwrapValue<RawBindings>>
|
|
|
|
}
|
|
|
|
// overload 3: object format with array props declaration
|
|
|
|
// props inferred as { [key in PropNames]?: any }
|
|
|
|
// return type is for Vetur and TSX support
|
2019-06-12 15:43:19 +08:00
|
|
|
export function createComponent<PropNames extends string, RawBindings>(
|
|
|
|
options: ComponentOptionsWithArrayProps<PropNames, RawBindings>
|
|
|
|
): {
|
|
|
|
new (): ComponentRenderProxy<
|
|
|
|
{ [key in PropNames]?: any },
|
|
|
|
UnwrapValue<RawBindings>
|
|
|
|
>
|
|
|
|
}
|
2019-06-13 10:25:24 +08:00
|
|
|
// overload 4: object format with object props declaration
|
|
|
|
// see `ExtractPropTypes` in ./componentProps.ts
|
2019-06-12 15:43:19 +08:00
|
|
|
export function createComponent<PropsOptions, RawBindings>(
|
|
|
|
options: ComponentOptionsWithProps<PropsOptions, RawBindings>
|
2019-05-29 10:43:27 +08:00
|
|
|
): {
|
2019-06-12 15:43:19 +08:00
|
|
|
// for Vetur and TSX support
|
|
|
|
new (): ComponentRenderProxy<
|
|
|
|
ExtractPropTypes<PropsOptions>,
|
|
|
|
UnwrapValue<RawBindings>,
|
|
|
|
ExtractPropTypes<PropsOptions, false>
|
|
|
|
>
|
|
|
|
}
|
2019-06-13 10:25:24 +08:00
|
|
|
// implementation, close to no-op
|
2019-06-12 15:43:19 +08:00
|
|
|
export function createComponent(options: any) {
|
|
|
|
return isFunction(options) ? { setup: options } : (options as any)
|
2019-05-29 10:43:27 +08:00
|
|
|
}
|
|
|
|
|
2019-06-03 09:43:28 +08:00
|
|
|
export function createComponentInstance(
|
|
|
|
type: any,
|
|
|
|
parent: ComponentInstance | null
|
|
|
|
): ComponentInstance {
|
|
|
|
const instance = {
|
2019-05-28 19:36:15 +08:00
|
|
|
type,
|
2019-06-03 09:43:28 +08:00
|
|
|
parent,
|
|
|
|
root: null as any, // set later so it can point to itself
|
2019-05-29 10:43:27 +08:00
|
|
|
vnode: null as any,
|
2019-05-28 19:36:15 +08:00
|
|
|
next: null,
|
2019-05-29 10:43:27 +08:00
|
|
|
subTree: null as any,
|
2019-05-28 19:36:15 +08:00
|
|
|
update: null as any,
|
2019-06-12 15:43:19 +08:00
|
|
|
render: null,
|
2019-05-30 23:16:15 +08:00
|
|
|
renderProxy: null,
|
|
|
|
propsProxy: null,
|
2019-06-19 16:43:34 +08:00
|
|
|
setupContext: null,
|
2019-05-28 19:36:15 +08:00
|
|
|
|
|
|
|
bm: null,
|
|
|
|
m: null,
|
|
|
|
bu: null,
|
|
|
|
u: null,
|
|
|
|
um: null,
|
|
|
|
bum: null,
|
|
|
|
da: null,
|
|
|
|
a: null,
|
|
|
|
rtg: null,
|
|
|
|
rtc: null,
|
|
|
|
ec: null,
|
2019-05-29 23:44:59 +08:00
|
|
|
effects: null,
|
2019-05-28 19:36:15 +08:00
|
|
|
|
|
|
|
// public properties
|
2019-05-29 11:36:16 +08:00
|
|
|
state: EMPTY_OBJ,
|
|
|
|
props: EMPTY_OBJ,
|
|
|
|
attrs: EMPTY_OBJ,
|
|
|
|
slots: EMPTY_OBJ,
|
2019-06-19 16:43:34 +08:00
|
|
|
refs: EMPTY_OBJ,
|
|
|
|
|
|
|
|
emit: (event: string, ...args: any[]) => {
|
|
|
|
const props = instance.vnode.props || EMPTY_OBJ
|
|
|
|
const handler = props[`on${event}`] || props[`on${capitalize(event)}`]
|
|
|
|
if (handler) {
|
|
|
|
invokeHandlers(handler, args)
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
2019-06-03 09:43:28 +08:00
|
|
|
|
|
|
|
instance.root = parent ? parent.root : instance
|
|
|
|
return instance
|
2019-05-28 19:36:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export let currentInstance: ComponentInstance | null = null
|
|
|
|
|
2019-05-29 11:36:16 +08:00
|
|
|
export function setupStatefulComponent(instance: ComponentInstance) {
|
2019-05-29 10:43:27 +08:00
|
|
|
const Component = instance.type as ComponentOptions
|
2019-05-28 19:36:15 +08:00
|
|
|
// 1. create render proxy
|
2019-05-30 23:16:15 +08:00
|
|
|
const proxy = (instance.renderProxy = new Proxy(
|
2019-05-29 11:36:16 +08:00
|
|
|
instance,
|
|
|
|
RenderProxyHandlers
|
|
|
|
) as any)
|
|
|
|
// 2. call setup()
|
2019-05-30 23:16:15 +08:00
|
|
|
const { setup } = Component
|
|
|
|
if (setup) {
|
2019-05-28 19:36:15 +08:00
|
|
|
currentInstance = instance
|
2019-05-30 23:16:15 +08:00
|
|
|
// the props proxy makes the props object passed to setup() reactive
|
|
|
|
// so props change can be tracked by watchers
|
|
|
|
// it will be updated in resolveProps() on updates before render
|
|
|
|
const propsProxy = (instance.propsProxy = setup.length
|
2019-06-11 23:50:28 +08:00
|
|
|
? immutableState(instance.props)
|
2019-05-30 23:16:15 +08:00
|
|
|
: null)
|
2019-06-19 16:43:34 +08:00
|
|
|
const setupContext = (instance.setupContext =
|
|
|
|
setup.length > 1 ? createSetupContext(instance) : null)
|
|
|
|
const setupResult = setup.call(proxy, propsProxy, setupContext)
|
2019-06-12 15:43:19 +08:00
|
|
|
if (isFunction(setupResult)) {
|
2019-06-13 10:25:24 +08:00
|
|
|
// setup returned an inline render function
|
2019-06-12 15:43:19 +08:00
|
|
|
instance.render = setupResult
|
|
|
|
} else {
|
2019-06-13 10:25:24 +08:00
|
|
|
// setup returned bindings.
|
|
|
|
// assuming a render function compiled from template is present.
|
2019-06-12 15:43:19 +08:00
|
|
|
instance.state = state(setupResult)
|
|
|
|
if (__DEV__ && !Component.render) {
|
|
|
|
// TODO warn missing render fn
|
|
|
|
}
|
|
|
|
instance.render = Component.render as RenderFunction
|
|
|
|
}
|
2019-05-28 19:36:15 +08:00
|
|
|
currentInstance = null
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 17:19:47 +08:00
|
|
|
|
2019-06-19 16:43:34 +08:00
|
|
|
const SetupProxyHandlers: { [key: string]: ProxyHandler<any> } = {}
|
|
|
|
;['attrs', 'slots', 'refs'].forEach((type: string) => {
|
|
|
|
SetupProxyHandlers[type] = {
|
|
|
|
get: (instance: any, key: string) => (instance[type] as any)[key],
|
|
|
|
has: (instance: any, key: string) => key in (instance[type] as any),
|
|
|
|
ownKeys: (instance: any) => Object.keys(instance[type] as any),
|
|
|
|
set: () => false,
|
|
|
|
deleteProperty: () => false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
function createSetupContext(instance: ComponentInstance): SetupContext {
|
|
|
|
const context = {
|
|
|
|
// attrs, slots & refs are non-reactive, but they need to always expose
|
|
|
|
// the latest values (instance.xxx may get replaced during updates) so we
|
|
|
|
// need to expose them through a proxy
|
|
|
|
attrs: new Proxy(instance, SetupProxyHandlers.attrs),
|
|
|
|
slots: new Proxy(instance, SetupProxyHandlers.slots),
|
|
|
|
refs: new Proxy(instance, SetupProxyHandlers.refs),
|
|
|
|
emit: instance.emit
|
|
|
|
} as any
|
|
|
|
return __DEV__ ? Object.freeze(context) : context
|
|
|
|
}
|
|
|
|
|
2019-05-29 11:36:16 +08:00
|
|
|
export function renderComponentRoot(instance: ComponentInstance): VNode {
|
2019-06-19 16:43:34 +08:00
|
|
|
const {
|
|
|
|
type: Component,
|
|
|
|
vnode,
|
|
|
|
renderProxy,
|
|
|
|
setupContext,
|
|
|
|
props,
|
|
|
|
slots,
|
|
|
|
attrs,
|
|
|
|
refs,
|
|
|
|
emit
|
|
|
|
} = instance
|
2019-06-02 16:35:19 +08:00
|
|
|
if (vnode.shapeFlag & STATEFUL_COMPONENT) {
|
2019-05-30 23:16:15 +08:00
|
|
|
return normalizeVNode(
|
2019-06-19 16:43:34 +08:00
|
|
|
(instance.render as RenderFunction).call(renderProxy, props, setupContext)
|
2019-05-30 23:16:15 +08:00
|
|
|
)
|
2019-06-02 16:35:19 +08:00
|
|
|
} else {
|
|
|
|
// functional
|
2019-06-12 15:43:19 +08:00
|
|
|
return normalizeVNode(
|
2019-06-19 16:43:34 +08:00
|
|
|
(Component as FunctionalComponent)(props, {
|
|
|
|
attrs,
|
|
|
|
slots,
|
|
|
|
refs,
|
|
|
|
emit
|
|
|
|
})
|
2019-06-12 15:43:19 +08:00
|
|
|
)
|
2019-05-28 17:19:47 +08:00
|
|
|
}
|
2019-05-28 13:27:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function shouldUpdateComponent(
|
|
|
|
prevVNode: VNode,
|
2019-06-01 02:14:49 +08:00
|
|
|
nextVNode: VNode,
|
|
|
|
optimized?: boolean
|
2019-05-28 13:27:31 +08:00
|
|
|
): boolean {
|
2019-05-31 12:25:11 +08:00
|
|
|
const { props: prevProps, children: prevChildren } = prevVNode
|
|
|
|
const { props: nextProps, children: nextChildren, patchFlag } = nextVNode
|
2019-06-03 09:43:28 +08:00
|
|
|
if (patchFlag) {
|
2019-05-31 12:25:11 +08:00
|
|
|
if (patchFlag & DYNAMIC_SLOTS) {
|
2019-05-30 16:00:42 +08:00
|
|
|
// slot content that references values that might have changed,
|
|
|
|
// e.g. in a v-for
|
|
|
|
return true
|
|
|
|
}
|
2019-05-31 12:25:11 +08:00
|
|
|
if (patchFlag & FULL_PROPS) {
|
|
|
|
// presence of this flag indicates props are always non-null
|
|
|
|
return hasPropsChanged(prevProps as Data, nextProps as Data)
|
|
|
|
} else if (patchFlag & PROPS) {
|
2019-05-30 16:00:42 +08:00
|
|
|
const dynamicProps = nextVNode.dynamicProps as string[]
|
|
|
|
for (let i = 0; i < dynamicProps.length; i++) {
|
|
|
|
const key = dynamicProps[i]
|
|
|
|
if ((nextProps as any)[key] !== (prevProps as any)[key]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 02:14:49 +08:00
|
|
|
} else if (!optimized) {
|
2019-05-31 12:25:11 +08:00
|
|
|
// this path is only taken by manually written render functions
|
|
|
|
// so presence of any children leads to a forced update
|
|
|
|
if (prevChildren != null || nextChildren != null) {
|
|
|
|
return true
|
|
|
|
}
|
2019-05-30 16:00:42 +08:00
|
|
|
if (prevProps === nextProps) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (prevProps === null) {
|
|
|
|
return nextProps !== null
|
|
|
|
}
|
|
|
|
if (nextProps === null) {
|
|
|
|
return prevProps !== null
|
|
|
|
}
|
2019-05-31 12:25:11 +08:00
|
|
|
return hasPropsChanged(prevProps, nextProps)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasPropsChanged(prevProps: Data, nextProps: Data): boolean {
|
|
|
|
const nextKeys = Object.keys(nextProps)
|
|
|
|
if (nextKeys.length !== Object.keys(prevProps).length) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for (let i = 0; i < nextKeys.length; i++) {
|
|
|
|
const key = nextKeys[i]
|
|
|
|
if (nextProps[key] !== prevProps[key]) {
|
2019-05-28 13:27:31 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|