@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.
- package/LICENSE +21 -21
- package/README.md +158 -158
- package/dist/index.js +89 -3
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +91 -5
- package/dist/index.umd.cjs.map +1 -1
- package/lib/ReactiveObject.d.ts +101 -0
- package/lib/ReactiveObject.d.ts.map +1 -0
- package/lib/batch.d.ts.map +1 -1
- package/lib/index.d.ts +5 -4
- package/lib/index.d.ts.map +1 -1
- package/package.json +69 -72
- package/src/ReactiveObject.ts +131 -0
- package/src/Reactivity.ts +168 -168
- package/src/arrayInstrumentations.ts +801 -801
- package/src/baseHandlers.ts +312 -312
- package/src/batch.ts +134 -118
- package/src/collectionHandlers.ts +486 -486
- package/src/computed.ts +253 -253
- package/src/effect.ts +146 -146
- package/src/effectScope.ts +294 -294
- package/src/index.ts +10 -9
- package/src/property.ts +231 -231
- package/src/reactive.ts +186 -186
- package/src/ref.ts +150 -150
- package/src/shared/constants.ts +41 -41
- package/src/shared/general.ts +109 -109
- package/tsconfig.json +19 -19
- package/dist/assets/RobotoMono-Medium-DVgDz_OO.woff2 +0 -0
- package/dist/assets/RobotoMono-Regular-BPoF81uy.woff2 +0 -0
- package/dist/assets/index-a2qCSG5V.css +0 -629
- package/dist/assets/index.html-Dyp3udP2.js +0 -200
- package/dist/assets/modulepreload-polyfill-DaKOjhqt.js +0 -37
- package/dist/assets/package-9zMEdmDL.js +0 -2540
- 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
- package/dist/assets/src//346/225/260/347/273/204/index.html-CaZ_5kCZ.js +0 -43
- package/dist/docs/.nojekyll +0 -1
- package/dist/docs/assets/hierarchy.js +0 -1
- package/dist/docs/assets/highlight.css +0 -92
- package/dist/docs/assets/icons.js +0 -18
- package/dist/docs/assets/icons.svg +0 -1
- package/dist/docs/assets/main.js +0 -60
- package/dist/docs/assets/navigation.js +0 -1
- package/dist/docs/assets/search.js +0 -1
- package/dist/docs/assets/style.css +0 -1640
- package/dist/docs/classes/ComputedReactivity.html +0 -72
- package/dist/docs/classes/EffectReactivity.html +0 -62
- package/dist/docs/classes/EffectScope.html +0 -40
- package/dist/docs/classes/Reactivity.html +0 -35
- package/dist/docs/classes/RefReactivity.html +0 -57
- package/dist/docs/functions/batchRun.html +0 -15
- package/dist/docs/functions/computed.html +0 -5
- package/dist/docs/functions/effect.html +0 -11
- package/dist/docs/functions/effectScope.html +0 -5
- package/dist/docs/functions/forceTrack.html +0 -6
- package/dist/docs/functions/getCurrentScope.html +0 -4
- package/dist/docs/functions/isProxy.html +0 -5
- package/dist/docs/functions/isReactive.html +0 -5
- package/dist/docs/functions/isRef.html +0 -5
- package/dist/docs/functions/noTrack.html +0 -6
- package/dist/docs/functions/onScopeDispose.html +0 -6
- package/dist/docs/functions/reactive.html +0 -19
- package/dist/docs/functions/ref.html +0 -13
- package/dist/docs/functions/toRaw.html +0 -4
- package/dist/docs/hierarchy.html +0 -1
- package/dist/docs/index.html +0 -129
- package/dist/docs/interfaces/Computed.html +0 -9
- package/dist/docs/interfaces/Effect.html +0 -8
- package/dist/docs/interfaces/Ref.html +0 -9
- package/dist/docs/modules.html +0 -1
- package/dist/docs/types/Reactive.html +0 -3
- package/dist/docs/types/UnReadonly.html +0 -3
- package/dist/files/RobotoMono-Medium.woff2 +0 -0
- package/dist/files/RobotoMono-Regular.woff2 +0 -0
- package/dist/files/ic_code_black_24dp.svg +0 -4
- package/dist/files/ic_search_black_24dp.svg +0 -4
- package/dist/files/main.css +0 -629
- package/dist/files/thumbnails.svg +0 -7
- package/dist/files.json +0 -7
- package/dist/index.html +0 -84
- package/dist/screenshots//345/244/215/346/235/202/346/203/205/345/206/265/345/217/226/345/200/274.jpg +0 -0
- package/dist/screenshots//346/225/260/347/273/204.jpg +0 -0
- 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
- package/dist/src//346/225/260/347/273/204/index.html +0 -65
- 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.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*/
|
|
118
|
-
|
|
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[] = [];
|