vueuse icon indicating copy to clipboard operation
vueuse copied to clipboard

feat(watchClone): add watchClone

Open xiaoliyooo opened this issue 8 months ago • 8 comments

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

xiaoliyooo avatar Jun 03 '25 15:06 xiaoliyooo

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

OrbisK avatar Jun 04 '25 08:06 OrbisK

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😀.

xiaoliyooo avatar Jun 04 '25 09:06 xiaoliyooo

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=

image

what am I missing?

OrbisK avatar Jun 04 '25 09:06 OrbisK

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

xiaoliyooo avatar Jun 04 '25 09:06 xiaoliyooo

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
})

OrbisK avatar Jun 04 '25 11:06 OrbisK

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

xiaoliyooo avatar Jun 04 '25 11:06 xiaoliyooo

Okay. I think we can still improve your implementation a little:

Proof of concept: https://playground.vueuse.org/?vueuse=13.3.0#eNqFU8tu2zAQ/JUFL7IBVT60J9c22gY5tIe2aIuceFGltaKEIgk+bBcC/71LUnIUIEjgi7kzw53ZpUb2Wevq5JFt2c42ptcOLDqvQdSy23PmLGcHLvtBK+NgBG/xRiiJLQQ4GjVA8YnUVN00ymCxYJ5r19yXYPB4pRKz+Mgll42S1oHDQYvaIewjazUSy/duC8XfWtKvKKE1vXykAt2FpoCwjuKjl43rlcwdspuVVd40WILSEbJ05ZjgH/m8hTGENYxcAuTmz+ASqqoyaF0g4XTFggrNHHn/NIBry6hLxoD+Om8mY6vVen/IwupUC0/MlcTzHXkU7R1hyUzuoQRWQnVLPIKhzFdNLqkWYp9l7nmGhO02eYG0LjrMAJ0Adr3U3sHp3aBaFHGtE1qlkXMG7p/GVL/Q6Q1N2soLmqTSBg/jCOPMDhDCbhOrydZm4YuV9Lwo/rHvqgerJL3BNBPOGjXoXqCZgnNG+8vT4qwWQp2/pZozNNW53txj8/hC/cFeYo2zn7QoNCfk7Iq52nRI5iN8+/t7CnIFKbcXxH4F/IW0Oh89ZtoXL1uyveAlt1/TN9HL7o+9vTiUdg4VjeZF59z0gdy8Ev3J7vvqQ9LRi2DhP1xzS/g=

Like this we are much more flexible, and can reuse existing composalbes.

OrbisK avatar Jun 04 '25 11:06 OrbisK

Okay. I think we can still improve your implementation a little:

Proof of concept: https://playground.vueuse.org/?vueuse=13.3.0#eNqFU8tu2zAQ/JUFL7IBVT60J9c22gY5tIe2aIuceFGltaKEIgk+bBcC/71LUnIUIEjgi7kzw53ZpUb2Wevq5JFt2c42ptcOLDqvQdSy23PmLGcHLvtBK+NgBG/xRiiJLQQ4GjVA8YnUVN00ymCxYJ5r19yXYPB4pRKz+Mgll42S1oHDQYvaIewjazUSy/duC8XfWtKvKKE1vXykAt2FpoCwjuKjl43rlcwdspuVVd40WILSEbJ05ZjgH/m8hTGENYxcAuTmz+ASqqoyaF0g4XTFggrNHHn/NIBry6hLxoD+Om8mY6vVen/IwupUC0/MlcTzHXkU7R1hyUzuoQRWQnVLPIKhzFdNLqkWYp9l7nmGhO02eYG0LjrMAJ0Adr3U3sHp3aBaFHGtE1qlkXMG7p/GVL/Q6Q1N2soLmqTSBg/jCOPMDhDCbhOrydZm4YuV9Lwo/rHvqgerJL3BNBPOGjXoXqCZgnNG+8vT4qwWQp2/pZozNNW53txj8/hC/cFeYo2zn7QoNCfk7Iq52nRI5iN8+/t7CnIFKbcXxH4F/IW0Oh89ZtoXL1uyveAlt1/TN9HL7o+9vTiUdg4VjeZF59z0gdy8Ev3J7vvqQ9LRi2DhP1xzS/g=

Like this we are much more flexible, and can reuse existing composalbes.

I took your advice and refactored it and the code is much clearer, thanks!

xiaoliyooo avatar Jun 04 '25 13:06 xiaoliyooo