[RFC] Control watch observation depth #597
Replies: 3 comments 6 replies
-
This is a feature idea I've pondered previously. I think the core idea of supporting depth is a good one. It'd be particularly useful for watching changes to an array without watching objects inside the array. Initially, I also thought that having For example: watch(..., {
depth: 1
}) The above would be equivalent to: watch(..., {
deep: true,
depth: 1
}) If the user explicitly passes watch(..., {
deep: false,
depth: 1 // The depth will be ignored as deep is false
}) I'm unclear whether There are a few reasons why I think a separate setting might be better:
For points 2 and 3 above, I'm not imagining somebody passing a literal number like |
Beta Was this translation helpful? Give feedback.
-
After discussion, a new option, |
Beta Was this translation helpful? Give feedback.
-
I'm looking forward to this. But my use case no.1 for this would be to watch for array mutations. Because we have to use deep watchers for that, and it always felt wrong to watch the entire thing when the elements are very complex objects. I imagine it would be watch-level const array = ref([]);
watch(array, callback, { deep: true, depth: 0 }); Will this trigger the callback when I do the following operations? array.value.push({ foo: 'test' });
array.value[0] = { foo: 'test2' };
array.value.splice(0, 1); And this: array.value[0].foo = 'test3'; will not trigger it, unless I set it to |
Beta Was this translation helpful? Give feedback.
-
Start Date: 2023-11-08
Target Major Version: 3.x
Implementation PR: vuejs/core#9572
Summary
Support passing watch deep option as a number type, allowing control over the depth of the watch. This helps prevent unnecessary monitoring, especially when dealing with large arrays or objects.
Basic example
Result playground
Motivation
When using the
watch
function to observe arrays or objects, enabling thedeep
option is necessary for monitoring deeper levels of the object. However, this approach can lead to performance issues since it requires recursive access to all properties of the object. In such cases, thedepth
option can be employed to control the monitoring depth, thereby avoiding unnecessary observations.Unresolved questions
Welcome to discussion
Beta Was this translation helpful? Give feedback.
All reactions