feat(watcher): introduce 'final' flush mode #1599
Closed
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.
In this PR the 'final' WatcherOptions flush mode is added.
'final' flush mode executes the watcher callbacks after all other effects (with multiple iterations).
Why is this necessary?
assertion
is executed double (3 times in total because of the initial call).The reason is that value it watches may have inter-dependencies, triggering each other in multi-iteration
flushPostQueue
runs. In every iterationassertion
may be added and invoked.We have a real world use case. We have a component that creates some div elements with scrollbars. You can change the
zoom
value, which in turn changes thevisibleRange
using awatchEffects
. Another part of the application watcheszoom
andvisibleRangeX
(and more) to finally draw stuff in a canvas. We noticed this draw function being called multiple times because of the issue described by @jods4. The draw function is very expensive so we don't want that to happen.Although the recent commit 165068d (see #1595) mitigates the problem somewhat by checking if the effect was still on the queue (and it has not been run yet), it can still go wrong when watchers that have multi-level dependencies, or when the effect was already executed in the pending PostFlushCbs.
This PR addresses this problem by providing a way of postponing the effect until everything else has been processed. This allows you to make sure that an expensive watcher is run only once in multi-iteration flushes.