@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
package/dist/index.js CHANGED
@@ -65,8 +65,14 @@ function noTrack(fn) {
65
65
  let _shouldTrack = true;
66
66
  function batch(dep, isRunning) {
67
67
  if (isRunning) {
68
+ if (_isRunedDeps.indexOf(dep) !== -1) {
69
+ return;
70
+ }
68
71
  _isRunedDeps.push(dep);
69
72
  } else {
73
+ if (_needEffectDeps.indexOf(dep) !== -1) {
74
+ return;
75
+ }
70
76
  _needEffectDeps.push(dep);
71
77
  }
72
78
  }
@@ -1620,8 +1626,89 @@ const toReactive = (value) => {
1620
1626
  function isProxy(value) {
1621
1627
  return value ? !!value[ReactiveFlags.RAW] : false;
1622
1628
  }
1629
+ class ReactiveObject {
1630
+ constructor() {
1631
+ this._effectScope = new EffectScope();
1632
+ this._destroyCallbacks = [];
1633
+ }
1634
+ /**
1635
+ * 创建并运行副作用
1636
+ *
1637
+ * 功能:
1638
+ * 1. 将传入的函数包装为副作用
1639
+ * 2. 自动收集副作用中访问的响应式属性作为依赖
1640
+ * 3. 当依赖变化时自动重新执行副作用
1641
+ * 4. 在类销毁时自动停止副作用
1642
+ *
1643
+ * 使用场景:
1644
+ * - 监听属性变化并执行相应操作
1645
+ * - 自动更新UI或重新计算派生状态
1646
+ * - 执行清理或初始化逻辑
1647
+ *
1648
+ * @param fn 副作用函数,会在依赖变化时自动执行
1649
+ *
1650
+ * 使用示例:
1651
+ * ```typescript
1652
+ * this.effect(() => {
1653
+ * // 访问响应式属性,建立依赖关系
1654
+ * const value = reactive(this).someProperty;
1655
+ *
1656
+ * // 执行相应的逻辑
1657
+ * this.updateUI(value);
1658
+ * });
1659
+ * ```
1660
+ */
1661
+ effect(fn) {
1662
+ let eff;
1663
+ this._effectScope.run(() => {
1664
+ eff = effect(fn);
1665
+ });
1666
+ return eff;
1667
+ }
1668
+ /**
1669
+ * 销毁时执行的函数
1670
+ * @param callback 销毁时执行的函数
1671
+ */
1672
+ destroyCall(callback) {
1673
+ this._destroyCallbacks.push(callback);
1674
+ }
1675
+ /**
1676
+ * 销毁响应式类实例
1677
+ *
1678
+ * 执行清理操作:
1679
+ * 1. 执行所有注册的清理函数
1680
+ * 2. 停止所有副作用作用域,防止副作用继续执行
1681
+ * 3. 清理引用,帮助垃圾回收,防止内存泄漏
1682
+ *
1683
+ * 重要:
1684
+ * - 子类重写此方法时必须调用 super.destroy()
1685
+ * - 确保在类实例不再使用时调用此方法
1686
+ * - 调用后实例将无法再使用 effect() 方法
1687
+ *
1688
+ * 使用示例:
1689
+ * ```typescript
1690
+ * class MyClass extends ReactiveObject {
1691
+ * destroy() {
1692
+ * // 执行子类特定的清理逻辑
1693
+ * this.cleanup();
1694
+ *
1695
+ * // 必须调用父类的destroy方法
1696
+ * super.destroy();
1697
+ * }
1698
+ * }
1699
+ * ```
1700
+ */
1701
+ destroy() {
1702
+ var _a2, _b2;
1703
+ (_a2 = this._destroyCallbacks) == null ? void 0 : _a2.forEach((item) => item());
1704
+ this._destroyCallbacks = null;
1705
+ (_b2 = this._effectScope) == null ? void 0 : _b2.stop();
1706
+ this._effectScope = null;
1707
+ }
1708
+ }
1623
1709
  export {
1624
1710
  EffectScope,
1711
+ ReactiveObject,
1625
1712
  batchRun,
1626
1713
  computed,
1627
1714
  effect,