fix(reactivity): fix WATCH_ARRAY v2 compat deep watching elements (fix #11872) #12236
+39
−9
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.
close #11872
This makes a minor change that brings the
WATCH_ARRAY
compat behavior further in line with the Vue 2 behavior.Currently, watching an array with
WATCH_ARRAY
compat enabled will trigger the watch function on deep mutation of elements. This differs from the Vue 2 behavior, where the watch would trigger when array elements were added/removed/replaced, but not mutated.Changes:
isArray
+isCompatEnabled
check that was done before wrapping the getter, for a couple reasons:watch
anyway, so this pre-check is redundant.options.deep = true
based on the initial value would lead to incorrect behavior if the initial value was an array but a future value wasn't.I couldn't find a way to test this change directly, however in an internal project an identical change using patch-package is working beautifully.
Also as a proof of concept that depth=1 would work I forked the reproduction example from #11872, disabled the
WATCH_ARRAY
compat, and set the watch to usedeep: 1
: https://stackblitz.com/edit/vitejs-vite-qarbhv?file=src%2FApp.vue