Skip to content

fix(useRefHistory): clone lastRawValue for shouldCommit comparison - #5497

Open
meta-syntax wants to merge 3 commits into
vueuse:mainfrom
meta-syntax:fix/use-ref-history-should-commit-clone
Open

fix(useRefHistory): clone lastRawValue for shouldCommit comparison#5497
meta-syntax wants to merge 3 commits into
vueuse:mainfrom
meta-syntax:fix/use-ref-history-should-commit-clone

Conversation

@meta-syntax

Copy link
Copy Markdown
Contributor

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.
  • Confirm that this PR is based on your own understanding and review of the project, not solely generated or summarized by AI tools.

Note: If this PR is deemed to be primarily generated by AI tools, it may be closed.

⚠️ 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

Fixes #4985

shouldCommit receives same object reference for both oldValue and newValue when deep: true.
This is because lastRawValue holds direct reference to source.value.

Clone lastRawValue at 3 assignment sites using same clone logic as useManualRefHistory.

Added tests for shouldCommit + deep: true with object values (both sync and pre flush).

Additional context

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jun 7, 2026
Comment thread packages/core/useRefHistory/index.ts Outdated
@@ -143,7 +150,7 @@ export function useRefHistory<Raw, Serialized = Raw>(
if (!shouldCommit(lastRawValue, source.value))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

maybe we should also clone the value here?

@meta-syntax meta-syntax Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the review.
I agreed that it should be cloned for consistency. Updated.

@meta-syntax
meta-syntax force-pushed the fix/use-ref-history-should-commit-clone branch from 4de702f to ec0f4a5 Compare June 18, 2026 00:51
@meta-syntax
meta-syntax requested a review from OrbisK June 18, 2026 00:59
Comment thread packages/core/useRefHistory/index.ts Outdated
Comment on lines +143 to +153
if (!shouldCommit(lastRawValue, source.value))
if (!shouldCommit(lastRawValue, _cloneRaw(source.value)))
return

lastRawValue = source.value
lastRawValue = _cloneRaw(source.value)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we should clone it only once here. Clones can be costly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching this. Now cloning once and reusing it for both the shouldCommit check and the assignment.

@meta-syntax
meta-syntax requested a review from OrbisK June 18, 2026 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useRefHistory | Use the clone function to set lastRawValueto freeze it

2 participants