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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

220 lines
4.8 KiB
TypeScript
Raw Normal View History

2023-12-29 22:05:33 +08:00
import { EffectScope, type Ref, ref } from '@vue/reactivity'
import { EMPTY_OBJ } from '@vue/shared'
2023-12-29 22:05:33 +08:00
import type { Block } from './render'
import type { DirectiveBinding } from './directive'
import {
type ComponentPropsOptions,
type NormalizedPropsOptions,
normalizePropsOptions,
} from './componentProps'
2023-12-07 01:51:57 +08:00
import type { Data } from '@vue/shared'
import { VaporLifecycleHooks } from './enums'
export type Component = FunctionalComponent | ObjectComponent
2023-12-06 14:59:11 +08:00
export type SetupFn = (props: any, ctx: any) => Block | Data
export type FunctionalComponent = SetupFn & {
props: ComponentPropsOptions
2023-12-06 14:59:11 +08:00
render(ctx: any): Block
}
export interface ObjectComponent {
props: ComponentPropsOptions
setup?: SetupFn
2023-12-06 14:59:11 +08:00
render(ctx: any): Block
}
type LifecycleHook<TFn = Function> = TFn[] | null
export interface ComponentInternalInstance {
uid: number
container: ParentNode
block: Block | null
scope: EffectScope
2023-12-06 14:59:11 +08:00
component: FunctionalComponent | ObjectComponent
propsOptions: NormalizedPropsOptions
parent: ComponentInternalInstance | null
// state
props: Data
setupState: Data
2023-12-03 18:36:01 +08:00
/** directives */
dirs: Map<Node, DirectiveBinding[]>
// lifecycle
get isMounted(): boolean
get isUnmounted(): boolean
isUnmountedRef: Ref<boolean>
isMountedRef: Ref<boolean>
// TODO: registory of provides, lifecycles, ...
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_CREATE]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.CREATED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_MOUNT]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.MOUNTED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_UPDATE]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.UPDATED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_UNMOUNT]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.UNMOUNTED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.RENDER_TRACKED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.RENDER_TRIGGERED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.ACTIVATED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.DEACTIVATED]: LifecycleHook
/**
* @internal
*/
[VaporLifecycleHooks.ERROR_CAPTURED]: LifecycleHook
/**
* @internal
*/
// [VaporLifecycleHooks.SERVER_PREFETCH]: LifecycleHook<() => Promise<unknown>>
}
2023-12-03 18:36:01 +08:00
// TODO
export let currentInstance: ComponentInternalInstance | null = null
export const getCurrentInstance: () => ComponentInternalInstance | null = () =>
currentInstance
export const setCurrentInstance = (instance: ComponentInternalInstance) => {
currentInstance = instance
}
export const unsetCurrentInstance = () => {
currentInstance = null
}
let uid = 0
export const createComponentInstance = (
2023-12-06 14:59:11 +08:00
component: ObjectComponent | FunctionalComponent,
): ComponentInternalInstance => {
2023-12-08 17:34:33 +08:00
const isMountedRef = ref(false)
const isUnmountedRef = ref(false)
const instance: ComponentInternalInstance = {
uid: uid++,
block: null,
container: null!, // set on mountComponent
scope: new EffectScope(true /* detached */)!,
component,
// TODO: registory of parent
parent: null,
// resolved props and emits options
propsOptions: normalizePropsOptions(component),
// emitsOptions: normalizeEmitsOptions(type, appContext), // TODO:
// state
props: EMPTY_OBJ,
setupState: EMPTY_OBJ,
dirs: new Map(),
// lifecycle
2023-12-08 17:34:33 +08:00
get isMounted() {
return isMountedRef.value
},
get isUnmounted() {
return isUnmountedRef.value
},
2023-12-08 17:34:33 +08:00
isMountedRef,
isUnmountedRef,
// TODO: registory of provides, appContext, lifecycles, ...
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_CREATE]: null,
/**
* @internal
*/
[VaporLifecycleHooks.CREATED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_MOUNT]: null,
/**
* @internal
*/
[VaporLifecycleHooks.MOUNTED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_UPDATE]: null,
/**
* @internal
*/
[VaporLifecycleHooks.UPDATED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.BEFORE_UNMOUNT]: null,
/**
* @internal
*/
[VaporLifecycleHooks.UNMOUNTED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.RENDER_TRACKED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.RENDER_TRIGGERED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.ACTIVATED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.DEACTIVATED]: null,
/**
* @internal
*/
[VaporLifecycleHooks.ERROR_CAPTURED]: null,
/**
* @internal
*/
// [VaporLifecycleHooks.SERVER_PREFETCH]: null,
}
return instance
}