Skip to content

fix: ensure tracking is paused when emit() calls handler so it can safely be called in effects (fix #6669) #6688

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

LinusBorg
Copy link
Member

fix: #6669

If emit() is called in an effect (i.e. the callback of a watchEffect), it will collect any reactive values accessed in that parent handle as dependencies of the current component.

This can lead to a recursive loop crashing the app.

@LinusBorg LinusBorg force-pushed the linusborg/pause-tracking-in-emit-6669 branch from 09f17b4 to 1c02e5a Compare September 17, 2022 10:00
@edison1105
Copy link
Member

I feel this PR is necessary though ##6669 is closed.

@LinusBorg
Copy link
Member Author

@yyx990803 You closed #6669 as "not a bug", so I assume my PR can be closed?

@skirtles-code
Copy link
Contributor

skirtles-code commented May 31, 2024

Just in case this does get merged, I think the same change would need to be applied to the onceHandler a bit further down.


As for whether it should be merged, I'm very much on the fence.

I think it's an easy problem for people to fall into without really understanding what's going on.

The exact timing of operations in Vue is not generally well understood by users. Various things are sensitive to timing and it usually 'just works' without needing to think about it.

I don't think it's necessarily obvious that emit runs code synchronously in the parent. I've encountered a lot of people who seem to think it's asynchronous. Why? Because the child emits an update event and the child's props don't synchronously update. They conclude that it's because the 'message' (event) is delivered asynchronously to the parent.

Even if someone isn't thinking about it in quite those terms, I still think events are perceived as somehow different from normal function calls, like throwing a parcel over a wall and not having to worry about what's on the other side.

But, I can also see Evan's perspective for closing the original issue.


Update: A few days after I posted this, we had another conversation about the synchronicity of emit on Vue Land. Link. On this occasion, two participants in the conversation believed that emit is not synchronous, Quote: emit is fired on the microtask. Perhaps this needs to be addressed more explicitly in the docs?

@edison1105 edison1105 closed this Oct 21, 2024
@edison1105 edison1105 deleted the linusborg/pause-tracking-in-emit-6669 branch October 21, 2024 07:37
@edison1105 edison1105 restored the linusborg/pause-tracking-in-emit-6669 branch October 21, 2024 08:22
@edison1105 edison1105 reopened this Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Needs Review
Development

Successfully merging this pull request may close these issues.

watchEffect should not have side effects on the response in emits
3 participants