-
Notifications
You must be signed in to change notification settings - Fork 0
/
v-model.js
58 lines (45 loc) · 982 Bytes
/
v-model.js
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
let activeEffect;
const effect = function(effectFn, ...args) {
activeEffect = effect;
let res = effectFn.apply(null, args);
return res;
};
let obj = {
message: "hello",
count: 0,
};
const map = new WeakMap();
const proxy = new Proxy(obj, {
get(t, k, r) {
let targetMap = map.get(target);
if (!targetMap) {
map.set(target, new Map());
}
let dep = targetMap.get(k);
if (!dep) {
targetMap.set(k, new Set());
}
if (activeEffect) {
dep.add(k, activeEffect);
}
if (k in t) {
return t;
}
},
set(t, k, r) {
let target = map.get(t);
let dep = target.get(k);
[...dep].forEach((cb) => {
cb();
});
return true;
},
});
function hello(obj) {
console.log(obj.message);
console.log(obj.count);
}
effect(hello, obj);
setTimeout(() => {
obj.count++;
}, 1000);