@feng3d/reactivity 1.0.7 → 1.0.9

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