@feng3d/reactivity 1.0.7 → 1.0.8

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 (81) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +158 -158
  3. package/dist/index.js +89 -0
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.umd.cjs +91 -2
  6. package/dist/index.umd.cjs.map +1 -1
  7. package/lib/ReactiveObject.d.ts +101 -0
  8. package/lib/ReactiveObject.d.ts.map +1 -0
  9. package/lib/batch.d.ts.map +1 -1
  10. package/lib/index.d.ts +1 -0
  11. package/lib/index.d.ts.map +1 -1
  12. package/package.json +69 -69
  13. package/src/ReactiveObject.ts +131 -0
  14. package/src/Reactivity.ts +168 -168
  15. package/src/arrayInstrumentations.ts +801 -801
  16. package/src/baseHandlers.ts +312 -312
  17. package/src/batch.ts +134 -118
  18. package/src/collectionHandlers.ts +486 -486
  19. package/src/computed.ts +253 -253
  20. package/src/effect.ts +146 -146
  21. package/src/effectScope.ts +294 -294
  22. package/src/index.ts +10 -9
  23. package/src/property.ts +231 -231
  24. package/src/reactive.ts +186 -186
  25. package/src/ref.ts +150 -150
  26. package/src/shared/constants.ts +41 -41
  27. package/src/shared/general.ts +109 -109
  28. package/tsconfig.json +19 -19
  29. package/dist/assets/RobotoMono-Medium-DVgDz_OO.woff2 +0 -0
  30. package/dist/assets/RobotoMono-Regular-BPoF81uy.woff2 +0 -0
  31. package/dist/assets/index-a2qCSG5V.css +0 -629
  32. package/dist/assets/index.html-Dyp3udP2.js +0 -200
  33. package/dist/assets/modulepreload-polyfill-DaKOjhqt.js +0 -37
  34. package/dist/assets/package-DuJynByc.js +0 -2539
  35. 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 +0 -59
  36. package/dist/assets/src//346/225/260/347/273/204/index.html-CHK6WEhd.js +0 -43
  37. package/dist/docs/.nojekyll +0 -1
  38. package/dist/docs/assets/hierarchy.js +0 -1
  39. package/dist/docs/assets/highlight.css +0 -92
  40. package/dist/docs/assets/icons.js +0 -18
  41. package/dist/docs/assets/icons.svg +0 -1
  42. package/dist/docs/assets/main.js +0 -60
  43. package/dist/docs/assets/navigation.js +0 -1
  44. package/dist/docs/assets/search.js +0 -1
  45. package/dist/docs/assets/style.css +0 -1640
  46. package/dist/docs/classes/EffectScope.html +0 -40
  47. package/dist/docs/functions/batchRun.html +0 -15
  48. package/dist/docs/functions/computed.html +0 -5
  49. package/dist/docs/functions/effect.html +0 -11
  50. package/dist/docs/functions/effectScope.html +0 -5
  51. package/dist/docs/functions/forceTrack.html +0 -6
  52. package/dist/docs/functions/getCurrentScope.html +0 -4
  53. package/dist/docs/functions/isProxy.html +0 -5
  54. package/dist/docs/functions/isReactive.html +0 -5
  55. package/dist/docs/functions/isRef.html +0 -5
  56. package/dist/docs/functions/noTrack.html +0 -6
  57. package/dist/docs/functions/onScopeDispose.html +0 -6
  58. package/dist/docs/functions/reactive.html +0 -19
  59. package/dist/docs/functions/ref.html +0 -13
  60. package/dist/docs/functions/toRaw.html +0 -4
  61. package/dist/docs/hierarchy.html +0 -1
  62. package/dist/docs/index.html +0 -129
  63. package/dist/docs/interfaces/Computed.html +0 -9
  64. package/dist/docs/interfaces/Effect.html +0 -8
  65. package/dist/docs/interfaces/Ref.html +0 -9
  66. package/dist/docs/modules.html +0 -1
  67. package/dist/docs/types/Reactive.html +0 -3
  68. package/dist/docs/types/UnReadonly.html +0 -3
  69. package/dist/files/RobotoMono-Medium.woff2 +0 -0
  70. package/dist/files/RobotoMono-Regular.woff2 +0 -0
  71. package/dist/files/ic_code_black_24dp.svg +0 -4
  72. package/dist/files/ic_search_black_24dp.svg +0 -4
  73. package/dist/files/main.css +0 -629
  74. package/dist/files/thumbnails.svg +0 -7
  75. package/dist/files.json +0 -7
  76. package/dist/index.html +0 -84
  77. package/dist/screenshots//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274.jpg +0 -0
  78. package/dist/screenshots//346/225/260/347/273/204.jpg +0 -0
  79. package/dist/src//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274/index.html +0 -70
  80. package/dist/src//346/225/260/347/273/204/index.html +0 -65
  81. package/dist/tags.json +0 -2
@@ -1,6 +1,6 @@
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
- })(this, function(exports2) {
3
+ })(this, (function(exports2) {
4
4
  "use strict";
5
5
  class Reactivity {
6
6
  constructor() {
@@ -69,8 +69,16 @@
69
69
  let _shouldTrack = true;
70
70
  function batch(dep, isRunning) {
71
71
  if (isRunning) {
72
+ const index = _isRunedDeps.indexOf(dep);
73
+ if (index !== -1) {
74
+ console.warn("dep already in _isRunedDeps");
75
+ }
72
76
  _isRunedDeps.push(dep);
73
77
  } else {
78
+ const index = _needEffectDeps.indexOf(dep);
79
+ if (index !== -1) {
80
+ console.warn("dep already in _needEffectDeps,存在多重依赖问题。可能是由于副作用中多次修改了同一个响应式对象。");
81
+ }
74
82
  _needEffectDeps.push(dep);
75
83
  }
76
84
  }
@@ -1624,7 +1632,88 @@
1624
1632
  function isProxy(value) {
1625
1633
  return value ? !!value[ReactiveFlags.RAW] : false;
1626
1634
  }
1635
+ class ReactiveObject {
1636
+ constructor() {
1637
+ this._effectScope = new EffectScope();
1638
+ this._destroyCallbacks = [];
1639
+ }
1640
+ /**
1641
+ * 创建并运行副作用
1642
+ *
1643
+ * 功能:
1644
+ * 1. 将传入的函数包装为副作用
1645
+ * 2. 自动收集副作用中访问的响应式属性作为依赖
1646
+ * 3. 当依赖变化时自动重新执行副作用
1647
+ * 4. 在类销毁时自动停止副作用
1648
+ *
1649
+ * 使用场景:
1650
+ * - 监听属性变化并执行相应操作
1651
+ * - 自动更新UI或重新计算派生状态
1652
+ * - 执行清理或初始化逻辑
1653
+ *
1654
+ * @param fn 副作用函数,会在依赖变化时自动执行
1655
+ *
1656
+ * 使用示例:
1657
+ * ```typescript
1658
+ * this.effect(() => {
1659
+ * // 访问响应式属性,建立依赖关系
1660
+ * const value = reactive(this).someProperty;
1661
+ *
1662
+ * // 执行相应的逻辑
1663
+ * this.updateUI(value);
1664
+ * });
1665
+ * ```
1666
+ */
1667
+ effect(fn) {
1668
+ let eff;
1669
+ this._effectScope.run(() => {
1670
+ eff = effect(fn);
1671
+ });
1672
+ return eff;
1673
+ }
1674
+ /**
1675
+ * 销毁时执行的函数
1676
+ * @param callback 销毁时执行的函数
1677
+ */
1678
+ destroyCall(callback) {
1679
+ this._destroyCallbacks.push(callback);
1680
+ }
1681
+ /**
1682
+ * 销毁响应式类实例
1683
+ *
1684
+ * 执行清理操作:
1685
+ * 1. 执行所有注册的清理函数
1686
+ * 2. 停止所有副作用作用域,防止副作用继续执行
1687
+ * 3. 清理引用,帮助垃圾回收,防止内存泄漏
1688
+ *
1689
+ * 重要:
1690
+ * - 子类重写此方法时必须调用 super.destroy()
1691
+ * - 确保在类实例不再使用时调用此方法
1692
+ * - 调用后实例将无法再使用 effect() 方法
1693
+ *
1694
+ * 使用示例:
1695
+ * ```typescript
1696
+ * class MyClass extends ReactiveObject {
1697
+ * destroy() {
1698
+ * // 执行子类特定的清理逻辑
1699
+ * this.cleanup();
1700
+ *
1701
+ * // 必须调用父类的destroy方法
1702
+ * super.destroy();
1703
+ * }
1704
+ * }
1705
+ * ```
1706
+ */
1707
+ destroy() {
1708
+ var _a2, _b2;
1709
+ (_a2 = this._destroyCallbacks) == null ? void 0 : _a2.forEach((item) => item());
1710
+ this._destroyCallbacks = null;
1711
+ (_b2 = this._effectScope) == null ? void 0 : _b2.stop();
1712
+ this._effectScope = null;
1713
+ }
1714
+ }
1627
1715
  exports2.EffectScope = EffectScope;
1716
+ exports2.ReactiveObject = ReactiveObject;
1628
1717
  exports2.batchRun = batchRun;
1629
1718
  exports2.computed = computed;
1630
1719
  exports2.effect = effect;
@@ -1640,5 +1729,5 @@
1640
1729
  exports2.ref = ref;
1641
1730
  exports2.toRaw = toRaw;
1642
1731
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
1643
- });
1732
+ }));
1644
1733
  //# sourceMappingURL=index.umd.cjs.map