-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
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
onScopeDispose
is not called inside not immediate watchers
#12681
Comments
I don't have a specific solution for this but yes hopefully a debugging hint. The immediate watcher is triggered even before the component is created. After that, the component is created. Once that happens, the non-immedate watcher is triggered. I assume that the fact this happens later affects the onScopeDispose actual scope, maybe focusing on the watcher itself rather than the component? Here's how I tried this out: play.vuejs Update: I've tried to handle scopes explicitly with effectScope, but the results are exactly the same. The first time the immediate watcher runs it does get the scope. However, the second time (the mounting and creating of the component are done) it does not get the scope. Either there's something I'm missing about Vue's scoping (which is very possible), or there's probably an actual issue/bug going on here. |
Interestingly enough, the callback runs on the |
I may have misunderstood, but... This all appears to be working correctly to me. I don't think
So for example, consider this code: watch(source, cb)
onUnmounted(doTeardown) This will only work inside a component, because watch(source, cb)
onScopeDispose(doTeardown) This can now be used inside any effect scope, not just inside a component, allowing The Much like with |
@skirtles-code I initially found this for some VueUse composables (specifically You're assuming all the logic will be bound to a component, but some composables might be intended to be used in both places. A good workaround would be to PR in VueUse and add, alongside their Then, it's the problem of making users aware of this. How an user is supposed to guess that Let me flip your question: why then not make There is also another case (through I agree is an edge one): I might be creating a different effect scope inside a component and dispose it in a different moment, which has nothing to do with when the component is unmounted. There's also another problem: the inconsistency between both watchers. Users pulling TLDR: Although it may seem silly, the playground is just a simple reproduction of the issue, but out there there are legitimate use cases for this! |
I agree with @skirtles-code. In the DEV mode, there are warnings. see Playground DEV mode, the warning will inform the user that this usage will not work. |
@edison1105 @skirtles-code Is there any reason/something I'm missing of why the watcher's callback doesn't run inside the effect scope of the component by default? Wouldn't wrapping all the calls to the callback like this:
Be a good solution? And yes, a warning is printed in DEV (sorry I didn't notice that, definitely good to know!) |
There is no need to track dependencies inside watch callback calls, so the watcher's callback doesn't run inside an effect scope. |
Vue version
3.5.13 (also tested on 22dcbf3 in Playground)
Link to minimal reproduction
https://play.vuejs.org/#__PROD__eNqNVE1vGjEQ/SuDLxBpyyptT4hETVOkplI/RJB68WVjhsWJ17bWNqFC/PeObb4LUW/rmTdv5o3fesXurO0vArIBGzrRSuvBoQ/2lmvZWNN6uDeNvYZZaxro9st0igXdHWAFbl4pZV7HOIP1BpkRXAujnYfGBO1xCjcH0J5vA15xPSxzX+pIB4+NVZVHOgEMn4L3RsMnoaR4ueFsz9PZfHJ2OzF1rTD3GJa5JJfn0Rfv5GxfyxmUlB2WB51YwbyjSWey7j87o2kZq0jAmSAGqbD9ab0kJZwNIGViLgn5lmJRSrGNizmKlzPxZ7eMMc5+teiwXSBnu5yv2hp9To8ef+CSvnfJxkyDIvQbyTE6o0KcMcM+Bz2lsQ9wadqHdGNS1xM3WnrUbisqDhqR64TnjK4vLu+S9P24H/ofUx3Xa9rizh2X7bQCox+FsfhFOmscFvBaeTEvDpxRAJHfh7ZF7RP0MPBAfqq0wAtOc7LWlTo22qxSLjqN69SqlzEF9K7g5jaLisVGYV+Zutf9fTe5/zoaw/UAxkF3ulQKJ1P3cu25Mqo6xnaviGCd+pclTObSEQBBKKx0sI5ISJjw6g88oaiCQ5Ae5pUD2TQ4lWTR7f3k8XPrZK1DCVlVf1GpgKR/e6Nnlb0fQGUtyO0uZ/RvTLsFdDr/LroXx7/MIzIa6P+xG6LOCVNaxobmv9dI1OfWWJCBTvYCcbdvvSJTuUgfAN83b0CMljl8/BCs/wIi+bb3
Steps to reproduce
immediate
"watcher 2" runs and watcher 1 does the same afterwards (because watcher 2 changes the variable it depends)onScopeDispose
from watcher 2 runs, but not the one from watcher 1What is expected?
Both
onScopeDispose
callbacks are fired properly and both messages are printed to the console, regardless theimmediate
status of the watcherWhat is actually happening?
The
onScopeDispose
is properly fired on bothSystem Info
No response
Any additional comments?
No response
The text was updated successfully, but these errors were encountered: