feat(apiWatch): support passing the `number` type for `deep` to control the watch depth
RFC: https://github.com/vuejs/rfcs/discussions/597
fixed #9916
Support passing watch deep option as a number type, indicating the depth to which the object should be monitored. This allows control over the depth of object access in the watch function. In scenarios with large datasets, this can significantly improve performance.
Size Report
Bundles
| File | Size | Gzip | Brotli |
|---|---|---|---|
| runtime-dom.global.prod.js | 95.4 kB (+5.24 kB) | 36.4 kB (+1.82 kB) | 32.7 kB (+1.6 kB) |
| vue.global.prod.js | 153 kB (+6.21 kB) | 56.1 kB (+2.09 kB) | 50 kB (+1.94 kB) |
Usages
| Name | Size | Gzip | Brotli |
|---|---|---|---|
| createApp | 53 kB (+3.35 kB) | 20.6 kB (+1.11 kB) | 18.8 kB (+988 B) |
| createSSRApp | 56.9 kB (+3.73 kB) | 22.2 kB (+1.26 kB) | 20.2 kB (+1.12 kB) |
| defineCustomElement | 55.3 kB (+3.35 kB) | 21.4 kB (+1.15 kB) | 19.5 kB (+995 B) |
| overall | 66.5 kB (+3.4 kB) | 25.6 kB (+1.13 kB) | 23.3 kB (+1.07 kB) |
Now have some conflicts after #9928
Also, I think it might be more concise to simply allow passing a number to deep, since the presence of depth implies deep: true:
// infinite depth
watch(src, () => {}, { deep: true })
// explicit depth
watch(src, () => {}, { deep: 1 })
Due to the consideration that users might have previously passed "deep" as a number in their usage, which was mistakenly treated as true in the previous implementation, if we now interpret "deep number" as referring to depth, the earlier practice of passing "deep" as a number would result in inconsistency. Of course, such usage was incorrect in the past. Should we take this into account? If we choose not to consider this, recognizing "deep" as referring to depth would be a more reasonable design approach.
That's a good point. I think we can add a warning in a 3.4 patch that checks for deep with number values and warn users that it will be treated as depth in a future version.
Agreed. It's a good plan.
This feature is very useful for me. awesome!
I don't want to annoy you guys, but I'm curious, since 3.5 already has some alpha releases.. is there still a chance this goes into 3.5? I'd love to use this feature 😀