vue3-yuanma/packages/runtime-core/src/component.ts

200 lines
4.8 KiB
TypeScript
Raw Normal View History

2019-05-28 17:19:47 +08:00
import { VNode, normalizeVNode, VNodeChild } from './vnode'
2019-05-29 22:11:33 +08:00
import { ReactiveEffect, UnwrapBindings, observable } from '@vue/observer'
2019-05-28 19:36:15 +08:00
import { isFunction, EMPTY_OBJ } from '@vue/shared'
2019-05-29 10:43:27 +08:00
import { RenderProxyHandlers } from './componentProxy'
2019-05-29 11:36:16 +08:00
import { ComponentPropsOptions, PropValidator } from './componentProps'
2019-05-28 13:27:31 +08:00
2019-05-29 10:43:27 +08:00
export type Data = { [key: string]: any }
2019-05-28 17:19:47 +08:00
type ExtractPropTypes<PropOptions> = {
2019-05-29 10:43:27 +08:00
readonly [key in keyof PropOptions]: PropOptions[key] extends PropValidator<
infer V
>
2019-05-28 17:19:47 +08:00
? V
: PropOptions[key] extends null | undefined ? any : PropOptions[key]
}
2019-05-28 18:06:00 +08:00
export interface ComponentPublicProperties<P = Data, S = Data> {
2019-05-28 17:19:47 +08:00
$state: S
2019-05-28 18:06:00 +08:00
$props: P
$attrs: Data
// TODO
$refs: Data
$slots: Data
2019-05-29 13:43:46 +08:00
$root: ComponentInstance | null
$parent: ComponentInstance | null
2019-05-28 18:06:00 +08:00
}
2019-05-28 17:19:47 +08:00
export interface ComponentOptions<
2019-05-28 18:06:00 +08:00
RawProps = ComponentPropsOptions,
RawBindings = Data | void,
2019-05-28 17:19:47 +08:00
Props = ExtractPropTypes<RawProps>,
Bindings = UnwrapBindings<RawBindings>
> {
props?: RawProps
setup?: (props: Props) => RawBindings
render?: <B extends Bindings>(
this: ComponentPublicProperties<Props, B>,
2019-05-29 11:36:16 +08:00
ctx: ComponentInstance<Props, B>
2019-05-28 17:19:47 +08:00
) => VNodeChild
}
2019-05-28 13:27:31 +08:00
2019-05-28 18:06:00 +08:00
export interface FunctionalComponent<P = {}> {
2019-05-29 11:36:16 +08:00
(ctx: ComponentInstance<P>): any
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
export interface LifecycleHooks {
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-05-29 11:36:16 +08:00
export type Slot = (...args: any[]) => VNode[]
export type Slots = Readonly<{
[name: string]: Slot
}>
export type ComponentInstance<P = Data, S = Data> = {
2019-05-28 18:06:00 +08:00
type: FunctionalComponent | ComponentOptions
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 10:43:27 +08:00
// the rest are only for stateful components
2019-05-29 11:36:16 +08:00
proxy: ComponentPublicProperties | null
state: S
props: P
attrs: Data
slots: Slots
refs: Data
} & LifecycleHooks
2019-05-28 19:36:15 +08:00
2019-05-29 10:43:27 +08:00
// no-op, for type inference only
export function createComponent<
RawProps,
RawBindings,
Props = ExtractPropTypes<RawProps>,
Bindings = UnwrapBindings<RawBindings>
>(
options: ComponentOptions<RawProps, RawBindings, Props, Bindings>
): {
// for TSX
new (): { $props: Props }
} {
return options as any
}
export function createComponentInstance(type: any): ComponentInstance {
return {
2019-05-28 19:36:15 +08:00
type,
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,
proxy: null,
bm: null,
m: null,
bu: null,
u: null,
um: null,
bum: null,
da: null,
a: null,
rtg: null,
rtc: null,
ec: null,
// public properties
2019-05-29 11:36:16 +08:00
state: EMPTY_OBJ,
props: EMPTY_OBJ,
attrs: EMPTY_OBJ,
slots: EMPTY_OBJ,
refs: EMPTY_OBJ
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-29 11:36:16 +08:00
const proxy = (instance.proxy = new Proxy(
instance,
RenderProxyHandlers
) as any)
// 2. call setup()
2019-05-29 10:43:27 +08:00
if (Component.setup) {
2019-05-28 19:36:15 +08:00
currentInstance = instance
2019-05-29 11:36:16 +08:00
// TODO should pass reactive props here
2019-05-29 13:43:46 +08:00
instance.state = observable(Component.setup.call(proxy, instance.props))
2019-05-28 19:36:15 +08:00
currentInstance = null
}
}
2019-05-28 17:19:47 +08:00
2019-05-29 11:36:16 +08:00
export function renderComponentRoot(instance: ComponentInstance): VNode {
const { type: Component, proxy } = instance
if (isFunction(Component)) {
return normalizeVNode(Component(instance))
2019-05-28 17:19:47 +08:00
} else {
2019-05-29 11:36:16 +08:00
if (__DEV__ && !Component.render) {
2019-05-28 17:19:47 +08:00
// TODO warn missing render
}
2019-05-29 11:36:16 +08:00
return normalizeVNode((Component.render as Function).call(proxy, instance))
2019-05-28 17:19:47 +08:00
}
2019-05-28 13:27:31 +08:00
}
export function shouldUpdateComponent(
prevVNode: VNode,
nextVNode: VNode
): boolean {
const { props: prevProps } = prevVNode
const { props: nextProps } = nextVNode
// TODO handle slots
// If has different slots content, or has non-compiled slots,
// the child needs to be force updated.
// if (
// prevChildFlags !== nextChildFlags ||
// (nextChildFlags & ChildrenFlags.DYNAMIC_SLOTS) > 0
// ) {
// return true
// }
if (prevProps === nextProps) {
return false
}
if (prevProps === null) {
return nextProps !== null
}
if (nextProps === null) {
return prevProps !== null
}
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]) {
return true
}
}
return false
}