@feng3d/reactivity 1.0.4 → 1.0.6
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/assets/RobotoMono-Medium-DVgDz_OO.woff2 +0 -0
- package/dist/assets/RobotoMono-Regular-BPoF81uy.woff2 +0 -0
- package/dist/assets/index-a2qCSG5V.css +629 -0
- package/dist/assets/index.html-Dyp3udP2.js +200 -0
- package/dist/assets/modulepreload-polyfill-DaKOjhqt.js +37 -0
- package/dist/assets/package-9zMEdmDL.js +2540 -0
- 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 +59 -0
- package/dist/assets/src//346/225/260/347/273/204/index.html-CaZ_5kCZ.js +43 -0
- package/dist/docs/.nojekyll +1 -0
- package/dist/docs/assets/hierarchy.js +1 -0
- package/dist/docs/assets/highlight.css +92 -0
- package/dist/docs/assets/icons.js +18 -0
- package/dist/docs/assets/icons.svg +1 -0
- package/dist/docs/assets/main.js +60 -0
- package/dist/docs/assets/navigation.js +1 -0
- package/dist/docs/assets/search.js +1 -0
- package/dist/docs/assets/style.css +1640 -0
- package/dist/docs/classes/ComputedReactivity.html +72 -0
- package/dist/docs/classes/EffectReactivity.html +62 -0
- package/dist/docs/classes/EffectScope.html +40 -0
- package/dist/docs/classes/Reactivity.html +35 -0
- package/dist/docs/classes/RefReactivity.html +57 -0
- package/dist/docs/functions/batchRun.html +15 -0
- package/dist/docs/functions/computed.html +5 -0
- package/dist/docs/functions/effect.html +11 -0
- package/dist/docs/functions/effectScope.html +5 -0
- package/dist/docs/functions/forceTrack.html +6 -0
- package/dist/docs/functions/getCurrentScope.html +4 -0
- package/dist/docs/functions/isProxy.html +5 -0
- package/dist/docs/functions/isReactive.html +5 -0
- package/dist/docs/functions/isRef.html +5 -0
- package/dist/docs/functions/noTrack.html +6 -0
- package/dist/docs/functions/onScopeDispose.html +6 -0
- package/dist/docs/functions/reactive.html +19 -0
- package/dist/docs/functions/ref.html +13 -0
- package/dist/docs/functions/toRaw.html +4 -0
- package/dist/docs/hierarchy.html +1 -0
- package/dist/docs/index.html +129 -0
- package/dist/docs/interfaces/Computed.html +9 -0
- package/dist/docs/interfaces/Effect.html +8 -0
- package/dist/docs/interfaces/Ref.html +9 -0
- package/dist/docs/modules.html +1 -0
- package/dist/docs/types/Reactive.html +3 -0
- package/dist/docs/types/UnReadonly.html +3 -0
- package/dist/files/RobotoMono-Medium.woff2 +0 -0
- package/dist/files/RobotoMono-Regular.woff2 +0 -0
- package/dist/files/ic_code_black_24dp.svg +4 -0
- package/dist/files/ic_search_black_24dp.svg +4 -0
- package/dist/files/main.css +629 -0
- package/dist/files/thumbnails.svg +7 -0
- package/dist/files.json +7 -0
- package/dist/index.html +84 -0
- package/dist/index.js +735 -156
- package/dist/index.js.map +1 -1
- package/dist/index.umd.cjs +735 -156
- package/dist/index.umd.cjs.map +1 -1
- 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 +70 -0
- package/dist/src//346/225/260/347/273/204/index.html +65 -0
- package/dist/tags.json +2 -0
- package/lib/Reactivity.d.ts +47 -13
- package/lib/Reactivity.d.ts.map +1 -1
- package/lib/arrayInstrumentations.d.ts +10 -0
- package/lib/arrayInstrumentations.d.ts.map +1 -1
- package/lib/baseHandlers.d.ts +3 -1
- package/lib/baseHandlers.d.ts.map +1 -1
- package/lib/batch.d.ts +17 -5
- package/lib/batch.d.ts.map +1 -1
- package/lib/collectionHandlers.d.ts +21 -0
- package/lib/collectionHandlers.d.ts.map +1 -1
- package/lib/computed.d.ts +75 -27
- package/lib/computed.d.ts.map +1 -1
- package/lib/effect.d.ts +25 -0
- package/lib/effect.d.ts.map +1 -1
- package/lib/effectScope.d.ts +129 -0
- package/lib/effectScope.d.ts.map +1 -0
- package/lib/index.d.ts +6 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/property.d.ts.map +1 -1
- package/lib/reactive.d.ts +63 -14
- package/lib/reactive.d.ts.map +1 -1
- package/lib/ref.d.ts +66 -4
- package/lib/ref.d.ts.map +1 -1
- package/lib/shared/constants.d.ts +7 -7
- package/lib/shared/constants.d.ts.map +1 -1
- package/lib/shared/general.d.ts +1 -1
- package/lib/shared/general.d.ts.map +1 -1
- package/package.json +71 -66
- package/src/Reactivity.ts +168 -126
- package/src/arrayInstrumentations.ts +801 -448
- package/src/baseHandlers.ts +312 -220
- package/src/batch.ts +118 -91
- package/src/collectionHandlers.ts +486 -298
- package/src/computed.ts +253 -204
- package/src/effect.ts +146 -108
- package/src/effectScope.ts +294 -0
- package/src/index.ts +9 -8
- package/src/property.ts +231 -225
- package/src/reactive.ts +186 -130
- package/src/ref.ts +150 -88
- package/src/shared/constants.ts +41 -41
- package/src/shared/general.ts +109 -109
- package/tsconfig.json +19 -19
package/src/effect.ts
CHANGED
|
@@ -1,108 +1,146 @@
|
|
|
1
|
-
import { batch, batchRun } from './batch';
|
|
2
|
-
import { ComputedReactivity } from './computed';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
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
|
-
this.
|
|
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
|
-
|
|
1
|
+
import { batch, batchRun } from './batch';
|
|
2
|
+
import { ComputedReactivity } from './computed';
|
|
3
|
+
import { activeEffectScope } from './effectScope';
|
|
4
|
+
import { Reactivity } from './Reactivity';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 创建效果反应式节点。
|
|
8
|
+
*
|
|
9
|
+
* 将会维持反应式效果,当被作用的函数所引用的响应式对象发生变化时,会立即执行 fn 函数。
|
|
10
|
+
*
|
|
11
|
+
* @param fn 要执行的函数
|
|
12
|
+
* @returns 暂停和恢复副作用的函数
|
|
13
|
+
*
|
|
14
|
+
* 注:
|
|
15
|
+
* 1. 与 `@vue/reactivity` 中的 effect 不同,此函数返回的是一个 Effect 对象,而不是一个函数。
|
|
16
|
+
* 2. 不希望用户直接执行,而是通过反应式自动触发。
|
|
17
|
+
* 3. 真有需求,可以使用 effect(func).run(true) 来代替 @vue/reactivity 中的 effect(func)() 。
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
export function effect<T = any>(fn: () => T): Effect
|
|
21
|
+
{
|
|
22
|
+
return new EffectReactivity(fn);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 效果反应式节点。
|
|
27
|
+
*/
|
|
28
|
+
export class EffectReactivity<T = any> extends ComputedReactivity<T> implements Effect
|
|
29
|
+
{
|
|
30
|
+
/**
|
|
31
|
+
* 是否为启用, 默认为 true。
|
|
32
|
+
*
|
|
33
|
+
* 启用时,会立即执行函数。
|
|
34
|
+
*/
|
|
35
|
+
private _isEnable = true;
|
|
36
|
+
|
|
37
|
+
constructor(func: (oldValue?: T) => T)
|
|
38
|
+
{
|
|
39
|
+
super(func);
|
|
40
|
+
if (activeEffectScope && activeEffectScope.active)
|
|
41
|
+
{
|
|
42
|
+
activeEffectScope.effects.push(this);
|
|
43
|
+
}
|
|
44
|
+
this.runIfDirty();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 暂停效果。
|
|
49
|
+
*
|
|
50
|
+
* 暂停后,当依赖发生变化时不会自动执行。
|
|
51
|
+
*/
|
|
52
|
+
pause()
|
|
53
|
+
{
|
|
54
|
+
this._isEnable = false;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 恢复效果。
|
|
59
|
+
*
|
|
60
|
+
* 恢复后,当依赖发生变化时会自动执行。
|
|
61
|
+
*/
|
|
62
|
+
resume()
|
|
63
|
+
{
|
|
64
|
+
if (this._isEnable) return;
|
|
65
|
+
this._isEnable = true;
|
|
66
|
+
if (EffectReactivity.pausedQueueEffects.has(this))
|
|
67
|
+
{
|
|
68
|
+
EffectReactivity.pausedQueueEffects.delete(this);
|
|
69
|
+
this.trigger();
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 停止效果。
|
|
75
|
+
*
|
|
76
|
+
* 停止后,效果将不再响应依赖的变化。
|
|
77
|
+
*/
|
|
78
|
+
stop()
|
|
79
|
+
{
|
|
80
|
+
this._isEnable = false;
|
|
81
|
+
EffectReactivity.pausedQueueEffects.delete(this);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 触发效果执行。
|
|
86
|
+
*
|
|
87
|
+
* 当依赖发生变化时,会调用此方法。
|
|
88
|
+
*/
|
|
89
|
+
trigger()
|
|
90
|
+
{
|
|
91
|
+
batchRun(() =>
|
|
92
|
+
{
|
|
93
|
+
super.trigger();
|
|
94
|
+
|
|
95
|
+
if (this._isEnable)
|
|
96
|
+
{
|
|
97
|
+
// 合批时需要判断是否已经运行的依赖。
|
|
98
|
+
batch(this, Reactivity.activeReactivity === this);
|
|
99
|
+
}
|
|
100
|
+
else
|
|
101
|
+
{
|
|
102
|
+
EffectReactivity.pausedQueueEffects.add(this);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
private static pausedQueueEffects = new WeakSet<EffectReactivity>();
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 执行当前节点。
|
|
111
|
+
*
|
|
112
|
+
* 当暂停时将会直接执行被包装的函数。
|
|
113
|
+
*/
|
|
114
|
+
run(): void
|
|
115
|
+
{
|
|
116
|
+
if (this._isEnable)
|
|
117
|
+
{
|
|
118
|
+
super.run();
|
|
119
|
+
}
|
|
120
|
+
else
|
|
121
|
+
{
|
|
122
|
+
this._func(this._value);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* 维持反应式效果。
|
|
129
|
+
*/
|
|
130
|
+
export interface Effect
|
|
131
|
+
{
|
|
132
|
+
/**
|
|
133
|
+
* 暂停。
|
|
134
|
+
*/
|
|
135
|
+
pause: () => void;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 恢复。
|
|
139
|
+
*/
|
|
140
|
+
resume: () => void;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* 停止。
|
|
144
|
+
*/
|
|
145
|
+
stop: () => void;
|
|
146
|
+
}
|
|
@@ -0,0 +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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export { batchRun } from './batch';
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
4
|
-
export {
|
|
5
|
-
export { isProxy, isReactive, reactive, type Reactive } from './reactive';
|
|
6
|
-
export {
|
|
7
|
-
export {
|
|
8
|
-
|
|
1
|
+
export { batchRun } from './batch';
|
|
2
|
+
export { computed, type Computed, type ComputedReactivity } from './computed';
|
|
3
|
+
export { effect, EffectReactivity, 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 { forceTrack, noTrack, Reactivity } from './Reactivity';
|
|
7
|
+
export { isRef, ref, RefReactivity, type Ref } from './ref';
|
|
8
|
+
export { toRaw } from './shared/general';
|
|
9
|
+
|