@feng3d/reactivity 1.0.5 → 1.0.7

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