-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathcomponent.ts
97 lines (78 loc) · 2.29 KB
/
component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { EffectScope, Ref, ref } from '@vue/reactivity'
import { EMPTY_OBJ } from '@vue/shared'
import { Block } from './render'
import { type DirectiveBinding } from './directive'
import {
type ComponentPropsOptions,
type NormalizedPropsOptions,
normalizePropsOptions,
} from './componentProps'
import type { Data } from '@vue/shared'
export type Component = FunctionalComponent | ObjectComponent
export type SetupFn = (props: any, ctx: any) => Block | Data
export type FunctionalComponent = SetupFn & {
props: ComponentPropsOptions
render(ctx: any): Block
}
export interface ObjectComponent {
props: ComponentPropsOptions
setup: SetupFn
render(ctx: any): Block
}
export interface ComponentInternalInstance {
uid: number
container: ParentNode
block: Block | null
scope: EffectScope
component: FunctionalComponent | ObjectComponent
propsOptions: NormalizedPropsOptions
// TODO: type
proxy: Data | null
// state
props: Data
setupState: Data
/** directives */
dirs: Map<Node, DirectiveBinding[]>
// lifecycle
get isMounted(): boolean
isMountedRef: Ref<boolean>
// TODO: registory of provides, appContext, lifecycles, ...
}
// 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 = (
component: ObjectComponent | FunctionalComponent,
): ComponentInternalInstance => {
const isMountedRef = ref(false)
const instance: ComponentInternalInstance = {
uid: uid++,
block: null,
container: null!, // set on mount
scope: new EffectScope(true /* detached */)!,
component,
// resolved props and emits options
propsOptions: normalizePropsOptions(component),
// emitsOptions: normalizeEmitsOptions(type, appContext), // TODO:
proxy: null,
// state
props: EMPTY_OBJ,
setupState: EMPTY_OBJ,
dirs: new Map(),
// lifecycle
get isMounted() {
return isMountedRef.value
},
isMountedRef,
// TODO: registory of provides, appContext, lifecycles, ...
}
return instance
}