Fix #10185, Re-evaluate effect immediately until it doesn't re-trigger itself #10189
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a proposal, or at least something to think about.
The improved reactivity system now evaluates computeds lazily. This caused issues when the computed re-triggered itself (in)directly. See #10185
Although I can follow the idea that direct recursive re-triggering is an antipattern, we must support this situation. Re-triggering may happen indirectly and in a not-so-obvious way to the user. Think of a sync watchers for example, which act immediately upon the change of a computed and may trigger other changes outside of the scope of the original computed. I think that we all agree that we need to support this.
The current solution leaves the computed 'dirty' after returning a value to the invoker (in the case of a recursive trigger). This value is already 'old' when it is being used. I think it would be more sane to re-evaluate the computed until it is no longer dirty, then return that value. This prevents the problems that have been found (so far), because, after executing the effect, it can never be dirty.
In the case of infinite recursion, we could detect this after X iterations mark the effect as
NotDirty
as normal but issue a warning.I don't know if this is a better approach than fixing this issues one by one, as in the PR #10187
The change did have impact on the unit tests, but nothing unexpected. The unit tests have been updated in this PR.
The PR may impact current deployments using vue, because recursion is handled differently and effects may be triggered in a different order. I tested it on our own application, which makes complex use of reactivity. I found that we received one warning for infinite recursion, which is most likely true positives that we should take a look at. Other than that everything seemed to work perfectly as before.
@johnsoncodehk @Doctor-wu @yyx990803 any considerations on this approach?