@feng3d/reactivity 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +158 -158
- package/dist/assets/RobotoMono-Medium-DVgDz_OO.woff2 +0 -0
- package/dist/assets/RobotoMono-Regular-BPoF81uy.woff2 +0 -0
- package/dist/assets/index-a2qCSG5V.css +629 -0
- package/dist/assets/index.html-Dyp3udP2.js +200 -0
- package/dist/assets/modulepreload-polyfill-DaKOjhqt.js +37 -0
- package/dist/assets/package-9zMEdmDL.js +2540 -0
- package/dist/assets/src//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274/index.html-a69uOZEV.js +59 -0
- package/dist/assets/src//346/225/260/347/273/204/index.html-CaZ_5kCZ.js +43 -0
- package/dist/docs/.nojekyll +1 -0
- package/dist/docs/assets/hierarchy.js +1 -0
- package/dist/docs/assets/highlight.css +92 -0
- package/dist/docs/assets/icons.js +18 -0
- package/dist/docs/assets/icons.svg +1 -0
- package/dist/docs/assets/main.js +60 -0
- package/dist/docs/assets/navigation.js +1 -0
- package/dist/docs/assets/search.js +1 -0
- package/dist/docs/assets/style.css +1640 -0
- package/dist/docs/classes/ComputedReactivity.html +72 -0
- package/dist/docs/classes/EffectReactivity.html +62 -0
- package/dist/docs/classes/EffectScope.html +40 -0
- package/dist/docs/classes/Reactivity.html +35 -0
- package/dist/docs/classes/RefReactivity.html +57 -0
- package/dist/docs/functions/batchRun.html +15 -0
- package/dist/docs/functions/computed.html +5 -0
- package/dist/docs/functions/effect.html +11 -0
- package/dist/docs/functions/effectScope.html +5 -0
- package/dist/docs/functions/forceTrack.html +6 -0
- package/dist/docs/functions/getCurrentScope.html +4 -0
- package/dist/docs/functions/isProxy.html +5 -0
- package/dist/docs/functions/isReactive.html +5 -0
- package/dist/docs/functions/isRef.html +5 -0
- package/dist/docs/functions/noTrack.html +6 -0
- package/dist/docs/functions/onScopeDispose.html +6 -0
- package/dist/docs/functions/reactive.html +19 -0
- package/dist/docs/functions/ref.html +13 -0
- package/dist/docs/functions/toRaw.html +4 -0
- package/dist/docs/hierarchy.html +1 -0
- package/dist/docs/index.html +129 -0
- package/dist/docs/interfaces/Computed.html +9 -0
- package/dist/docs/interfaces/Effect.html +8 -0
- package/dist/docs/interfaces/Ref.html +9 -0
- package/dist/docs/modules.html +1 -0
- package/dist/docs/types/Reactive.html +3 -0
- package/dist/docs/types/UnReadonly.html +3 -0
- package/dist/files/RobotoMono-Medium.woff2 +0 -0
- package/dist/files/RobotoMono-Regular.woff2 +0 -0
- package/dist/files/ic_code_black_24dp.svg +4 -0
- package/dist/files/ic_search_black_24dp.svg +4 -0
- package/dist/files/main.css +629 -0
- package/dist/files/thumbnails.svg +7 -0
- package/dist/files.json +7 -0
- package/dist/index.html +84 -0
- package/dist/index.js +735 -156
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +735 -156
- package/dist/index.umd.cjs.map +1 -1
- package/dist/screenshots//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274.jpg +0 -0
- package/dist/screenshots//346/225/260/347/273/204.jpg +0 -0
- package/dist/src//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274/index.html +70 -0
- package/dist/src//346/225/260/347/273/204/index.html +65 -0
- package/dist/tags.json +2 -0
- package/lib/Reactivity.d.ts +47 -13
- package/lib/Reactivity.d.ts.map +1 -1
- package/lib/arrayInstrumentations.d.ts +10 -0
- package/lib/arrayInstrumentations.d.ts.map +1 -1
- package/lib/baseHandlers.d.ts +3 -1
- package/lib/baseHandlers.d.ts.map +1 -1
- package/lib/batch.d.ts +17 -5
- package/lib/batch.d.ts.map +1 -1
- package/lib/collectionHandlers.d.ts +21 -0
- package/lib/collectionHandlers.d.ts.map +1 -1
- package/lib/computed.d.ts +75 -27
- package/lib/computed.d.ts.map +1 -1
- package/lib/effect.d.ts +25 -0
- package/lib/effect.d.ts.map +1 -1
- package/lib/effectScope.d.ts +129 -0
- package/lib/effectScope.d.ts.map +1 -0
- package/lib/index.d.ts +6 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/property.d.ts.map +1 -1
- package/lib/reactive.d.ts +63 -14
- package/lib/reactive.d.ts.map +1 -1
- package/lib/ref.d.ts +66 -4
- package/lib/ref.d.ts.map +1 -1
- package/lib/shared/constants.d.ts +7 -7
- package/lib/shared/constants.d.ts.map +1 -1
- package/lib/shared/general.d.ts +1 -1
- package/lib/shared/general.d.ts.map +1 -1
- package/package.json +71 -66
- package/src/Reactivity.ts +168 -126
- package/src/arrayInstrumentations.ts +801 -448
- package/src/baseHandlers.ts +312 -220
- package/src/batch.ts +118 -91
- package/src/collectionHandlers.ts +486 -298
- package/src/computed.ts +253 -204
- package/src/effect.ts +146 -108
- package/src/effectScope.ts +294 -0
- package/src/index.ts +9 -8
- package/src/property.ts +231 -225
- package/src/reactive.ts +186 -130
- package/src/ref.ts +150 -88
- package/src/shared/constants.ts +41 -41
- package/src/shared/general.ts +109 -109
- package/tsconfig.json +19 -19
package/src/reactive.ts
CHANGED
|
@@ -1,130 +1,186 @@
|
|
|
1
|
-
import { mutableHandlers } from './baseHandlers';
|
|
2
|
-
import { mutableCollectionHandlers } from './collectionHandlers';
|
|
3
|
-
import { Computed } from './computed';
|
|
4
|
-
import { Ref } from './ref';
|
|
5
|
-
import { ReactiveFlags, TargetType } from './shared/constants';
|
|
6
|
-
import { getTargetType, isObject, Target } from './shared/general';
|
|
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
|
-
if (
|
|
40
|
-
{
|
|
41
|
-
return
|
|
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
|
-
|
|
1
|
+
import { mutableHandlers } from './baseHandlers';
|
|
2
|
+
import { mutableCollectionHandlers } from './collectionHandlers';
|
|
3
|
+
import { Computed } from './computed';
|
|
4
|
+
import { Ref } from './ref';
|
|
5
|
+
import { ReactiveFlags, TargetType } from './shared/constants';
|
|
6
|
+
import { getTargetType, isObject, Target } from './shared/general';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 移除对象属性中的 readonly 关键字。
|
|
10
|
+
*
|
|
11
|
+
* 用于将只读类型转换为可写类型。
|
|
12
|
+
*/
|
|
13
|
+
export type UnReadonly<T> = {
|
|
14
|
+
-readonly [P in keyof T]: T[P];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 创建或者获取响应式对象的代理对象。
|
|
19
|
+
*
|
|
20
|
+
* 将普通对象转换为响应式对象,使其属性可以被追踪和更新。
|
|
21
|
+
*
|
|
22
|
+
* 特点:
|
|
23
|
+
* 1. 支持对象和集合类型的响应式转换
|
|
24
|
+
* 2. 自动处理嵌套对象的响应式转换
|
|
25
|
+
* 3. 缓存已创建的代理对象
|
|
26
|
+
* 4. 支持只读属性的响应式转换
|
|
27
|
+
*
|
|
28
|
+
* 注意:
|
|
29
|
+
* 1. 扩大被反应式的对象的类型范围,只有 `Object.isExtensible` 不通过的对象不被响应化。
|
|
30
|
+
* Float32Array 等都允许被响应化。
|
|
31
|
+
* 2. 被反应式的对象的只读属性也会被标记为可编辑。希望被反应对象属性一般为只读属性,
|
|
32
|
+
* 通过反应式来改变属性值,同时又通过反应式系统来处理其更改后响应逻辑。
|
|
33
|
+
*
|
|
34
|
+
* @param target 要转换为响应式的对象
|
|
35
|
+
* @returns 响应式代理对象
|
|
36
|
+
*/
|
|
37
|
+
export function reactive<T extends object>(target: T): Reactive<T>
|
|
38
|
+
{
|
|
39
|
+
if (!isObject(target))
|
|
40
|
+
{
|
|
41
|
+
return target;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (target[ReactiveFlags.RAW])
|
|
45
|
+
{
|
|
46
|
+
return target as any;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// 只有特定类型的值可以被观察
|
|
50
|
+
const targetType = getTargetType(target);
|
|
51
|
+
|
|
52
|
+
if (targetType === TargetType.INVALID)
|
|
53
|
+
{
|
|
54
|
+
return target as any;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 如果目标已经有对应的代理对象,直接返回
|
|
58
|
+
const existingProxy = reactiveMap.get(target);
|
|
59
|
+
|
|
60
|
+
if (existingProxy)
|
|
61
|
+
{
|
|
62
|
+
return existingProxy;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// 创建新的代理对象
|
|
66
|
+
const proxy = new Proxy<T>(
|
|
67
|
+
target,
|
|
68
|
+
targetType === TargetType.COLLECTION ? mutableCollectionHandlers : mutableHandlers as any,
|
|
69
|
+
) as T;
|
|
70
|
+
|
|
71
|
+
reactiveMap.set(target, proxy);
|
|
72
|
+
|
|
73
|
+
return proxy as any;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* 响应式对象缓存映射。
|
|
78
|
+
*
|
|
79
|
+
* 用于缓存已创建的响应式代理对象,避免重复创建。
|
|
80
|
+
*/
|
|
81
|
+
export const reactiveMap = new WeakMap<Target, any>();
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* 判断一个对象是否为响应式对象。
|
|
85
|
+
*
|
|
86
|
+
* 通过检查对象是否具有 IS_REACTIVE 标志来判断。
|
|
87
|
+
*
|
|
88
|
+
* @param value 要检查的对象
|
|
89
|
+
* @returns 如果是响应式对象则返回 true,否则返回 false
|
|
90
|
+
*/
|
|
91
|
+
export function isReactive(value: unknown): boolean
|
|
92
|
+
{
|
|
93
|
+
return !!(value && (value as Target)[ReactiveFlags.IS_REACTIVE]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 转换为响应式对象。
|
|
98
|
+
*
|
|
99
|
+
* 如果输入是对象,则将其转换为响应式对象。
|
|
100
|
+
* 如果输入不是对象,则直接返回。
|
|
101
|
+
*
|
|
102
|
+
* @param value 要转换的值
|
|
103
|
+
* @returns 转换后的响应式对象或原值
|
|
104
|
+
*/
|
|
105
|
+
export const toReactive = <T>(value: T): T =>
|
|
106
|
+
{
|
|
107
|
+
if (isObject(value))
|
|
108
|
+
{
|
|
109
|
+
return reactive(value as any) as any;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return value;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 判断一个对象是否为代理对象。
|
|
117
|
+
*
|
|
118
|
+
* 通过检查对象是否具有 RAW 标志来判断。
|
|
119
|
+
*
|
|
120
|
+
* @param value 要检查的对象
|
|
121
|
+
* @returns 如果是代理对象则返回 true,否则返回 false
|
|
122
|
+
*/
|
|
123
|
+
export function isProxy(value: any): boolean
|
|
124
|
+
{
|
|
125
|
+
return value ? !!value[ReactiveFlags.RAW] : false;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* 响应式类型。
|
|
130
|
+
*
|
|
131
|
+
* 表示一个对象的所有属性都是响应式的。
|
|
132
|
+
*/
|
|
133
|
+
export type Reactive<T> = UnReadonly<UnwrapRefSimple<T>>;
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* 原始类型。
|
|
137
|
+
*
|
|
138
|
+
* 包括:字符串、数字、布尔值、大整数、符号、undefined、null。
|
|
139
|
+
*/
|
|
140
|
+
type Primitive = string | number | boolean | bigint | symbol | undefined | null;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 内置类型。
|
|
144
|
+
*
|
|
145
|
+
* 包括:原始类型、函数、日期、错误、正则表达式。
|
|
146
|
+
*/
|
|
147
|
+
export type Builtin = Primitive | Function | Date | Error | RegExp;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* 用于扩展不被解包的类型。
|
|
151
|
+
*
|
|
152
|
+
* 可以通过声明合并来扩展此接口,添加不需要被解包的类型。
|
|
153
|
+
*
|
|
154
|
+
* 示例:
|
|
155
|
+
* ```ts
|
|
156
|
+
* declare module '@vue/reactivity' {
|
|
157
|
+
* export interface RefUnwrapBailTypes {
|
|
158
|
+
* runtimeDOMBailTypes: Node | Window
|
|
159
|
+
* }
|
|
160
|
+
* }
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export interface RefUnwrapBailTypes { }
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 解包类型。
|
|
167
|
+
*
|
|
168
|
+
* 递归地解包响应式对象的类型,包括:
|
|
169
|
+
* 1. 内置类型直接返回
|
|
170
|
+
* 2. Ref 类型解包为内部类型
|
|
171
|
+
* 3. Computed 类型解包为内部类型
|
|
172
|
+
* 4. 对象类型递归解包其属性
|
|
173
|
+
* 5. 其他类型直接返回
|
|
174
|
+
*/
|
|
175
|
+
export type UnwrapRefSimple<T> =
|
|
176
|
+
T extends
|
|
177
|
+
| Builtin
|
|
178
|
+
| Ref
|
|
179
|
+
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
|
|
180
|
+
? T :
|
|
181
|
+
T extends Ref<infer TT> ? TT :
|
|
182
|
+
T extends Computed<infer TT> ? TT :
|
|
183
|
+
T extends object ? {
|
|
184
|
+
[K in keyof T]: UnwrapRefSimple<T[K]>
|
|
185
|
+
} :
|
|
186
|
+
T;
|
package/src/ref.ts
CHANGED
|
@@ -1,88 +1,150 @@
|
|
|
1
|
-
import { batchRun } from './batch';
|
|
2
|
-
import { Reactivity } from './Reactivity';
|
|
3
|
-
import { toReactive } from './reactive';
|
|
4
|
-
import { ReactiveFlags } from './shared/constants';
|
|
5
|
-
import { hasChanged, toRaw } from './shared/general';
|
|
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
|
-
export
|
|
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
|
-
|
|
1
|
+
import { batchRun } from './batch';
|
|
2
|
+
import { Reactivity } from './Reactivity';
|
|
3
|
+
import { toReactive } from './reactive';
|
|
4
|
+
import { ReactiveFlags } from './shared/constants';
|
|
5
|
+
import { hasChanged, toRaw } from './shared/general';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 创建一个引用,该引用的值可以被响应式系统追踪和更新。
|
|
9
|
+
*
|
|
10
|
+
* ref 是响应式系统中最基本的响应式对象。
|
|
11
|
+
* 它可以包装任何类型的值,使其成为响应式的。
|
|
12
|
+
*
|
|
13
|
+
* 特点:
|
|
14
|
+
* 1. 可以包装任何类型的值
|
|
15
|
+
* 2. 通过 .value 访问和修改值
|
|
16
|
+
* 3. 自动解包原始值
|
|
17
|
+
* 4. 支持嵌套的响应式对象
|
|
18
|
+
*
|
|
19
|
+
* @param value 要包装的值
|
|
20
|
+
* @returns 包含 value 属性的响应式引用对象
|
|
21
|
+
*/
|
|
22
|
+
export function ref<T>(value?: T): Ref<T>
|
|
23
|
+
{
|
|
24
|
+
if (isRef(value))
|
|
25
|
+
{
|
|
26
|
+
return value as any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return new RefReactivity<T>(value) as any;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 判断一个对象是否为引用。
|
|
34
|
+
*
|
|
35
|
+
* 通过检查对象是否具有 IS_REF 标志来判断。
|
|
36
|
+
*
|
|
37
|
+
* @param r 要检查的对象
|
|
38
|
+
* @returns 如果是引用则返回 true,否则返回 false
|
|
39
|
+
*/
|
|
40
|
+
export function isRef<T>(r: Ref<T> | unknown): r is Ref<T>;
|
|
41
|
+
export function isRef(r: any): r is Ref
|
|
42
|
+
{
|
|
43
|
+
return r ? r[ReactiveFlags.IS_REF] === true : false;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 引用反应式节点接口。
|
|
48
|
+
*
|
|
49
|
+
* 继承自 Ref 接口,表示这是一个引用反应式节点。
|
|
50
|
+
*/
|
|
51
|
+
export interface RefReactivity<T = any> extends Ref<T> { }
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 引用反应式节点类。
|
|
55
|
+
*
|
|
56
|
+
* 当使用 ref 函数时,会创建一个 RefReactivity 对象。
|
|
57
|
+
* 实现了引用的核心功能:
|
|
58
|
+
* 1. 值的存储和访问
|
|
59
|
+
* 2. 值的自动解包
|
|
60
|
+
* 3. 变更通知
|
|
61
|
+
* 4. 依赖追踪
|
|
62
|
+
*/
|
|
63
|
+
export class RefReactivity<T = any> extends Reactivity<T> implements Ref<T>
|
|
64
|
+
{
|
|
65
|
+
/**
|
|
66
|
+
* 标识这是一个 ref 对象。
|
|
67
|
+
*
|
|
68
|
+
* 用于 isRef 函数判断对象是否为引用。
|
|
69
|
+
*/
|
|
70
|
+
public readonly [ReactiveFlags.IS_REF] = true;
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 获取引用的值。
|
|
74
|
+
*
|
|
75
|
+
* 取值时会:
|
|
76
|
+
* 1. 建立依赖关系
|
|
77
|
+
* 2. 返回当前值
|
|
78
|
+
*/
|
|
79
|
+
get value()
|
|
80
|
+
{
|
|
81
|
+
this.track();
|
|
82
|
+
|
|
83
|
+
return this._value;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 设置引用的值。
|
|
88
|
+
*
|
|
89
|
+
* 设置值时会:
|
|
90
|
+
* 1. 比较新旧值是否发生变化
|
|
91
|
+
* 2. 如果值发生变化,则:
|
|
92
|
+
* - 触发更新通知
|
|
93
|
+
* - 更新原始值
|
|
94
|
+
* - 更新响应式值
|
|
95
|
+
*
|
|
96
|
+
* @param v 要设置的新值
|
|
97
|
+
*/
|
|
98
|
+
set value(v: T)
|
|
99
|
+
{
|
|
100
|
+
const oldValue = this._rawValue;
|
|
101
|
+
const newValue = toRaw(v);
|
|
102
|
+
|
|
103
|
+
if (!hasChanged(oldValue, newValue)) return;
|
|
104
|
+
|
|
105
|
+
batchRun(() =>
|
|
106
|
+
{
|
|
107
|
+
this.trigger();
|
|
108
|
+
|
|
109
|
+
this._rawValue = newValue;
|
|
110
|
+
this._value = toReactive(newValue);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 原始值。
|
|
116
|
+
*
|
|
117
|
+
* 存储未经响应式处理的原始值。
|
|
118
|
+
* 用于比较值是否发生变化。
|
|
119
|
+
*
|
|
120
|
+
* @private
|
|
121
|
+
*/
|
|
122
|
+
private _rawValue: T;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 创建引用反应式节点。
|
|
126
|
+
*
|
|
127
|
+
* @param value 要包装的值
|
|
128
|
+
*/
|
|
129
|
+
constructor(value: T)
|
|
130
|
+
{
|
|
131
|
+
super();
|
|
132
|
+
this._rawValue = toRaw(value);
|
|
133
|
+
this._value = toReactive(value);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 引用接口。
|
|
139
|
+
*
|
|
140
|
+
* 定义了引用的基本结构:
|
|
141
|
+
* 1. value: 引用的值,可读可写
|
|
142
|
+
* 2. RefSymbol: 用于标识这是一个引用
|
|
143
|
+
*/
|
|
144
|
+
export interface Ref<T = any, S = T>
|
|
145
|
+
{
|
|
146
|
+
get value(): T
|
|
147
|
+
set value(_: S)
|
|
148
|
+
[RefSymbol]: true
|
|
149
|
+
}
|
|
150
|
+
declare const RefSymbol: unique symbol;
|
package/src/shared/constants.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 响应式标志枚举,用于标识响应式对象的特殊属性或状态
|
|
3
|
-
*/
|
|
4
|
-
export enum ReactiveFlags
|
|
5
|
-
{
|
|
6
|
-
IS_REACTIVE = '__v_isReactive', // 标识对象是否为响应式对象
|
|
7
|
-
RAW = '__v_raw', // 获取对象的原始非响应式版本
|
|
8
|
-
IS_REF = '__v_isRef', // 标识对象是否为 ref 对象
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export enum TargetType
|
|
12
|
-
{
|
|
13
|
-
INVALID = 0,
|
|
14
|
-
COMMON = 1,
|
|
15
|
-
COLLECTION = 2,
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 跟踪操作类型枚举,用于标识在响应式系统中对对象属性进行的操作类型
|
|
20
|
-
*/
|
|
21
|
-
export enum TrackOpTypes
|
|
22
|
-
{
|
|
23
|
-
GET = 'get', // 获取属性值
|
|
24
|
-
HAS = 'has', // 检查属性是否存在
|
|
25
|
-
ITERATE = 'iterate', // 遍历对象属性
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 触发操作类型枚举,用于标识在响应式系统中对对象属性进行的修改操作类型
|
|
30
|
-
*/
|
|
31
|
-
export enum TriggerOpTypes
|
|
32
|
-
{
|
|
33
|
-
SET = 'set', // 设置属性值
|
|
34
|
-
ADD = 'add', // 添加新属性
|
|
35
|
-
DELETE = 'delete', // 删除属性
|
|
36
|
-
CLEAR = 'clear', // 清空对象属性
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export const ITERATE_KEY: unique symbol = Symbol(__DEV__ ? 'Object iterate' : '');
|
|
40
|
-
export const MAP_KEY_ITERATE_KEY: unique symbol = Symbol(__DEV__ ? 'Map keys iterate' : '');
|
|
41
|
-
export const ARRAY_ITERATE_KEY: unique symbol = Symbol(__DEV__ ? 'Array iterate' : '');
|
|
1
|
+
/**
|
|
2
|
+
* 响应式标志枚举,用于标识响应式对象的特殊属性或状态
|
|
3
|
+
*/
|
|
4
|
+
export enum ReactiveFlags
|
|
5
|
+
{
|
|
6
|
+
IS_REACTIVE = '__v_isReactive', // 标识对象是否为响应式对象
|
|
7
|
+
RAW = '__v_raw', // 获取对象的原始非响应式版本
|
|
8
|
+
IS_REF = '__v_isRef', // 标识对象是否为 ref 对象
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export enum TargetType
|
|
12
|
+
{
|
|
13
|
+
INVALID = 0,
|
|
14
|
+
COMMON = 1,
|
|
15
|
+
COLLECTION = 2,
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 跟踪操作类型枚举,用于标识在响应式系统中对对象属性进行的操作类型
|
|
20
|
+
*/
|
|
21
|
+
export enum TrackOpTypes
|
|
22
|
+
{
|
|
23
|
+
GET = 'get', // 获取属性值
|
|
24
|
+
HAS = 'has', // 检查属性是否存在
|
|
25
|
+
ITERATE = 'iterate', // 遍历对象属性
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 触发操作类型枚举,用于标识在响应式系统中对对象属性进行的修改操作类型
|
|
30
|
+
*/
|
|
31
|
+
export enum TriggerOpTypes
|
|
32
|
+
{
|
|
33
|
+
SET = 'set', // 设置属性值
|
|
34
|
+
ADD = 'add', // 添加新属性
|
|
35
|
+
DELETE = 'delete', // 删除属性
|
|
36
|
+
CLEAR = 'clear', // 清空对象属性
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const ITERATE_KEY: unique symbol = Symbol(__DEV__ ? 'Object iterate' : '');
|
|
40
|
+
export const MAP_KEY_ITERATE_KEY: unique symbol = Symbol(__DEV__ ? 'Map keys iterate' : '');
|
|
41
|
+
export const ARRAY_ITERATE_KEY: unique symbol = Symbol(__DEV__ ? 'Array iterate' : '');
|