distinctUntilChanged with objects #7314
turanofff
started this conversation in
Ideas / Feature request
Replies: 2 comments 2 replies
-
It would be great to hear why or why not the idea doesn't make sense @demensky or @voliva? |
Beta Was this translation helpful? Give feedback.
2 replies
-
Thanks for the answers! It now makes perfect sense |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I noticed that
distinctUntilChanged
operator provides default comparator which only works with primitives.Official documentation https://rxjs.dev/api/index/function/distinctUntilChanged does not seem to outline the fact that this approach won't work when Observable emits objects. It will only compare object references. Given that the best practice is always emit new object and not mutate it,
distinctUntilChanged
would become useless if custom comparator is not provided in this case. Additionally, I think it is counterintuitive for some developers who didn't pay too much attention to how this operator works.I also understand that there is
distinctUntilKeyChanged
operator to work with objects, though it would only work if there's a need to compare objects based on specific key.Based on all mentioned above I have two possible solutions to improve the situation:
a. May use something like
fast-deep-equal
to compare previous value with the current one.b. If it is undesired to use a 3rd party library, then I think it would be possible to write a method to compare objects in scope of the library.
I would be happy to contribute in order to improve the situation.
Beta Was this translation helpful? Give feedback.
All reactions