-
Notifications
You must be signed in to change notification settings - Fork 1
/
deps.html
46 lines (44 loc) · 987 Bytes
/
deps.html
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
<!--
* @Description: deps demo
* @Author: peterroe
* @Date: 2022-01-15 16:18:12
* @LastEditors: peterroe
* @LastEditTime: 2022-01-15 16:36:24
-->
<script>
let activeEffect
class Dep {
constructor(value) {
this.subscribes = new Set()
this._value = value
}
get value() {
this.depend()
return this._value
}
set value(newValue) {
this._value = newValue
this.notify()
}
notify() {
this.subscribes.forEach(effect => {
effect()
})
}
depend() {
if (activeEffect) {
this.subscribes.add(activeEffect)
}
}
}
function watchEffect(effect) {
activeEffect = effect
effect()
activeEffect = null
}
const dep = new Dep("hello")
watchEffect(() => {
console.log(dep.value)
})
dep.value = 23
</script>