@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/effectScope.ts
CHANGED
|
@@ -1,294 +1,294 @@
|
|
|
1
|
-
import type { Effect as ReactiveEffect } from './effect';
|
|
2
|
-
import { warn } from './shared/general';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 当前活动的效果作用域
|
|
6
|
-
*/
|
|
7
|
-
export let activeEffectScope: EffectScope | undefined;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 效果作用域类
|
|
11
|
-
*
|
|
12
|
-
* 用于管理一组相关的响应式效果,可以统一控制它们的生命周期。
|
|
13
|
-
*/
|
|
14
|
-
export class EffectScope
|
|
15
|
-
{
|
|
16
|
-
/**
|
|
17
|
-
* 作用域是否处于活动状态
|
|
18
|
-
* @internal
|
|
19
|
-
*/
|
|
20
|
-
private _active = true;
|
|
21
|
-
/**
|
|
22
|
-
* 跟踪 on 方法的调用次数,允许多次调用 on 方法
|
|
23
|
-
* @internal
|
|
24
|
-
*/
|
|
25
|
-
private _on = 0;
|
|
26
|
-
/**
|
|
27
|
-
* 存储当前作用域中的所有效果
|
|
28
|
-
* @internal
|
|
29
|
-
*/
|
|
30
|
-
effects: ReactiveEffect[] = [];
|
|
31
|
-
/**
|
|
32
|
-
* 存储清理函数
|
|
33
|
-
* @internal
|
|
34
|
-
*/
|
|
35
|
-
cleanups: (() => void)[] = [];
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* 作用域是否被暂停
|
|
39
|
-
*/
|
|
40
|
-
private _isPaused = false;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 父作用域,仅由非分离的作用域分配
|
|
44
|
-
* @internal
|
|
45
|
-
*/
|
|
46
|
-
parent: EffectScope | undefined;
|
|
47
|
-
/**
|
|
48
|
-
* 记录未分离的子作用域
|
|
49
|
-
* @internal
|
|
50
|
-
*/
|
|
51
|
-
scopes: EffectScope[] | undefined;
|
|
52
|
-
/**
|
|
53
|
-
* 在父作用域的 scopes 数组中记录子作用域的索引,用于优化移除操作
|
|
54
|
-
* @internal
|
|
55
|
-
*/
|
|
56
|
-
private index: number | undefined;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 构造函数
|
|
60
|
-
* @param detached 是否创建分离的作用域
|
|
61
|
-
*/
|
|
62
|
-
constructor(public detached = false)
|
|
63
|
-
{
|
|
64
|
-
this.parent = activeEffectScope;
|
|
65
|
-
if (!detached && activeEffectScope)
|
|
66
|
-
{
|
|
67
|
-
this.index =
|
|
68
|
-
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
|
|
69
|
-
this,
|
|
70
|
-
) - 1;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 获取作用域是否处于活动状态
|
|
76
|
-
*/
|
|
77
|
-
get active(): boolean
|
|
78
|
-
{
|
|
79
|
-
return this._active;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 暂停作用域
|
|
84
|
-
*
|
|
85
|
-
* 暂停当前作用域及其所有子作用域和效果
|
|
86
|
-
*/
|
|
87
|
-
pause(): void
|
|
88
|
-
{
|
|
89
|
-
if (this._active)
|
|
90
|
-
{
|
|
91
|
-
this._isPaused = true;
|
|
92
|
-
let i: number, l: number;
|
|
93
|
-
|
|
94
|
-
if (this.scopes)
|
|
95
|
-
{
|
|
96
|
-
for (i = 0, l = this.scopes.length; i < l; i++)
|
|
97
|
-
{
|
|
98
|
-
this.scopes[i].pause();
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
for (i = 0, l = this.effects.length; i < l; i++)
|
|
102
|
-
{
|
|
103
|
-
this.effects[i].pause();
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* 恢复作用域
|
|
110
|
-
*
|
|
111
|
-
* 恢复当前作用域及其所有子作用域和效果
|
|
112
|
-
*/
|
|
113
|
-
resume(): void
|
|
114
|
-
{
|
|
115
|
-
if (this._active)
|
|
116
|
-
{
|
|
117
|
-
if (this._isPaused)
|
|
118
|
-
{
|
|
119
|
-
this._isPaused = false;
|
|
120
|
-
let i: number, l: number;
|
|
121
|
-
|
|
122
|
-
if (this.scopes)
|
|
123
|
-
{
|
|
124
|
-
for (i = 0, l = this.scopes.length; i < l; i++)
|
|
125
|
-
{
|
|
126
|
-
this.scopes[i].resume();
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
for (i = 0, l = this.effects.length; i < l; i++)
|
|
130
|
-
{
|
|
131
|
-
this.effects[i].resume();
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* 在作用域中运行函数
|
|
139
|
-
* @param fn 要运行的函数
|
|
140
|
-
* @returns 函数的返回值
|
|
141
|
-
*/
|
|
142
|
-
run<T>(fn: () => T): T | undefined
|
|
143
|
-
{
|
|
144
|
-
if (this._active)
|
|
145
|
-
{
|
|
146
|
-
const currentEffectScope = activeEffectScope;
|
|
147
|
-
|
|
148
|
-
try
|
|
149
|
-
{
|
|
150
|
-
activeEffectScope = this;
|
|
151
|
-
|
|
152
|
-
return fn();
|
|
153
|
-
}
|
|
154
|
-
finally
|
|
155
|
-
{
|
|
156
|
-
activeEffectScope = currentEffectScope;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
else if (__DEV__)
|
|
160
|
-
{
|
|
161
|
-
warn(`cannot run an inactive effect scope.`);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* 前一个作用域
|
|
167
|
-
*/
|
|
168
|
-
prevScope: EffectScope | undefined;
|
|
169
|
-
/**
|
|
170
|
-
* 激活作用域
|
|
171
|
-
* 仅应在非分离的作用域上调用
|
|
172
|
-
* @internal
|
|
173
|
-
*/
|
|
174
|
-
on(): void
|
|
175
|
-
{
|
|
176
|
-
if (++this._on === 1)
|
|
177
|
-
{
|
|
178
|
-
this.prevScope = activeEffectScope;
|
|
179
|
-
activeEffectScope = this;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* 停用作用域
|
|
185
|
-
* 仅应在非分离的作用域上调用
|
|
186
|
-
* @internal
|
|
187
|
-
*/
|
|
188
|
-
off(): void
|
|
189
|
-
{
|
|
190
|
-
if (this._on > 0 && --this._on === 0)
|
|
191
|
-
{
|
|
192
|
-
activeEffectScope = this.prevScope;
|
|
193
|
-
this.prevScope = undefined;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
/**
|
|
198
|
-
* 停止作用域
|
|
199
|
-
*
|
|
200
|
-
* 停止当前作用域及其所有子作用域和效果,并执行清理函数
|
|
201
|
-
* @param fromParent 是否由父作用域调用
|
|
202
|
-
*/
|
|
203
|
-
stop(fromParent?: boolean): void
|
|
204
|
-
{
|
|
205
|
-
if (this._active)
|
|
206
|
-
{
|
|
207
|
-
this._active = false;
|
|
208
|
-
let i: number, l: number;
|
|
209
|
-
|
|
210
|
-
for (i = 0, l = this.effects.length; i < l; i++)
|
|
211
|
-
{
|
|
212
|
-
this.effects[i].stop();
|
|
213
|
-
}
|
|
214
|
-
this.effects.length = 0;
|
|
215
|
-
|
|
216
|
-
for (i = 0, l = this.cleanups.length; i < l; i++)
|
|
217
|
-
{
|
|
218
|
-
this.cleanups[i]();
|
|
219
|
-
}
|
|
220
|
-
this.cleanups.length = 0;
|
|
221
|
-
|
|
222
|
-
if (this.scopes)
|
|
223
|
-
{
|
|
224
|
-
for (i = 0, l = this.scopes.length; i < l; i++)
|
|
225
|
-
{
|
|
226
|
-
this.scopes[i].stop(true);
|
|
227
|
-
}
|
|
228
|
-
this.scopes.length = 0;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// 嵌套作用域,从父作用域中解除引用以避免内存泄漏
|
|
232
|
-
if (!this.detached && this.parent && !fromParent)
|
|
233
|
-
{
|
|
234
|
-
// 优化的 O(1) 移除
|
|
235
|
-
const last = this.parent.scopes!.pop();
|
|
236
|
-
|
|
237
|
-
if (last && last !== this)
|
|
238
|
-
{
|
|
239
|
-
this.parent.scopes![this.index!] = last;
|
|
240
|
-
last.index = this.index!;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
this.parent = undefined;
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* 创建效果作用域对象
|
|
250
|
-
*
|
|
251
|
-
* 可以捕获在其中创建的响应式效果(即计算属性和观察者),以便这些效果可以一起处理。
|
|
252
|
-
*
|
|
253
|
-
* @param detached 是否创建分离的作用域
|
|
254
|
-
* @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
|
|
255
|
-
*/
|
|
256
|
-
export function effectScope(detached?: boolean): EffectScope
|
|
257
|
-
{
|
|
258
|
-
return new EffectScope(detached);
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* 获取当前活动的效果作用域
|
|
263
|
-
*
|
|
264
|
-
* @returns 当前活动的效果作用域,如果没有则返回 undefined
|
|
265
|
-
* @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
|
|
266
|
-
*/
|
|
267
|
-
export function getCurrentScope(): EffectScope | undefined
|
|
268
|
-
{
|
|
269
|
-
return activeEffectScope;
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* 在当前活动的效果作用域上注册清理回调
|
|
274
|
-
*
|
|
275
|
-
* 当关联的效果作用域停止时,将调用此回调函数。
|
|
276
|
-
*
|
|
277
|
-
* @param fn 要附加到作用域清理的回调函数
|
|
278
|
-
* @param failSilently 是否静默失败
|
|
279
|
-
* @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
|
|
280
|
-
*/
|
|
281
|
-
export function onScopeDispose(fn: () => void, failSilently = false): void
|
|
282
|
-
{
|
|
283
|
-
if (activeEffectScope)
|
|
284
|
-
{
|
|
285
|
-
activeEffectScope.cleanups.push(fn);
|
|
286
|
-
}
|
|
287
|
-
else if (__DEV__ && !failSilently)
|
|
288
|
-
{
|
|
289
|
-
warn(
|
|
290
|
-
`onScopeDispose() is called when there is no active effect scope` +
|
|
291
|
-
` to be associated with.`,
|
|
292
|
-
);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
1
|
+
import type { Effect as ReactiveEffect } from './effect';
|
|
2
|
+
import { warn } from './shared/general';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 当前活动的效果作用域
|
|
6
|
+
*/
|
|
7
|
+
export let activeEffectScope: EffectScope | undefined;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 效果作用域类
|
|
11
|
+
*
|
|
12
|
+
* 用于管理一组相关的响应式效果,可以统一控制它们的生命周期。
|
|
13
|
+
*/
|
|
14
|
+
export class EffectScope
|
|
15
|
+
{
|
|
16
|
+
/**
|
|
17
|
+
* 作用域是否处于活动状态
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
private _active = true;
|
|
21
|
+
/**
|
|
22
|
+
* 跟踪 on 方法的调用次数,允许多次调用 on 方法
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
private _on = 0;
|
|
26
|
+
/**
|
|
27
|
+
* 存储当前作用域中的所有效果
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
effects: ReactiveEffect[] = [];
|
|
31
|
+
/**
|
|
32
|
+
* 存储清理函数
|
|
33
|
+
* @internal
|
|
34
|
+
*/
|
|
35
|
+
cleanups: (() => void)[] = [];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 作用域是否被暂停
|
|
39
|
+
*/
|
|
40
|
+
private _isPaused = false;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 父作用域,仅由非分离的作用域分配
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
parent: EffectScope | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* 记录未分离的子作用域
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
scopes: EffectScope[] | undefined;
|
|
52
|
+
/**
|
|
53
|
+
* 在父作用域的 scopes 数组中记录子作用域的索引,用于优化移除操作
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
private index: number | undefined;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 构造函数
|
|
60
|
+
* @param detached 是否创建分离的作用域
|
|
61
|
+
*/
|
|
62
|
+
constructor(public detached = false)
|
|
63
|
+
{
|
|
64
|
+
this.parent = activeEffectScope;
|
|
65
|
+
if (!detached && activeEffectScope)
|
|
66
|
+
{
|
|
67
|
+
this.index =
|
|
68
|
+
(activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
|
|
69
|
+
this,
|
|
70
|
+
) - 1;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 获取作用域是否处于活动状态
|
|
76
|
+
*/
|
|
77
|
+
get active(): boolean
|
|
78
|
+
{
|
|
79
|
+
return this._active;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* 暂停作用域
|
|
84
|
+
*
|
|
85
|
+
* 暂停当前作用域及其所有子作用域和效果
|
|
86
|
+
*/
|
|
87
|
+
pause(): void
|
|
88
|
+
{
|
|
89
|
+
if (this._active)
|
|
90
|
+
{
|
|
91
|
+
this._isPaused = true;
|
|
92
|
+
let i: number, l: number;
|
|
93
|
+
|
|
94
|
+
if (this.scopes)
|
|
95
|
+
{
|
|
96
|
+
for (i = 0, l = this.scopes.length; i < l; i++)
|
|
97
|
+
{
|
|
98
|
+
this.scopes[i].pause();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
for (i = 0, l = this.effects.length; i < l; i++)
|
|
102
|
+
{
|
|
103
|
+
this.effects[i].pause();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* 恢复作用域
|
|
110
|
+
*
|
|
111
|
+
* 恢复当前作用域及其所有子作用域和效果
|
|
112
|
+
*/
|
|
113
|
+
resume(): void
|
|
114
|
+
{
|
|
115
|
+
if (this._active)
|
|
116
|
+
{
|
|
117
|
+
if (this._isPaused)
|
|
118
|
+
{
|
|
119
|
+
this._isPaused = false;
|
|
120
|
+
let i: number, l: number;
|
|
121
|
+
|
|
122
|
+
if (this.scopes)
|
|
123
|
+
{
|
|
124
|
+
for (i = 0, l = this.scopes.length; i < l; i++)
|
|
125
|
+
{
|
|
126
|
+
this.scopes[i].resume();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
for (i = 0, l = this.effects.length; i < l; i++)
|
|
130
|
+
{
|
|
131
|
+
this.effects[i].resume();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 在作用域中运行函数
|
|
139
|
+
* @param fn 要运行的函数
|
|
140
|
+
* @returns 函数的返回值
|
|
141
|
+
*/
|
|
142
|
+
run<T>(fn: () => T): T | undefined
|
|
143
|
+
{
|
|
144
|
+
if (this._active)
|
|
145
|
+
{
|
|
146
|
+
const currentEffectScope = activeEffectScope;
|
|
147
|
+
|
|
148
|
+
try
|
|
149
|
+
{
|
|
150
|
+
activeEffectScope = this;
|
|
151
|
+
|
|
152
|
+
return fn();
|
|
153
|
+
}
|
|
154
|
+
finally
|
|
155
|
+
{
|
|
156
|
+
activeEffectScope = currentEffectScope;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else if (__DEV__)
|
|
160
|
+
{
|
|
161
|
+
warn(`cannot run an inactive effect scope.`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 前一个作用域
|
|
167
|
+
*/
|
|
168
|
+
prevScope: EffectScope | undefined;
|
|
169
|
+
/**
|
|
170
|
+
* 激活作用域
|
|
171
|
+
* 仅应在非分离的作用域上调用
|
|
172
|
+
* @internal
|
|
173
|
+
*/
|
|
174
|
+
on(): void
|
|
175
|
+
{
|
|
176
|
+
if (++this._on === 1)
|
|
177
|
+
{
|
|
178
|
+
this.prevScope = activeEffectScope;
|
|
179
|
+
activeEffectScope = this;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 停用作用域
|
|
185
|
+
* 仅应在非分离的作用域上调用
|
|
186
|
+
* @internal
|
|
187
|
+
*/
|
|
188
|
+
off(): void
|
|
189
|
+
{
|
|
190
|
+
if (this._on > 0 && --this._on === 0)
|
|
191
|
+
{
|
|
192
|
+
activeEffectScope = this.prevScope;
|
|
193
|
+
this.prevScope = undefined;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 停止作用域
|
|
199
|
+
*
|
|
200
|
+
* 停止当前作用域及其所有子作用域和效果,并执行清理函数
|
|
201
|
+
* @param fromParent 是否由父作用域调用
|
|
202
|
+
*/
|
|
203
|
+
stop(fromParent?: boolean): void
|
|
204
|
+
{
|
|
205
|
+
if (this._active)
|
|
206
|
+
{
|
|
207
|
+
this._active = false;
|
|
208
|
+
let i: number, l: number;
|
|
209
|
+
|
|
210
|
+
for (i = 0, l = this.effects.length; i < l; i++)
|
|
211
|
+
{
|
|
212
|
+
this.effects[i].stop();
|
|
213
|
+
}
|
|
214
|
+
this.effects.length = 0;
|
|
215
|
+
|
|
216
|
+
for (i = 0, l = this.cleanups.length; i < l; i++)
|
|
217
|
+
{
|
|
218
|
+
this.cleanups[i]();
|
|
219
|
+
}
|
|
220
|
+
this.cleanups.length = 0;
|
|
221
|
+
|
|
222
|
+
if (this.scopes)
|
|
223
|
+
{
|
|
224
|
+
for (i = 0, l = this.scopes.length; i < l; i++)
|
|
225
|
+
{
|
|
226
|
+
this.scopes[i].stop(true);
|
|
227
|
+
}
|
|
228
|
+
this.scopes.length = 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// 嵌套作用域,从父作用域中解除引用以避免内存泄漏
|
|
232
|
+
if (!this.detached && this.parent && !fromParent)
|
|
233
|
+
{
|
|
234
|
+
// 优化的 O(1) 移除
|
|
235
|
+
const last = this.parent.scopes!.pop();
|
|
236
|
+
|
|
237
|
+
if (last && last !== this)
|
|
238
|
+
{
|
|
239
|
+
this.parent.scopes![this.index!] = last;
|
|
240
|
+
last.index = this.index!;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
this.parent = undefined;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* 创建效果作用域对象
|
|
250
|
+
*
|
|
251
|
+
* 可以捕获在其中创建的响应式效果(即计算属性和观察者),以便这些效果可以一起处理。
|
|
252
|
+
*
|
|
253
|
+
* @param detached 是否创建分离的作用域
|
|
254
|
+
* @see {@link https://vuejs.org/api/reactivity-advanced.html#effectscope}
|
|
255
|
+
*/
|
|
256
|
+
export function effectScope(detached?: boolean): EffectScope
|
|
257
|
+
{
|
|
258
|
+
return new EffectScope(detached);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 获取当前活动的效果作用域
|
|
263
|
+
*
|
|
264
|
+
* @returns 当前活动的效果作用域,如果没有则返回 undefined
|
|
265
|
+
* @see {@link https://vuejs.org/api/reactivity-advanced.html#getcurrentscope}
|
|
266
|
+
*/
|
|
267
|
+
export function getCurrentScope(): EffectScope | undefined
|
|
268
|
+
{
|
|
269
|
+
return activeEffectScope;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* 在当前活动的效果作用域上注册清理回调
|
|
274
|
+
*
|
|
275
|
+
* 当关联的效果作用域停止时,将调用此回调函数。
|
|
276
|
+
*
|
|
277
|
+
* @param fn 要附加到作用域清理的回调函数
|
|
278
|
+
* @param failSilently 是否静默失败
|
|
279
|
+
* @see {@link https://vuejs.org/api/reactivity-advanced.html#onscopedispose}
|
|
280
|
+
*/
|
|
281
|
+
export function onScopeDispose(fn: () => void, failSilently = false): void
|
|
282
|
+
{
|
|
283
|
+
if (activeEffectScope)
|
|
284
|
+
{
|
|
285
|
+
activeEffectScope.cleanups.push(fn);
|
|
286
|
+
}
|
|
287
|
+
else if (__DEV__ && !failSilently)
|
|
288
|
+
{
|
|
289
|
+
warn(
|
|
290
|
+
`onScopeDispose() is called when there is no active effect scope` +
|
|
291
|
+
` to be associated with.`,
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
export { batchRun } from './batch';
|
|
2
|
-
export { computed, type Computed
|
|
3
|
-
export { effect,
|
|
4
|
-
export { effectScope, EffectScope, getCurrentScope, onScopeDispose } from './effectScope';
|
|
5
|
-
export { isProxy, isReactive, reactive, type Reactive, type UnReadonly } from './reactive';
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
export {
|
|
9
|
-
|
|
1
|
+
export { batchRun } from './batch';
|
|
2
|
+
export { computed, type Computed } from './computed';
|
|
3
|
+
export { effect, type Effect } from './effect';
|
|
4
|
+
export { effectScope, EffectScope, getCurrentScope, onScopeDispose } from './effectScope';
|
|
5
|
+
export { isProxy, isReactive, reactive, type Reactive, type UnReadonly } from './reactive';
|
|
6
|
+
export { ReactiveObject } from './ReactiveObject';
|
|
7
|
+
export { forceTrack, noTrack } from './Reactivity';
|
|
8
|
+
export { isRef, ref, type Ref } from './ref';
|
|
9
|
+
export { toRaw } from './shared/general';
|
|
10
|
+
|