-
Notifications
You must be signed in to change notification settings - Fork 915
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
《Vue.js设计与实现》第4章 响应系统的作用与实现 4.5小节 嵌套的effect与effect栈 #329
Comments
@wangnan0916 我照着书上敲 activeEffect 和effectStack 的实现 咋和你这个不一样? |
嵌套这里确实有bug,外层更新数据时,会触发内层重新执行effect函数,这时内层的cleanup(effectFn),并不会清掉上一次绑定副作用函数 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
在进行嵌套定义effect时,如果多次修改外部effect中使用的响应式数据,会间接调用内部effect。
effect(fn)
会将fn
包装为effectFn
obj.foo
进行重新绑定时,会导致内部的依赖再次绑定bucket
的数据结构中的Set无法对effectFn
进行去重,也就导致注册多个重复的依赖。程序的输出结果为:
在对内部的响应式数据进行修改时,触发了三个
effectFn
,其中有初始化的1个,两次对obj.foo
进行修改时,间接添加的effectFn
。在我个人看来,应该只需要执行一个
effectFn
,而不是三个,不知道是本意如此,还是意外的BUG。如果是BUG,我想到了一个不是很好解决的方法:通过将函数转为string来判断是否为相同的函数。例如:
涉及修改的代码如下:
以上是个人看法,如有错误,还请霍老师指正😳。
The text was updated successfully, but these errors were encountered: