effectScope解析
# 概述
EffectScope
是Vue3中一个响应式系统的辅助类,用于管理响应式效果的作用域。它可以帮助我们更好地组织和管理响应式效果,避免了全局状态的污染和管理的复杂性。
每一个vue组件的实例上都会挂载一个EffectScope
的实例scope
,该挂载动作会在createComponentInstance
中进行。在组件被激活挂载时,会调用scope.on()
方法,将当前作用域设置为活动作用域;而在组件被卸载或者销毁时,会调用scope.stop()
方法。
# 源码解析
let activeEffectScope;
class EffectScope{
constructor(detached=false){
this.detached = detached;
this._active = true;
this._on = 0;
this.effects = [];
this.cleanups = [];
this._isPaused = false;
this.parent = undefined;
this.scopes = undefined;
this.index = undefined;
this.prevScope = undefined;
this.parent = activeEffectScope;
if(!detached && activeEffectScope){
this.index = (activeEffectScope.scopes || (activeEffectScope.scopes = [])).push(
this
) - 1;
}
}
pause() {
if (this._active) {
this._isPaused = true;
let i, l;
if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].pause();
}
}
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].pause();
}
}
}
resume() {
if (this._active) {
if (this._isPaused) {
this._isPaused = false;
let i, l;
if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].resume();
}
}
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].resume();
}
}
}
}
run(fn) {
if (this._active) {
const currentEffectScope = activeEffectScope;
try {
activeEffectScope = this;
return fn();
} finally {
activeEffectScope = currentEffectScope;
}
}
}
on() {
if (++this._on === 1) {
this.prevScope = activeEffectScope;
activeEffectScope = this;
}
}
off() {
if (this._on > 0 && --this._on === 0) {
activeEffectScope = this.prevScope;
this.prevScope = void 0;
}
}
stop(fromParent) {
if (this._active) {
this._active = false;
let i, l;
for (i = 0, l = this.effects.length; i < l; i++) {
this.effects[i].stop();
}
this.effects.length = 0;
for (i = 0, l = this.cleanups.length; i < l; i++) {
this.cleanups[i]();
}
this.cleanups.length = 0;
if (this.scopes) {
for (i = 0, l = this.scopes.length; i < l; i++) {
this.scopes[i].stop(true);
}
this.scopes.length = 0;
}
if (!this.detached && this.parent && !fromParent) {
const last = this.parent.scopes.pop();
if (last && last !== this) {
this.parent.scopes[this.index] = last;
last.index = this.index;
}
}
this.parent = void 0;
}
}
}
1
2
3
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
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
2
3
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
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
# 辅助方法
effectScope
function effectScope(detached) {
return new EffectScope(detached);
}
1
2
3
2
3
getCurrentScope
function getCurrentScope() {
return activeEffectScope;
}
1
2
3
2
3
onScopeDispose
function onScopeDispose(fn, failSilently = false) {
if (activeEffectScope) {
activeEffectScope.cleanups.push(fn);
}
}
1
2
3
4
5
2
3
4
5
编辑 (opens new window)
上次更新: 2025/08/26, 10:34:53