@feng3d/reactivity 1.0.6 → 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 (85) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +158 -158
  3. package/dist/index.js +89 -3
  4. package/dist/index.js.map +1 -1
  5. package/dist/index.umd.cjs +91 -5
  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 +5 -4
  11. package/lib/index.d.ts.map +1 -1
  12. package/package.json +69 -72
  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-9zMEdmDL.js +0 -2540
  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-a69uOZEV.js +0 -59
  36. package/dist/assets/src//346/225/260/347/273/204/index.html-CaZ_5kCZ.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/ComputedReactivity.html +0 -72
  47. package/dist/docs/classes/EffectReactivity.html +0 -62
  48. package/dist/docs/classes/EffectScope.html +0 -40
  49. package/dist/docs/classes/Reactivity.html +0 -35
  50. package/dist/docs/classes/RefReactivity.html +0 -57
  51. package/dist/docs/functions/batchRun.html +0 -15
  52. package/dist/docs/functions/computed.html +0 -5
  53. package/dist/docs/functions/effect.html +0 -11
  54. package/dist/docs/functions/effectScope.html +0 -5
  55. package/dist/docs/functions/forceTrack.html +0 -6
  56. package/dist/docs/functions/getCurrentScope.html +0 -4
  57. package/dist/docs/functions/isProxy.html +0 -5
  58. package/dist/docs/functions/isReactive.html +0 -5
  59. package/dist/docs/functions/isRef.html +0 -5
  60. package/dist/docs/functions/noTrack.html +0 -6
  61. package/dist/docs/functions/onScopeDispose.html +0 -6
  62. package/dist/docs/functions/reactive.html +0 -19
  63. package/dist/docs/functions/ref.html +0 -13
  64. package/dist/docs/functions/toRaw.html +0 -4
  65. package/dist/docs/hierarchy.html +0 -1
  66. package/dist/docs/index.html +0 -129
  67. package/dist/docs/interfaces/Computed.html +0 -9
  68. package/dist/docs/interfaces/Effect.html +0 -8
  69. package/dist/docs/interfaces/Ref.html +0 -9
  70. package/dist/docs/modules.html +0 -1
  71. package/dist/docs/types/Reactive.html +0 -3
  72. package/dist/docs/types/UnReadonly.html +0 -3
  73. package/dist/files/RobotoMono-Medium.woff2 +0 -0
  74. package/dist/files/RobotoMono-Regular.woff2 +0 -0
  75. package/dist/files/ic_code_black_24dp.svg +0 -4
  76. package/dist/files/ic_search_black_24dp.svg +0 -4
  77. package/dist/files/main.css +0 -629
  78. package/dist/files/thumbnails.svg +0 -7
  79. package/dist/files.json +0 -7
  80. package/dist/index.html +0 -84
  81. package/dist/screenshots//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274.jpg +0 -0
  82. package/dist/screenshots//346/225/260/347/273/204.jpg +0 -0
  83. 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
  84. package/dist/src//346/225/260/347/273/204/index.html +0 -65
  85. package/dist/tags.json +0 -2
package/src/batch.ts CHANGED
@@ -1,118 +1,134 @@
1
- import { ComputedReactivity } from './computed';
2
- import { Reactivity } from './Reactivity';
3
-
4
- /**
5
- * 合批处理依赖。
6
- *
7
- * 将依赖添加到待处理队列中,根据依赖的运行状态决定处理方式:
8
- * 1. 如果依赖正在运行,添加到已运行队列
9
- * 2. 如果依赖未运行,添加到待运行队列
10
- *
11
- * @param dep 要处理的依赖
12
- * @param isRunning 依赖是否正在运行
13
- */
14
- export function batch(dep: ComputedReactivity, isRunning: boolean): void
15
- {
16
- if (isRunning)
17
- {
18
- _isRunedDeps.push(dep);
19
- }
20
- else
21
- {
22
- _needEffectDeps.push(dep);
23
- }
24
- }
25
-
26
- /**
27
- * 批次执行多次修改反应式对象。
28
- *
29
- * 将多个响应式更新合并为一个批次执行,可以减少不必要的反应式触发。
30
- * 在批次执行期间:
31
- * 1. 所有响应式更新都会被收集
32
- * 2. 批次结束后统一处理所有更新
33
- * 3. 避免中间状态触发不必要的更新
34
- *
35
- * 示例:
36
- * ```ts
37
- * batchRun(() => {
38
- * // 修改反应式对象
39
- * reactiveObj.a = 1;
40
- * reactiveObj.b = 2;
41
- * })
42
- * ```
43
- *
44
- * @param fn 要执行的函数,在此函数中多次修改反应式对象
45
- * @returns 函数的执行结果
46
- */
47
- export function batchRun<T>(fn: () => T): T
48
- {
49
- _batchDepth++;
50
-
51
- const result = fn();
52
-
53
- if (--_batchDepth > 0)
54
- {
55
- return;
56
- }
57
-
58
- // 处理已经运行过的依赖
59
- if (_isRunedDeps.length > 0)
60
- {
61
- _isRunedDeps.forEach((dep) =>
62
- {
63
- // 此时依赖以及子依赖都已经运行过了,只需修复与子节点关系
64
- __DEV__ && console.assert(dep._isDirty === false, 'dep.dirty === false');
65
-
66
- // 修复与子节点关系
67
- dep._children.forEach((version, node) =>
68
- {
69
- node._parents.set(dep, dep._version);
70
- });
71
- dep._children.clear();
72
- });
73
- _isRunedDeps.length = 0;
74
- }
75
-
76
- // 批次处理待运行的依赖
77
- if (_needEffectDeps.length > 0)
78
- {
79
- _needEffectDeps.forEach((dep) =>
80
- {
81
- // 独立执行回调,避免影响其他依赖
82
- const pre = Reactivity.activeReactivity;
83
-
84
- Reactivity.activeReactivity = null;
85
-
86
- dep.runIfDirty();
87
-
88
- Reactivity.activeReactivity = pre;
89
- });
90
- _needEffectDeps.length = 0;
91
- }
92
-
93
- return result;
94
- }
95
-
96
- /**
97
- * 批次深度。
98
- *
99
- * 用于跟踪嵌套的批次执行。
100
- * 当深度大于 0 时,表示当前正在批次执行中。
101
- */
102
- let _batchDepth = 0;
103
-
104
- /**
105
- * 待运行的依赖队列。
106
- *
107
- * 存储需要执行但尚未执行的依赖。
108
- * 在批次执行结束后,会统一处理这些依赖。
109
- */
110
- const _needEffectDeps: ComputedReactivity[] = [];
111
-
112
- /**
113
- * 已运行的依赖队列。
114
- *
115
- * 存储已经执行过的依赖。
116
- * 这些依赖只需要修复与子节点的关系,不需要重新执行。
117
- */
118
- const _isRunedDeps: ComputedReactivity[] = [];
1
+ import { ComputedReactivity } from './computed';
2
+ import { Reactivity } from './Reactivity';
3
+
4
+ /**
5
+ * 合批处理依赖。
6
+ *
7
+ * 将依赖添加到待处理队列中,根据依赖的运行状态决定处理方式:
8
+ * 1. 如果依赖正在运行,添加到已运行队列
9
+ * 2. 如果依赖未运行,添加到待运行队列
10
+ *
11
+ * @param dep 要处理的依赖
12
+ * @param isRunning 依赖是否正在运行
13
+ */
14
+ export function batch(dep: ComputedReactivity, isRunning: boolean): void
15
+ {
16
+ if (isRunning)
17
+ {
18
+ const index = _isRunedDeps.indexOf(dep);
19
+
20
+ if (index !== -1)
21
+ {
22
+ console.warn('dep already in _isRunedDeps');
23
+ // _isRunedDeps.splice(index, 1);
24
+ }
25
+
26
+ _isRunedDeps.push(dep);
27
+ }
28
+ else
29
+ {
30
+ const index = _needEffectDeps.indexOf(dep);
31
+
32
+ if (index !== -1)
33
+ {
34
+ console.warn('dep already in _needEffectDeps,存在多重依赖问题。可能是由于副作用中多次修改了同一个响应式对象。');
35
+ // _needEffectDeps.splice(index, 1);
36
+ }
37
+
38
+ _needEffectDeps.push(dep);
39
+ }
40
+ }
41
+
42
+ /**
43
+ * 批次执行多次修改反应式对象。
44
+ *
45
+ * 将多个响应式更新合并为一个批次执行,可以减少不必要的反应式触发。
46
+ * 在批次执行期间:
47
+ * 1. 所有响应式更新都会被收集
48
+ * 2. 批次结束后统一处理所有更新
49
+ * 3. 避免中间状态触发不必要的更新
50
+ *
51
+ * 示例:
52
+ * ```ts
53
+ * batchRun(() => {
54
+ * // 修改反应式对象
55
+ * reactiveObj.a = 1;
56
+ * reactiveObj.b = 2;
57
+ * })
58
+ * ```
59
+ *
60
+ * @param fn 要执行的函数,在此函数中多次修改反应式对象
61
+ * @returns 函数的执行结果
62
+ */
63
+ export function batchRun<T>(fn: () => T): T
64
+ {
65
+ _batchDepth++;
66
+
67
+ const result = fn();
68
+
69
+ if (--_batchDepth > 0)
70
+ {
71
+ return;
72
+ }
73
+
74
+ // 处理已经运行过的依赖
75
+ if (_isRunedDeps.length > 0)
76
+ {
77
+ _isRunedDeps.forEach((dep) =>
78
+ {
79
+ // 此时依赖以及子依赖都已经运行过了,只需修复与子节点关系
80
+ __DEV__ && console.assert(dep._isDirty === false, 'dep.dirty === false');
81
+
82
+ // 修复与子节点关系
83
+ dep._children.forEach((version, node) =>
84
+ {
85
+ node._parents.set(dep, dep._version);
86
+ });
87
+ dep._children.clear();
88
+ });
89
+ _isRunedDeps.length = 0;
90
+ }
91
+
92
+ // 批次处理待运行的依赖
93
+ if (_needEffectDeps.length > 0)
94
+ {
95
+ _needEffectDeps.forEach((dep) =>
96
+ {
97
+ // 独立执行回调,避免影响其他依赖
98
+ const pre = Reactivity.activeReactivity;
99
+
100
+ Reactivity.activeReactivity = null;
101
+
102
+ dep.runIfDirty();
103
+
104
+ Reactivity.activeReactivity = pre;
105
+ });
106
+ _needEffectDeps.length = 0;
107
+ }
108
+
109
+ return result;
110
+ }
111
+
112
+ /**
113
+ * 批次深度。
114
+ *
115
+ * 用于跟踪嵌套的批次执行。
116
+ * 当深度大于 0 时,表示当前正在批次执行中。
117
+ */
118
+ let _batchDepth = 0;
119
+
120
+ /**
121
+ * 待运行的依赖队列。
122
+ *
123
+ * 存储需要执行但尚未执行的依赖。
124
+ * 在批次执行结束后,会统一处理这些依赖。
125
+ */
126
+ const _needEffectDeps: ComputedReactivity[] = [];
127
+
128
+ /**
129
+ * 已运行的依赖队列。
130
+ *
131
+ * 存储已经执行过的依赖。
132
+ * 这些依赖只需要修复与子节点的关系,不需要重新执行。
133
+ */
134
+ const _isRunedDeps: ComputedReactivity[] = [];