class RefImpl {
_value
subs = new Set<() => void>()
constructor(value: any) {
this._value = value
}
get value() {
if (activeSub) {
this.subs.add(activeSub)
}
return this._value
}
set value(newValue) {
this._value = newValue
this.subs.forEach((fn) => fn())
}
}
function ref(value: any) {
return new RefImpl(value)
}
let activeSub: undefined | (() => void)
function effect(fn: () => void) {
activeSub = fn
fn()
activeSub = undefined
}
const count = ref(0)
effect(() => {
console.log('count value ==>> ', count.value)
})
setTimeout(() => {
count.value++
}, 500)