forked from Code-Pop/vue-3-reactivity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
07-all-together.js
71 lines (61 loc) · 1.58 KB
/
07-all-together.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
59
60
61
62
63
64
65
66
67
68
69
70
71
// targetMap stores the effects that each object should re-run when it's updated
const targetMap = new WeakMap()
function track(target, key) {
console.log(`track: key = ${key}`)
let depsMap = targetMap.get(target)
if (!depsMap) {
depsMap = new Map()
targetMap.set(target, depsMap)
}
let dep = depsMap.get(key)
if (!dep) {
dep = new Set()
depsMap.set(key, dep)
}
dep.add(effect)
}
function trigger(target, key) {
console.log(`trigger: key = ${key}`)
const depsMap = targetMap.get(target)
if (!depsMap) {
return
}
const dep = depsMap.get(key)
if (dep) {
dep.forEach((effect) => effect())
}
}
function reactive(target) {
const handlers = {
get(target, key, receiver) {
const result = Reflect.get(target, key, receiver)
// Track when we accessing the key!
track(target, key)
return result
},
set(target, key, value, receiver) {
const oldValue = target[key]
const result = Reflect.set(target, key, value, receiver)
if (result && oldValue != value) {
// Trigger when the value changes!
trigger(target, key)
}
return result
},
}
return new Proxy(target, handlers)
}
const product = reactive({ price: 5, quantity: 2 })
let total = 0
// problem: how do we make the effects more dynamic?
const effect = () => {
total = product.price * product.quantity
}
effect()
console.log(total)
product.quantity = 3
console.log(total)
product.quantity = 4
console.log(total)
// problem: we only want to track within an effect
// console.log('Updated quantity to =', product.quantity)