Skip to content

feat(watchClone): add watchClone #4794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

18845778092
Copy link

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.
⚠️ Slowing down new functions

Warning: Slowing down new functions

As the VueUse audience continues to grow, we have been inundated with an overwhelming number of feature requests and pull requests. As a result, maintaining the project has become increasingly challenging and has stretched our capacity to its limits. As such, in the near future, we may need to slow down our acceptance of new features and prioritize the stability and quality of existing functions. Please note that new features for VueUse may not be accepted at this time. If you have any new ideas, we suggest that you first incorporate them into your own codebase, iterate on them to suit your needs, and assess their generalizability. If you strongly believe that your ideas are beneficial to the community, you may submit a pull request along with your use cases, and we would be happy to review and discuss them. Thank you for your understanding.


Description

This PR add watchClone, provide a cloned old value while keeping the new value isolated when watching an object.

Additional context

  • Test coverage of some simple scenarios
  • TypeScript support with proper type definitions
  • Documentation with real-world usage examples

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request new function labels Jun 3, 2025
Comment on lines 18 to 20
} }, {
deep: true
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does ref() has this option? 👀

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I made a mistake. Ha ha

@OrbisK
Copy link
Collaborator

OrbisK commented Jun 4, 2025

Thank you for your PR. I am not sure about the use cases. Could you please provide one?

@18845778092
Copy link
Author

Thank you for your PR. I am not sure about the use cases. Could you please provide one?

emm...because the watch api provided by vue doesn't support get the old value before the reactivity object changed, I want to provide a watch wrapper that can get the old value😀.

@18845778092
Copy link
Author

https://play.vuejs.org/#eNp9kk9P4zAQxb/KrC8tUppol91L1aLdRUjAARAguFhCIZmmBse27ElaVPW7M3b4UxDiECme98bzm5dsxD/n8r5DMRWzUHnlCAJS5w6kUa2znmDjcZHBqqRquYWFty2M2D+SprImELShgTmwZzw6Rq0t3Fqv6x+jPWmkSV1jtmRjg6ubDKyub/bmBxtpAGK/1Zhr24w37/KWO+MzKwYeJuEDYet0SZhOADNlXEfQT1pbo55LwTOkgILlWbHjFZkY1pi0pcsfgjW8aJouX4QgxRRSJdZcScvJvbergF4tnqImxZLIhWlRVLXhK3ie6n1ukArj2uJTx984OFChTI1rdksRr97ySoxCgXdeqOYTSGVbpzT6c0eKM/kAVHKkq9NUI99h9lqvllg9flF/COsB+sIjE/UoxZtGpW+QBvno6gzX/P4mcpCdZvc34iXy9+oi42D735masXd8ifYk5apMcx2O1oQmvC4VQVMayS8F/0aH36z+jruf/95J8a5HH+/kAPfzP/nPX2L7DCj47nU=https://play.vuejs.org/#eNp9kk9P4zAQxb/KrC8tUppol91L1aLdRUjAARAguFhCIZmmBse27ElaVPW7M3b4UxDiECme98bzm5dsxD/n8r5DMRWzUHnlCAJS5w6kUa2znmDjcZHBqqRquYWFty2M2D+SprImELShgTmwZzw6Rq0t3Fqv6x+jPWmkSV1jtmRjg6ubDKyub/bmBxtpAGK/1Zhr24w37/KWO+MzKwYeJuEDYet0SZhOADNlXEfQT1pbo55LwTOkgILlWbHjFZ ky1pi0pcsfgjW8aJouX4QgxRRSJdZcScvJvbergF4tnqImxZLIhWlRVLXhK3ie6n1ukArj2uJTx984OFChTI1rdksRr97ySoxCgXdeqOYTSGVbpzT6c0eKM/kAVHKkq9NUI99h9lqvl lg9flF/COsB+sIjE/UoxZtGpW+QBvno6gzX/P4mcpCdZvc34iXy9+oi42D735masXd8if Yk5apMcx2O1oQmvC4VQVMayS8F/0aH36z+jruf/95J8a5HH+/kAPfzP/nPX2L7DCj47nU=

image

what am I missing?我错过了什么?

I have modified your example, please take a look at it😀

https://play.vuejs.org/#eNp9UsFu1DAQ/RXLl92V0kRQuETZVaHqAQ6AAPVkCbnJbOLWsS17kg1a7b8zjtk2raqeEs978+bNs4/8k3P5OAAveRVqrxyyADi4nTCqd9YjO3rYZ+wgse5ObO9tz1bEXwlTWxOQ9aFlW0ac9VEYxozsoWSrSUmrFZFOG2GEmbvXRM3WBg63GbO6ud1sd3NL1LEacm3bJUq9GZsJDYArGfoBznppdCdNo+GaPi2Qh/WGbXepgyblo9QD5NEPYatOdjLaEaYq0p60IR0QeqclwnxirLobEK1hV7VW9cNW8OUMwXd1GhbVqiJxqbMqFjI84ym5i166/D5YQ9nOrsR/IAheJp+x5iR2F3feHgJ4tf8bMRqL6EJZFHVjSKIBrUafG8DCuL540XEVBwcslGlgIrbgUZo2PZEVDJTVXrUvjNS2d0qD/+5QUZbPDEmt7eHrXIuZZ+d63UH98Er9PkzJ9A8P5GiknB4xlL4FTPDNr28w0f8j2Ntm0MR+A/wJ9DSG6DHRPg/xOvyCN7v9MueqTPs73EwIJpyXSo+G0pj5gtPLvX5j9Se7l/mHRYp/RvBRkwK8zD/m797z0z8mURaD

image

@OrbisK
Copy link
Collaborator

OrbisK commented Jun 4, 2025

Okay, I see your problem now. But I think there is an easy workaround for this:

//                vvv clone here 
watch(()=>({...msg.value}),(newV, oldV)=>{
  console.log(newV, oldV)
}, {
  deep: true
})

@18845778092
Copy link
Author

Okay, I see your problem now. But I think there is an easy workaround for this:

//                vvv clone here 
watch(()=>({...msg.value}),(newV, oldV)=>{
  console.log(newV, oldV)
}, {
  deep: true
})

Yours is nice, too, but wouldn't it be nice to add a wrapper like this?. Ha ha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request new function size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants