-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.d.ts
254 lines (237 loc) · 7.92 KB
/
index.d.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
declare class RefImpl<T = any> {
private _value;
private readonly _target;
constructor(value: T);
get value(): T;
set value(value: T);
}
declare function ref<T extends any>(value: T): RefImpl<T>;
declare function state<T extends any>(value: T): RefImpl<T>;
declare function defineState<T extends Record<string | symbol, any>>(target: T): T;
/**
* 响应式对象
*/
declare class Activer<T = any> {
callback: () => T;
flag: string;
constructor(fn: () => T);
get value(): T;
}
/**
* 外置函数
*/
declare function active<T extends any>(fn: () => T): Activer<T>;
declare type Meta = {
targetPropOldValue: any;
targetPropnewValue: any;
};
/**
* 观察者
* 观察数据的变化
*/
declare class Watcher<T = any> {
value: T;
callback: (oldValue: T, newValue: T, meta?: Meta) => void;
activeProps: Activer<T>;
depArr?: Array<any> | false;
nextDepArr?: Array<any>;
constructor(activeProps: Activer<T>, callback: (oldValue: T, newValue: T, meta?: Meta) => void);
update(): void;
}
/**
* 监控自定义响应式属性
*/
declare function watch<T = any>(activeProps: (() => T) | Activer<T>, callback: (oldValue: T, newValue: T) => void): Watcher<T>;
/**
* 监控可变状态dom
*/
declare function watchVNode(activeVNode: VAlive, callback: (oldVNode: VNode, newVNode: VNode) => void): VNode;
/**
* 监控可变dom的prop
*/
declare function watchProp(activeProp: Activer, callback: (oldVNode: VNode, newVNode: VNode) => void): any;
/**
* 实现响应式对象
*/
declare function reactive<T extends Record<string | symbol, any>>(target: T): T;
/**
* 响应触发依赖
*/
declare function trigger(target: Record<string | symbol, any>, prop: string | symbol): void;
/**
* 追踪绑定依赖
*/
declare function track(target: Record<string | symbol, any>, prop: string | symbol): void;
declare function setActiver(fn: Watcher | null): void;
declare function getActiver(): Watcher | null;
/**
* 传说中的render函数
*/
declare type HSymbol = typeof TextSymbol | typeof FragmentSymbol | typeof AliveSymbol | typeof ArraySymbol;
declare type HType = string | HSymbol | ComponentType;
declare type H = (type: HType, props?: VNodeProps, children?: OriginVNode | Array<OriginVNode>) => VNode;
/**
* text(不需要props)、fragment(不需要props)、element、component为显性创建
* array(不需要props)、alive(不需要props)为隐形创建
*/
declare function renderApi(type: HType, props?: VNodeProps | null, children?: OriginVNode | Array<OriginVNode>): VNode;
interface AppUtils {
state: typeof state;
defineState: typeof defineState;
h: typeof renderApi;
watch: typeof watch;
}
interface AppContext {
utils: AppUtils;
}
interface AppPlugin {
install(utils: AppContext): void;
}
/**
* 根类组件用于类型提示
* 抽象类
*/
interface ComponentInstance {
props?: VNodeProps;
children?: Array<OriginVNode>;
utils: AppUtils;
life?: ComponentLifeCycle;
render(h?: H): OriginVNode;
/** 组件示例创建之后触发 */
created?(): void;
/** 组件示例在渲染之前触发(DOM还没有生成) */
beforeMounted?(): void;
/** dom已经生成到js内存但还没挂载 */
readyMounted?(): void;
/** dom挂载到了页面上*/
mounted?(): void;
/** 卸载之前触发 */
beforeUnMounted?(): void;
/** 卸载之后触发 */
unMounted?(): void;
}
declare abstract class Component implements ComponentInstance {
abstract props?: VNodeProps;
abstract children?: Array<OriginVNode>;
abstract render(h?: H): VNode;
utils: AppUtils;
abstract life?: ComponentLifeCycle;
/** 组件示例创建之后触发 */
abstract created?(): void;
/** 组件示例在渲染之前触发(DOM还没有生成) */
abstract beforeMounted?(): void;
/** dom已经生成到js内存但还没挂载 */
abstract readyMounted?(): void;
/** dom挂载到了页面上*/
abstract mounted?(): void;
/** 卸载之前触发 */
abstract beforeUnMounted?(): void;
/** 卸载之后触发 */
abstract unMounted?(): void;
}
declare function defineComponent(componentType: FunctionComponentType): FunctionComponentType;
/**
* 主要实现关于组件的生命周期
*/
interface ComponentLifeCycleInstance {
/** 组件示例创建之后触发 */
created(fn: () => void): void;
/** 组件示例在渲染之前触发(DOM还没有生成) */
beforeMounted(fn: () => void): void;
/** dom已经生成到js内存但还没挂载 */
readyMounted(fn: () => void): void;
/** dom挂载到了页面上*/
mounted(fn: () => void): void;
/** 卸载之前触发 */
beforeUnMounted(fn: () => void): void;
/** 卸载之后触发 */
unMounted(fn: () => void): void;
}
declare class ComponentLifeCycle implements ComponentLifeCycleInstance {
private readonly createdList;
private readonly beforeMountedList;
private readonly readyMountedList;
private readonly mountedList;
private readonly beforeUnMountedList;
private readonly unMountedList;
constructor(createdList?: Array<() => void>, beforeMountedList?: Array<() => void>, readyMountedList?: Array<() => void>, mountedList?: Array<() => void>, beforeUnMountedList?: Array<() => void>, unMountedList?: Array<() => void>);
created(fn: () => void): void;
beforeMounted(fn: () => void): void;
readyMounted(fn: () => void): void;
mounted(fn: () => void): void;
beforeUnMounted(fn: () => void): void;
unMounted(fn: () => void): void;
emit(lifeName: keyof ComponentLifeCycleInstance): void;
}
interface ComponentContext {
props: Record<string, any>;
children: Array<VNode>;
life: ComponentLifeCycle;
utils: AppUtils;
}
/** 函数组件类型 */
interface FunctionComponentType {
(context: ComponentContext): OriginVNode;
}
/** 类组件类型 */
interface ClassComponentType {
new (props: Record<string, any>, children: Array<VNode>): ComponentInstance;
}
declare type ComponentType = FunctionComponentType | ClassComponentType;
declare const VComponentSymbol: unique symbol;
/**
* 虚拟dom节点类型枚举
*/
declare enum VNODE_TYPE {
ELEMENT = 0,
TEXT = 1,
FRAGMENT = 2,
COMPONENT = 3,
ARRAYNODE = 4,
ALIVE = 5
}
declare type VNodeElement = ChildNode;
/**
* 虚拟dom接口类型
*/
interface VNode {
type: string | symbol | ComponentType;
flag: VNODE_TYPE;
props?: Record<string, any>;
children?: Array<VNode> | string;
el?: VNodeElement;
anchor?: VNodeElement;
activer?: Activer;
vnode?: VNode;
}
declare type NotFunctionOriginVNode = string | VNode | Array<OriginVNode> | Activer;
declare type WithFunctionOriginVNode = (() => OriginVNode) | NotFunctionOriginVNode;
declare type OriginVNode = WithFunctionOriginVNode | Exclude<any, WithFunctionOriginVNode>;
/** 虚拟节点属性 todo */
interface VNodeProps {
[propName: string]: any;
}
declare const TextSymbol: unique symbol;
declare const FragmentSymbol: unique symbol;
declare const ArraySymbol: unique symbol;
declare const AliveSymbol: unique symbol;
interface VAlive extends VNode {
type: symbol;
flag: VNODE_TYPE.ELEMENT;
activer: Activer<OriginVNode>;
vnode: VNode;
}
interface Options {
arrayDiff?: boolean;
}
declare class App {
rootVNode: VNode;
options: Options;
private pluginList;
constructor(vNode: VNode, options?: Options);
mount(selector?: string | HTMLElement): void;
use(plugin: AppPlugin): this;
}
declare function createApp(vNode: VNode, options?: Options): App;
declare function mount(vnode: VNode, container: HTMLElement, anchor?: VNodeElement, app?: App): void;
export { Activer, Component, RefImpl, ArraySymbol as VArray, VComponentSymbol as VComponent, FragmentSymbol as VFragment, TextSymbol as VText, Watcher, active, createApp, renderApi as createVNode, App as default, defineComponent, defineState, getActiver, renderApi as h, mount, reactive, ref, renderApi as render, setActiver, state, track, trigger, watch, watchProp, watchVNode };