export default class Dep{
static targe?:Watcher;
subs:Array<Watcher>;
constructor {
this.subs = []
}
addSub (sub: Watcher) {
this.subs.push(sub)
}
removeSub (sub: Watcher) {
remove(this.subs, sub)
}
depend () {
if (Dep.target) {
Dep.target.addDep(this)
}
}
notify () {
const subs = this.subs.slice()
for (let i = 0, l = subs.length; i < l; i++) {
subs[i].update()
}
}
}
Dep.target = null
export default defineReactive(
obj:Object,
key:String,
val:any
){
const dep = new Dep()
Object.defineProperties(obj,key,{
enumerable:true,
configurable:true,
get:function reactiveGetter(){
if(dep.target){
dep.depend()
}
},
set:function reactiveSetter(newVal){
val = newVal
dep.notify()
}
})
}
console