@feng3d/reactivity 1.0.9 → 1.0.12

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.
@@ -1,10 +1,29 @@
1
1
  (function(global, factory) {
2
2
  typeof exports === "object" && typeof module !== "undefined" ? factory(exports) : typeof define === "function" && define.amd ? define(["exports"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.feng3d = {}));
3
3
  })(this, (function(exports2) {
4
- "use strict";
5
- class Reactivity {
4
+ "use strict";var __defProp = Object.defineProperty;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
+
8
+ var _a, _b;
9
+ const _Reactivity = class _Reactivity {
6
10
  constructor() {
7
- this._parents = /* @__PURE__ */ new Map();
11
+ /**
12
+ * @private
13
+ */
14
+ __publicField(this, "_value");
15
+ /**
16
+ * 父反应节点集合。
17
+ *
18
+ * 记录了哪些节点依赖了当前节点。
19
+ * 当当前节点值发生变化时,会通知所有父节点。
20
+ *
21
+ * Map 的 key 是父节点,value 是父节点的版本号。
22
+ * 版本号用于判断依赖关系是否过期。
23
+ *
24
+ * @private
25
+ */
26
+ __publicField(this, "_parents", /* @__PURE__ */ new Map());
8
27
  }
9
28
  /**
10
29
  * 获取当前节点值。
@@ -25,8 +44,8 @@
25
44
  * 如果当前没有活动的响应式节点,或者不应该跟踪依赖,则不会建立依赖关系。
26
45
  */
27
46
  track() {
28
- if (!Reactivity.activeReactivity || !_shouldTrack) return;
29
- const parent = Reactivity.activeReactivity;
47
+ if (!_Reactivity.activeReactivity || !_shouldTrack) return;
48
+ const parent = _Reactivity.activeReactivity;
30
49
  if (parent) {
31
50
  this._parents.set(parent, parent._version);
32
51
  }
@@ -51,7 +70,17 @@
51
70
  });
52
71
  this._parents.clear();
53
72
  }
54
- }
73
+ };
74
+ /**
75
+ * 当前正在执行的反应式节点。
76
+ *
77
+ * 用于在依赖收集过程中标识当前正在执行的节点。
78
+ * 当其他节点访问此节点的值时,会将其作为父节点。
79
+ *
80
+ * @internal
81
+ */
82
+ __publicField(_Reactivity, "activeReactivity");
83
+ let Reactivity = _Reactivity;
55
84
  function forceTrack(fn) {
56
85
  const preShouldTrack = _shouldTrack;
57
86
  _shouldTrack = true;
@@ -88,6 +117,7 @@
88
117
  }
89
118
  if (_isRunedDeps.length > 0) {
90
119
  _isRunedDeps.forEach((dep) => {
120
+ __DEV__ && console.assert(dep._isDirty === false, "dep.dirty === false");
91
121
  dep._children.forEach((version, node) => {
92
122
  node._parents.set(dep, dep._version);
93
123
  });
@@ -120,10 +150,48 @@
120
150
  */
121
151
  constructor(func) {
122
152
  super();
123
- this.__v_isRef = true;
124
- this._children = /* @__PURE__ */ new Map();
125
- this._isDirty = true;
126
- this._version = -1;
153
+ /**
154
+ * 标识这是一个 ref 对象。
155
+ *
156
+ * @internal
157
+ */
158
+ __publicField(this, "__v_isRef", true);
159
+ /**
160
+ * 计算函数。
161
+ *
162
+ * 用于计算属性值的函数,可以访问其他响应式数据。
163
+ * 当依赖发生变化时,会重新执行此函数。
164
+ */
165
+ __publicField(this, "_func");
166
+ /**
167
+ * 失效子节点集合。
168
+ *
169
+ * 记录所有依赖此计算属性的子节点。
170
+ * 当计算属性重新计算时,会通知这些子节点。
171
+ *
172
+ * @private
173
+ */
174
+ __publicField(this, "_children", /* @__PURE__ */ new Map());
175
+ /**
176
+ * 脏标记。
177
+ *
178
+ * 表示计算属性是否需要重新计算。
179
+ * 当依赖发生变化时,会设置此标记。
180
+ * 重新计算后会清除此标记。
181
+ *
182
+ * @private
183
+ */
184
+ __publicField(this, "_isDirty", true);
185
+ /**
186
+ * 版本号。
187
+ *
188
+ * 每次重新计算后自动递增。
189
+ * 用于判断子节点中的父节点引用是否过期。
190
+ * 当子节点发现父节点的版本号不匹配时,会重新建立依赖关系。
191
+ *
192
+ * @private
193
+ */
194
+ __publicField(this, "_version", -1);
127
195
  this._func = func;
128
196
  }
129
197
  /**
@@ -248,9 +316,9 @@
248
316
  TriggerOpTypes2["CLEAR"] = "clear";
249
317
  return TriggerOpTypes2;
250
318
  })(TriggerOpTypes || {});
251
- const ITERATE_KEY = Symbol("");
252
- const MAP_KEY_ITERATE_KEY = Symbol("");
253
- const ARRAY_ITERATE_KEY = Symbol("");
319
+ const ITERATE_KEY = Symbol(__DEV__ ? "Object iterate" : "");
320
+ const MAP_KEY_ITERATE_KEY = Symbol(__DEV__ ? "Map keys iterate" : "");
321
+ const ARRAY_ITERATE_KEY = Symbol(__DEV__ ? "Array iterate" : "");
254
322
  globalThis.__DEV__ ?? (globalThis.__DEV__ = true);
255
323
  const isObject = (val) => val !== null && typeof val === "object";
256
324
  const isArray = Array.isArray;
@@ -287,6 +355,9 @@
287
355
  const raw = observed && observed[ReactiveFlags.RAW];
288
356
  return raw ? toRaw(raw) : observed;
289
357
  }
358
+ function warn(msg, ...args) {
359
+ console.warn(`[警告] ${msg}`, ...args);
360
+ }
290
361
  // @__NO_SIDE_EFFECTS__
291
362
  function makeMap(str) {
292
363
  const map = /* @__PURE__ */ Object.create(null);
@@ -300,12 +371,50 @@
300
371
  * @param detached 是否创建分离的作用域
301
372
  */
302
373
  constructor(detached = false) {
374
+ /**
375
+ * 作用域是否处于活动状态
376
+ * @internal
377
+ */
378
+ __publicField(this, "_active", true);
379
+ /**
380
+ * 跟踪 on 方法的调用次数,允许多次调用 on 方法
381
+ * @internal
382
+ */
383
+ __publicField(this, "_on", 0);
384
+ /**
385
+ * 存储当前作用域中的所有效果
386
+ * @internal
387
+ */
388
+ __publicField(this, "effects", []);
389
+ /**
390
+ * 存储清理函数
391
+ * @internal
392
+ */
393
+ __publicField(this, "cleanups", []);
394
+ /**
395
+ * 作用域是否被暂停
396
+ */
397
+ __publicField(this, "_isPaused", false);
398
+ /**
399
+ * 父作用域,仅由非分离的作用域分配
400
+ * @internal
401
+ */
402
+ __publicField(this, "parent");
403
+ /**
404
+ * 记录未分离的子作用域
405
+ * @internal
406
+ */
407
+ __publicField(this, "scopes");
408
+ /**
409
+ * 在父作用域的 scopes 数组中记录子作用域的索引,用于优化移除操作
410
+ * @internal
411
+ */
412
+ __publicField(this, "index");
413
+ /**
414
+ * 前一个作用域
415
+ */
416
+ __publicField(this, "prevScope");
303
417
  this.detached = detached;
304
- this._active = true;
305
- this._on = 0;
306
- this.effects = [];
307
- this.cleanups = [];
308
- this._isPaused = false;
309
418
  this.parent = activeEffectScope;
310
419
  if (!detached && activeEffectScope) {
311
420
  this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
@@ -373,6 +482,8 @@
373
482
  } finally {
374
483
  activeEffectScope = currentEffectScope;
375
484
  }
485
+ } else if (__DEV__) {
486
+ warn(`无法运行已停用的 effect 作用域。`);
376
487
  }
377
488
  }
378
489
  /**
@@ -441,6 +552,10 @@
441
552
  function onScopeDispose(fn, failSilently = false) {
442
553
  if (activeEffectScope) {
443
554
  activeEffectScope.cleanups.push(fn);
555
+ } else if (__DEV__ && !failSilently) {
556
+ warn(
557
+ `onScopeDispose() 在没有活动的 effect 作用域时被调用,无法关联。`
558
+ );
444
559
  }
445
560
  }
446
561
  function effect(fn) {
@@ -449,7 +564,12 @@
449
564
  const _EffectReactivity = class _EffectReactivity extends ComputedReactivity {
450
565
  constructor(func) {
451
566
  super(func);
452
- this._isEnable = true;
567
+ /**
568
+ * 是否为启用, 默认为 true。
569
+ *
570
+ * 启用时,会立即执行函数。
571
+ */
572
+ __publicField(this, "_isEnable", true);
453
573
  if (activeEffectScope && activeEffectScope.active) {
454
574
  activeEffectScope.effects.push(this);
455
575
  }
@@ -513,7 +633,7 @@
513
633
  }
514
634
  }
515
635
  };
516
- _EffectReactivity.pausedQueueEffects = /* @__PURE__ */ new WeakSet();
636
+ __publicField(_EffectReactivity, "pausedQueueEffects", /* @__PURE__ */ new WeakSet());
517
637
  let EffectReactivity = _EffectReactivity;
518
638
  function property(target, key) {
519
639
  let depsMap = PropertyReactivity._targetMap.get(target);
@@ -528,7 +648,21 @@
528
648
  }
529
649
  return dep;
530
650
  }
531
- const _PropertyReactivity = class _PropertyReactivity extends Reactivity {
651
+ class PropertyReactivity extends Reactivity {
652
+ constructor(target, key) {
653
+ super();
654
+ __publicField(this, "_target");
655
+ __publicField(this, "_key");
656
+ this._target = target;
657
+ this._key = key;
658
+ if (target instanceof Map || target instanceof WeakMap) {
659
+ this._value = target.get(key);
660
+ } else if (target instanceof Set || target instanceof WeakSet) {
661
+ this._value = target.has(key);
662
+ } else {
663
+ this._value = target[key];
664
+ }
665
+ }
532
666
  /**
533
667
  * 获取当前节点值。
534
668
  *
@@ -548,18 +682,6 @@
548
682
  this.trigger();
549
683
  this._value = v;
550
684
  }
551
- constructor(target, key) {
552
- super();
553
- this._target = target;
554
- this._key = key;
555
- if (target instanceof Map || target instanceof WeakMap) {
556
- this._value = target.get(key);
557
- } else if (target instanceof Set || target instanceof WeakSet) {
558
- this._value = target.has(key);
559
- } else {
560
- this._value = target[key];
561
- }
562
- }
563
685
  triggerIfChanged() {
564
686
  }
565
687
  /**
@@ -643,9 +765,11 @@
643
765
  }
644
766
  });
645
767
  }
646
- };
647
- _PropertyReactivity._targetMap = /* @__PURE__ */ new WeakMap();
648
- let PropertyReactivity = _PropertyReactivity;
768
+ }
769
+ /**
770
+ * @private
771
+ */
772
+ __publicField(PropertyReactivity, "_targetMap", /* @__PURE__ */ new WeakMap());
649
773
  const arrayInstrumentations = {
650
774
  __proto__: null,
651
775
  /**
@@ -1126,7 +1250,6 @@
1126
1250
  );
1127
1251
  return res;
1128
1252
  }
1129
- var _a, _b;
1130
1253
  function ref(value) {
1131
1254
  if (isRef(value)) {
1132
1255
  return value;
@@ -1144,7 +1267,21 @@
1144
1267
  */
1145
1268
  constructor(value) {
1146
1269
  super();
1147
- this[_a] = true;
1270
+ /**
1271
+ * 标识这是一个 ref 对象。
1272
+ *
1273
+ * 用于 isRef 函数判断对象是否为引用。
1274
+ */
1275
+ __publicField(this, _a, true);
1276
+ /**
1277
+ * 原始值。
1278
+ *
1279
+ * 存储未经响应式处理的原始值。
1280
+ * 用于比较值是否发生变化。
1281
+ *
1282
+ * @private
1283
+ */
1284
+ __publicField(this, "_rawValue");
1148
1285
  this._rawValue = toRaw(value);
1149
1286
  this._value = toReactive(value);
1150
1287
  }
@@ -1265,6 +1402,7 @@
1265
1402
  value,
1266
1403
  isRef(target) ? target : receiver
1267
1404
  );
1405
+ __DEV__ && console.assert(target === toRaw(receiver));
1268
1406
  if (target === toRaw(receiver)) {
1269
1407
  if (!hadKey) {
1270
1408
  PropertyReactivity.trigger(target, TriggerOpTypes.ADD, key, value);
@@ -1491,6 +1629,8 @@
1491
1629
  if (!hadKey) {
1492
1630
  key = toRaw(key);
1493
1631
  hadKey = has.call(target, key);
1632
+ } else if (__DEV__) {
1633
+ checkIdentityKeys(target, has, key);
1494
1634
  }
1495
1635
  const oldValue = get.call(target, key);
1496
1636
  target.set(key, value);
@@ -1518,6 +1658,8 @@
1518
1658
  if (!hadKey) {
1519
1659
  key = toRaw(key);
1520
1660
  hadKey = has.call(target, key);
1661
+ } else if (__DEV__) {
1662
+ checkIdentityKeys(target, has, key);
1521
1663
  }
1522
1664
  const oldValue = get ? get.call(target, key) : void 0;
1523
1665
  const result = target.delete(key);
@@ -1539,7 +1681,7 @@
1539
1681
  clear() {
1540
1682
  const target = toRaw(this);
1541
1683
  const hadItems = target.size !== 0;
1542
- const oldTarget = void 0;
1684
+ const oldTarget = __DEV__ ? isMap(target) ? new Map(target) : new Set(target) : void 0;
1543
1685
  const result = target.clear();
1544
1686
  if (hadItems) {
1545
1687
  PropertyReactivity.trigger(
@@ -1594,6 +1736,15 @@
1594
1736
  };
1595
1737
  };
1596
1738
  }
1739
+ function checkIdentityKeys(target, has, key) {
1740
+ const rawKey = toRaw(key);
1741
+ if (rawKey !== key && has.call(target, rawKey)) {
1742
+ const type = toRawType(target);
1743
+ warn(
1744
+ `响应式 ${type} 同时包含同一对象的原始版本和响应式版本${type === `Map` ? `作为键` : ``},这可能导致不一致。建议仅使用响应式版本。`
1745
+ );
1746
+ }
1747
+ }
1597
1748
  const getProto = (v) => Reflect.getPrototypeOf(v);
1598
1749
  function reactive(target) {
1599
1750
  if (!isObject(target)) {
@@ -1632,8 +1783,21 @@
1632
1783
  }
1633
1784
  class ReactiveObject {
1634
1785
  constructor() {
1635
- this._effectScope = new EffectScope();
1636
- this._destroyCallbacks = [];
1786
+ /**
1787
+ * 副作用作用域
1788
+ *
1789
+ * 用于管理所有副作用的生命周期:
1790
+ * - 收集所有通过 effect() 方法创建的副作用
1791
+ * - 在类销毁时自动停止所有副作用
1792
+ * - 防止副作用在类销毁后继续执行,避免内存泄漏
1793
+ *
1794
+ * 私有属性,外部无法直接访问,只能通过 effect() 方法使用
1795
+ */
1796
+ __publicField(this, "_effectScope", new EffectScope());
1797
+ /**
1798
+ * 销毁时需要执行的函数
1799
+ */
1800
+ __publicField(this, "_destroyCallbacks", []);
1637
1801
  }
1638
1802
  /**
1639
1803
  * 创建并运行副作用
@@ -1728,4 +1892,5 @@
1728
1892
  exports2.toRaw = toRaw;
1729
1893
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
1730
1894
  }));
1895
+ console.log("@feng3d/reactivity v1.0.12");
1731
1896
  //# sourceMappingURL=index.umd.cjs.map