@feng3d/reactivity 1.0.5 → 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.
Files changed (101) 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-9zMEdmDL.js +2540 -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-a69uOZEV.js +59 -0
  8. package/dist/assets/src//346/225/260/347/273/204/index.html-CaZ_5kCZ.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/ComputedReactivity.html +72 -0
  19. package/dist/docs/classes/EffectReactivity.html +62 -0
  20. package/dist/docs/classes/EffectScope.html +40 -0
  21. package/dist/docs/classes/Reactivity.html +35 -0
  22. package/dist/docs/classes/RefReactivity.html +57 -0
  23. package/dist/docs/functions/batchRun.html +15 -0
  24. package/dist/docs/functions/computed.html +5 -0
  25. package/dist/docs/functions/effect.html +11 -0
  26. package/dist/docs/functions/effectScope.html +5 -0
  27. package/dist/docs/functions/forceTrack.html +6 -0
  28. package/dist/docs/functions/getCurrentScope.html +4 -0
  29. package/dist/docs/functions/isProxy.html +5 -0
  30. package/dist/docs/functions/isReactive.html +5 -0
  31. package/dist/docs/functions/isRef.html +5 -0
  32. package/dist/docs/functions/noTrack.html +6 -0
  33. package/dist/docs/functions/onScopeDispose.html +6 -0
  34. package/dist/docs/functions/reactive.html +19 -0
  35. package/dist/docs/functions/ref.html +13 -0
  36. package/dist/docs/functions/toRaw.html +4 -0
  37. package/dist/docs/hierarchy.html +1 -0
  38. package/dist/docs/index.html +129 -0
  39. package/dist/docs/interfaces/Computed.html +9 -0
  40. package/dist/docs/interfaces/Effect.html +8 -0
  41. package/dist/docs/interfaces/Ref.html +9 -0
  42. package/dist/docs/modules.html +1 -0
  43. package/dist/docs/types/Reactive.html +3 -0
  44. package/dist/docs/types/UnReadonly.html +3 -0
  45. package/dist/files/RobotoMono-Medium.woff2 +0 -0
  46. package/dist/files/RobotoMono-Regular.woff2 +0 -0
  47. package/dist/files/ic_code_black_24dp.svg +4 -0
  48. package/dist/files/ic_search_black_24dp.svg +4 -0
  49. package/dist/files/main.css +629 -0
  50. package/dist/files/thumbnails.svg +7 -0
  51. package/dist/files.json +7 -0
  52. package/dist/index.html +84 -0
  53. package/dist/index.js +735 -156
  54. package/dist/index.js.map +1 -1
  55. package/dist/index.umd.cjs +735 -156
  56. package/dist/index.umd.cjs.map +1 -1
  57. package/dist/screenshots//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274.jpg +0 -0
  58. package/dist/screenshots//346/225/260/347/273/204.jpg +0 -0
  59. 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
  60. package/dist/src//346/225/260/347/273/204/index.html +65 -0
  61. package/dist/tags.json +2 -0
  62. package/lib/Reactivity.d.ts +47 -13
  63. package/lib/Reactivity.d.ts.map +1 -1
  64. package/lib/arrayInstrumentations.d.ts +10 -0
  65. package/lib/arrayInstrumentations.d.ts.map +1 -1
  66. package/lib/baseHandlers.d.ts +3 -1
  67. package/lib/baseHandlers.d.ts.map +1 -1
  68. package/lib/batch.d.ts +17 -5
  69. package/lib/batch.d.ts.map +1 -1
  70. package/lib/collectionHandlers.d.ts +21 -0
  71. package/lib/collectionHandlers.d.ts.map +1 -1
  72. package/lib/computed.d.ts +75 -27
  73. package/lib/computed.d.ts.map +1 -1
  74. package/lib/effect.d.ts +25 -0
  75. package/lib/effect.d.ts.map +1 -1
  76. package/lib/effectScope.d.ts +129 -0
  77. package/lib/effectScope.d.ts.map +1 -0
  78. package/lib/index.d.ts +6 -5
  79. package/lib/index.d.ts.map +1 -1
  80. package/lib/property.d.ts.map +1 -1
  81. package/lib/reactive.d.ts +58 -17
  82. package/lib/reactive.d.ts.map +1 -1
  83. package/lib/ref.d.ts +66 -4
  84. package/lib/ref.d.ts.map +1 -1
  85. package/lib/shared/constants.d.ts +7 -7
  86. package/lib/shared/constants.d.ts.map +1 -1
  87. package/lib/shared/general.d.ts +1 -1
  88. package/lib/shared/general.d.ts.map +1 -1
  89. package/package.json +21 -16
  90. package/src/Reactivity.ts +57 -15
  91. package/src/arrayInstrumentations.ts +406 -53
  92. package/src/baseHandlers.ts +124 -32
  93. package/src/batch.ts +38 -11
  94. package/src/collectionHandlers.ts +207 -19
  95. package/src/computed.ts +92 -43
  96. package/src/effect.ts +38 -0
  97. package/src/effectScope.ts +294 -0
  98. package/src/index.ts +6 -5
  99. package/src/property.ts +6 -0
  100. package/src/reactive.ts +67 -20
  101. package/src/ref.ts +66 -4
@@ -3,10 +3,26 @@ import { ITERATE_KEY, MAP_KEY_ITERATE_KEY, ReactiveFlags, TrackOpTypes, TriggerO
3
3
  import { hasChanged, hasOwn, isMap, Target, toRaw, toRawType, warn } from './shared/general';
4
4
  import { PropertyReactivity } from './property';
5
5
 
6
+ /**
7
+ * 可变集合响应式处理器。
8
+ *
9
+ * 用于处理集合类型(Map、Set、WeakMap、WeakSet)的响应式代理。
10
+ * 通过拦截集合的操作方法,实现响应式功能。
11
+ */
6
12
  export const mutableCollectionHandlers: ProxyHandler<CollectionTypes> = {
7
13
  get: createInstrumentationGetter(),
8
14
  };
9
15
 
16
+ /**
17
+ * 创建集合方法的拦截器。
18
+ *
19
+ * 返回一个 get 拦截器函数,用于:
20
+ * 1. 处理响应式标识和原始对象获取
21
+ * 2. 拦截集合的操作方法
22
+ * 3. 转发其他属性访问
23
+ *
24
+ * @returns 集合方法的拦截器函数
25
+ */
10
26
  function createInstrumentationGetter()
11
27
  {
12
28
  const instrumentations = createInstrumentations();
@@ -17,15 +33,18 @@ function createInstrumentationGetter()
17
33
  receiver: CollectionTypes,
18
34
  ) =>
19
35
  {
36
+ // 处理响应式标识
20
37
  if (key === ReactiveFlags.IS_REACTIVE)
21
38
  {
22
39
  return true;
23
40
  }
41
+ // 获取原始对象
24
42
  else if (key === ReactiveFlags.RAW)
25
43
  {
26
44
  return target;
27
45
  }
28
46
 
47
+ // 如果方法在增强对象中存在,则使用增强版本
29
48
  return Reflect.get(
30
49
  hasOwn(instrumentations, key) && key in target
31
50
  ? instrumentations
@@ -36,19 +55,45 @@ function createInstrumentationGetter()
36
55
  };
37
56
  }
38
57
 
58
+ /**
59
+ * 集合方法增强类型。
60
+ *
61
+ * 定义了所有需要增强的集合方法的类型。
62
+ */
39
63
  type Instrumentations = Record<string | symbol, Function | number>;
40
64
 
65
+ /**
66
+ * 创建集合方法的增强实现。
67
+ *
68
+ * 为集合类型(Map、Set、WeakMap、WeakSet)创建响应式增强方法:
69
+ * 1. 基本操作:get、set、has、delete、clear
70
+ * 2. 遍历操作:forEach、keys、values、entries
71
+ * 3. 大小获取:size
72
+ *
73
+ * @returns 增强后的集合方法对象
74
+ */
41
75
  function createInstrumentations(): Instrumentations
42
76
  {
43
77
  const instrumentations: Instrumentations = {
78
+ /**
79
+ * 获取 Map 中的值。
80
+ *
81
+ * 实现了以下功能:
82
+ * 1. 支持原始键和响应式键的查找
83
+ * 2. 自动追踪键的访问
84
+ * 3. 自动将返回值转换为响应式
85
+ *
86
+ * @param key 要查找的键
87
+ * @returns 找到的值,如果不存在则返回 undefined
88
+ */
44
89
  get(this: MapTypes, key: unknown)
45
90
  {
46
- // #1772: readonly(reactive(Map)) should return readonly + reactive version
47
- // of the value
91
+ // #1772: readonly(reactive(Map)) 应该返回只读的响应式值
48
92
  const target = this[ReactiveFlags.RAW];
49
93
  const rawTarget = toRaw(target);
50
94
  const rawKey = toRaw(key);
51
95
 
96
+ // 追踪键的访问
52
97
  if (hasChanged(key, rawKey))
53
98
  {
54
99
  PropertyReactivity.track(rawTarget, TrackOpTypes.GET, key);
@@ -57,6 +102,7 @@ function createInstrumentations(): Instrumentations
57
102
 
58
103
  const { has } = getProto(rawTarget);
59
104
  const wrap = toReactive;
105
+
60
106
  if (has.call(rawTarget, key))
61
107
  {
62
108
  return wrap(target.get(key));
@@ -68,23 +114,44 @@ function createInstrumentations(): Instrumentations
68
114
  else if (target !== rawTarget)
69
115
  {
70
116
  // #3602 readonly(reactive(Map))
71
- // ensure that the nested reactive `Map` can do tracking for itself
117
+ // 确保嵌套的响应式 Map 可以追踪自身
72
118
  target.get(key);
73
119
  }
74
120
  },
121
+
122
+ /**
123
+ * 获取集合的大小。
124
+ *
125
+ * 实现了以下功能:
126
+ * 1. 追踪集合大小的访问
127
+ * 2. 返回集合的实际大小
128
+ */
75
129
  get size()
76
130
  {
77
131
  const target = (this as unknown as IterableCollections)[ReactiveFlags.RAW];
132
+
78
133
  PropertyReactivity.track(toRaw(target), TrackOpTypes.ITERATE, ITERATE_KEY);
79
134
 
80
135
  return Reflect.get(target, 'size', target);
81
136
  },
137
+
138
+ /**
139
+ * 检查集合是否包含某个值。
140
+ *
141
+ * 实现了以下功能:
142
+ * 1. 支持原始键和响应式键的检查
143
+ * 2. 自动追踪键的检查
144
+ *
145
+ * @param key 要检查的键
146
+ * @returns 如果集合包含该键则返回 true,否则返回 false
147
+ */
82
148
  has(this: CollectionTypes, key: unknown): boolean
83
149
  {
84
150
  const target = this[ReactiveFlags.RAW];
85
151
  const rawTarget = toRaw(target);
86
152
  const rawKey = toRaw(key);
87
153
 
154
+ // 追踪键的检查
88
155
  if (hasChanged(key, rawKey))
89
156
  {
90
157
  PropertyReactivity.track(rawTarget, TrackOpTypes.HAS, key);
@@ -95,29 +162,53 @@ function createInstrumentations(): Instrumentations
95
162
  ? target.has(key)
96
163
  : target.has(key) || target.has(rawKey);
97
164
  },
165
+
166
+ /**
167
+ * 遍历集合中的所有元素。
168
+ *
169
+ * 实现了以下功能:
170
+ * 1. 追踪集合的遍历操作
171
+ * 2. 自动将遍历的值转换为响应式
172
+ * 3. 保持回调函数的 this 上下文
173
+ *
174
+ * @param callback 遍历回调函数
175
+ * @param thisArg 回调函数的 this 值
176
+ */
98
177
  forEach(this: IterableCollections, callback: Function, thisArg?: unknown)
99
178
  {
100
179
  const observed = this;
101
180
  const target = observed[ReactiveFlags.RAW];
102
181
  const rawTarget = toRaw(target);
103
182
  const wrap = toReactive;
183
+
104
184
  PropertyReactivity.track(rawTarget, TrackOpTypes.ITERATE, ITERATE_KEY);
105
185
 
106
186
  return target.forEach((value: unknown, key: unknown) =>
107
-
108
- // important: make sure the callback is
109
- // 1. invoked with the reactive map as `this` and 3rd arg
110
- // 2. the value received should be a corresponding reactive/readonly.
111
- callback.call(thisArg, wrap(value), wrap(key), observed)
187
+ // 重要:确保回调函数
188
+ // 1. 使用响应式 map 作为 this 和第三个参数
189
+ // 2. 接收到的值应该是相应的响应式/只读版本
190
+ callback.call(thisArg, wrap(value), wrap(key), observed),
112
191
  );
113
192
  },
114
193
 
194
+ /**
195
+ * 向 Set 中添加值。
196
+ *
197
+ * 实现了以下功能:
198
+ * 1. 自动将值转换为原始值
199
+ * 2. 避免重复添加
200
+ * 3. 触发添加操作的通知
201
+ *
202
+ * @param value 要添加的值
203
+ * @returns 集合本身,支持链式调用
204
+ */
115
205
  add(this: SetTypes, value: unknown)
116
206
  {
117
207
  value = toRaw(value);
118
208
  const target = toRaw(this);
119
209
  const proto = getProto(target);
120
210
  const hadKey = proto.has.call(target, value);
211
+
121
212
  if (!hadKey)
122
213
  {
123
214
  target.add(value);
@@ -126,6 +217,19 @@ function createInstrumentations(): Instrumentations
126
217
 
127
218
  return this;
128
219
  },
220
+
221
+ /**
222
+ * 设置 Map 中的值。
223
+ *
224
+ * 实现了以下功能:
225
+ * 1. 自动将值转换为原始值
226
+ * 2. 支持原始键和响应式键的设置
227
+ * 3. 触发添加或修改操作的通知
228
+ *
229
+ * @param key 要设置的键
230
+ * @param value 要设置的值
231
+ * @returns 集合本身,支持链式调用
232
+ */
129
233
  set(this: MapTypes, key: unknown, value: unknown)
130
234
  {
131
235
  value = toRaw(value);
@@ -133,6 +237,7 @@ function createInstrumentations(): Instrumentations
133
237
  const { has, get } = getProto(target);
134
238
 
135
239
  let hadKey = has.call(target, key);
240
+
136
241
  if (!hadKey)
137
242
  {
138
243
  key = toRaw(key);
@@ -144,6 +249,7 @@ function createInstrumentations(): Instrumentations
144
249
  }
145
250
 
146
251
  const oldValue = get.call(target, key);
252
+
147
253
  target.set(key, value);
148
254
  if (!hadKey)
149
255
  {
@@ -156,11 +262,23 @@ function createInstrumentations(): Instrumentations
156
262
 
157
263
  return this;
158
264
  },
265
+
266
+ /**
267
+ * 从集合中删除值。
268
+ *
269
+ * 实现了以下功能:
270
+ * 1. 支持原始键和响应式键的删除
271
+ * 2. 触发删除操作的通知
272
+ *
273
+ * @param key 要删除的键
274
+ * @returns 如果删除成功则返回 true,否则返回 false
275
+ */
159
276
  delete(this: CollectionTypes, key: unknown)
160
277
  {
161
278
  const target = toRaw(this);
162
279
  const { has, get } = getProto(target);
163
280
  let hadKey = has.call(target, key);
281
+
164
282
  if (!hadKey)
165
283
  {
166
284
  key = toRaw(key);
@@ -172,8 +290,9 @@ function createInstrumentations(): Instrumentations
172
290
  }
173
291
 
174
292
  const oldValue = get ? get.call(target, key) : undefined;
175
- // forward the operation before queueing reactions
293
+ // 在触发反应之前执行操作
176
294
  const result = target.delete(key);
295
+
177
296
  if (hadKey)
178
297
  {
179
298
  PropertyReactivity.trigger(target, TriggerOpTypes.DELETE, key, undefined, oldValue);
@@ -181,6 +300,17 @@ function createInstrumentations(): Instrumentations
181
300
 
182
301
  return result;
183
302
  },
303
+
304
+ /**
305
+ * 清空集合。
306
+ *
307
+ * 实现了以下功能:
308
+ * 1. 清空集合中的所有元素
309
+ * 2. 触发清空操作的通知
310
+ * 3. 在开发模式下保存旧值用于调试
311
+ *
312
+ * @returns 如果清空成功则返回 true,否则返回 false
313
+ */
184
314
  clear(this: IterableCollections)
185
315
  {
186
316
  const target = toRaw(this);
@@ -190,8 +320,9 @@ function createInstrumentations(): Instrumentations
190
320
  ? new Map(target)
191
321
  : new Set(target)
192
322
  : undefined;
193
- // forward the operation before queueing reactions
323
+ // 在触发反应之前执行操作
194
324
  const result = target.clear();
325
+
195
326
  if (hadItems)
196
327
  {
197
328
  PropertyReactivity.trigger(
@@ -207,6 +338,7 @@ function createInstrumentations(): Instrumentations
207
338
  },
208
339
  };
209
340
 
341
+ // 添加迭代器方法
210
342
  const iteratorMethods = [
211
343
  'keys',
212
344
  'values',
@@ -222,6 +354,18 @@ function createInstrumentations(): Instrumentations
222
354
  return instrumentations;
223
355
  }
224
356
 
357
+ /**
358
+ * 创建迭代器方法。
359
+ *
360
+ * 为集合创建响应式的迭代器方法,包括:
361
+ * 1. keys() - 返回键的迭代器
362
+ * 2. values() - 返回值的迭代器
363
+ * 3. entries() - 返回键值对的迭代器
364
+ * 4. [Symbol.iterator] - 返回默认迭代器
365
+ *
366
+ * @param method 迭代器方法名
367
+ * @returns 增强后的迭代器方法
368
+ */
225
369
  function createIterableMethod(method: string | symbol)
226
370
  {
227
371
  return function (
@@ -238,16 +382,16 @@ function createIterableMethod(method: string | symbol)
238
382
  const innerIterator = target[method](...args);
239
383
  const wrap = toReactive;
240
384
 
385
+ // 追踪迭代操作
241
386
  PropertyReactivity.track(
242
387
  rawTarget,
243
388
  TrackOpTypes.ITERATE,
244
389
  isKeyOnly ? MAP_KEY_ITERATE_KEY : ITERATE_KEY,
245
390
  );
246
391
 
247
- // return a wrapped iterator which returns observed versions of the
248
- // values emitted from the real iterator
392
+ // 返回一个包装的迭代器,它会返回响应式版本的值
249
393
  return {
250
- // iterator protocol
394
+ // 迭代器协议
251
395
  next()
252
396
  {
253
397
  const { value, done } = innerIterator.next();
@@ -259,7 +403,7 @@ function createIterableMethod(method: string | symbol)
259
403
  done,
260
404
  };
261
405
  },
262
- // iterable protocol
406
+ // 可迭代协议
263
407
  [Symbol.iterator]()
264
408
  {
265
409
  return this;
@@ -268,6 +412,15 @@ function createIterableMethod(method: string | symbol)
268
412
  };
269
413
  }
270
414
 
415
+ /**
416
+ * 检查键的身份。
417
+ *
418
+ * 在开发模式下检查键的身份,确保不会出现重复的键。
419
+ *
420
+ * @param target 目标集合
421
+ * @param has 检查方法
422
+ * @param key 要检查的键
423
+ */
271
424
  function checkIdentityKeys(
272
425
  target: CollectionTypes,
273
426
  has: (key: unknown) => boolean,
@@ -275,24 +428,59 @@ function checkIdentityKeys(
275
428
  )
276
429
  {
277
430
  const rawKey = toRaw(key);
431
+
278
432
  if (rawKey !== key && has.call(target, rawKey))
279
433
  {
280
434
  const type = toRawType(target);
435
+
281
436
  warn(
282
- `Reactive ${type} contains both the raw and reactive `
283
- + `versions of the same object${type === `Map` ? ` as keys` : ``}, `
284
- + `which can lead to inconsistencies. `
285
- + `Avoid differentiating between the raw and reactive versions `
286
- + `of an object and only use the reactive version if possible.`,
437
+ `Reactive ${type} contains both the raw and reactive ` +
438
+ `versions of the same object${type === `Map` ? ` as keys` : ``}, ` +
439
+ `which can lead to inconsistencies. ` +
440
+ `It is recommended to use only the reactive version.`,
287
441
  );
288
442
  }
289
443
  }
290
444
 
445
+ /**
446
+ * 获取对象的原型。
447
+ *
448
+ * @param v 目标对象
449
+ * @returns 对象的原型
450
+ */
291
451
  const getProto = <T extends CollectionTypes>(v: T): any => Reflect.getPrototypeOf(v);
292
452
 
453
+ /**
454
+ * 集合类型。
455
+ *
456
+ * 包括可迭代集合和弱引用集合。
457
+ */
293
458
  type CollectionTypes = IterableCollections | WeakCollections;
294
459
 
460
+ /**
461
+ * 可迭代集合类型。
462
+ *
463
+ * 包括 Map 和 Set。
464
+ */
295
465
  type IterableCollections = (Map<any, any> | Set<any>) & Target;
466
+
467
+ /**
468
+ * 弱引用集合类型。
469
+ *
470
+ * 包括 WeakMap 和 WeakSet。
471
+ */
296
472
  type WeakCollections = (WeakMap<any, any> | WeakSet<any>) & Target;
473
+
474
+ /**
475
+ * Map 类型。
476
+ *
477
+ * 包括 Map 和 WeakMap。
478
+ */
297
479
  type MapTypes = (Map<any, any> | WeakMap<any, any>) & Target;
480
+
481
+ /**
482
+ * Set 类型。
483
+ *
484
+ * 包括 Set 和 WeakSet。
485
+ */
298
486
  type SetTypes = (Set<any> | WeakSet<any>) & Target;