feat: Use hash of reactive value object to determine if it has changed #1428
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Starting a PR to help the discussion we've had around
reactive.value
objects and mutability.Before - Reactivity with mutable objects
The key problem can be seen in the example from Reactivity | Mutable objects. In this example, currently, even if you call
.set()
on a reactive object, if the object was mutated in place.set()
doesn't invalidate reactivity because the new value and the current value are already the same.The best approach for updating the reactive value is to create a new instance of the reactive value, e.g.
This works well for lists -- once you become aware of the problem, of course -- and dictionaries, but is easy to run into with other mutable objects that might be harder to copy.
After -- Compare new (mutable object) values by hash
This PR uses xxHash to hash mutable objects to then compare the new value with the hash of the old value when
.set()
was last called. After this change the example above works as most people would expect.