Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

第4章 相应系统的作用与实现 70页代码track(obj,'value')执行有问题 #10

Open
yztjoin opened this issue Feb 9, 2023 · 0 comments

Comments

@yztjoin
Copy link

yztjoin commented Feb 9, 2023

function computed(getter) {
// 增加diry 脏值检查 用于判断是否更新数据
let dirty = true
let value
// 把getter作为副作用函数,创建一个lazy的effect
const effectFn = effect(getter, {
lazy: true,
scheduler() {
dirty = true
trigger(obj, 'value')
},
})
const obj = {
// 当读取的时候才执行effectFn
get value() {
if (dirty) {
value = effectFn()
dirty = false
// 我觉的应该放在这里,在有脏值的时候才track
}
/**
第一次执行没有问题,再次获取sumRes.value的值会报错
TypeError: Cannot read properties of undefined (reading 'deps')
**/
track(obj, 'value')
return value
},
}
return obj
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant