Vue3 - 源码
yuiyake 7/12/2022
# vue3响应式原理
# 解读
# 手写
let currentEffect;
class Dep {
constructor() {
this.effects = new Set(); // 依赖收集集合,用set防止重复收集
}
// 依赖收集
depend() {
if(currentEffect) this.effects.add(currentEffect);
}
// 派发更新
notice() {
this.effects.forEach(effect => {
effect()
})
}
}
// 建立键与dep的一一对应关系
let targetMap = new Map();
function getDep(target, key) {
let depsMap = targetMap.get(target);
if(!depsMap) {
depsMap = new Map();
targetMap.set(target, depsMap);
}
let dep = depsMap.get(key);
if(!dep){
dep = new Dep();
depsMap.set(key, dep);
}
return dep;
}
// 响应式
function reactivity(obj) {
return new Proxy(obj, {
get(target, key){
const dep = getDep((target, key));
// 依赖收集
dep.depend();
return Reflect.get(target, key);
},
set(target, key, newVal) {
const dep = getDep(target, key);
const ret = Reflect.set(target, key, newVal);
// 设置值之后再去触发更新
dep.notice();
return ret;
}
})
}
// 监听变化
function effectWatch(effect) {
currentEffect = effect;
effect(); // 第一次先执行一次
currentEffect = null
}
// test
let obj = reactivity({
count: 1
})
let b;
effectWatch(() => {
b = obj.count + 1;
console.log(b);
})
obj.count = 2;
obj.count = 3;
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
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