-
Notifications
You must be signed in to change notification settings - Fork 73
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
Ignore undefined values in merge queue when merging objects #339
Ignore undefined values in merge queue when merging objects #339
Conversation
Why would it reset the value? I would think it would just not do anything. |
Ok, if that's the expected behaviour then we can ignore this change. Still, the current implementation isn't 100% correct then. I created a new test, which has a lot of The expected output should be P.S. pls ignore the typo in |
The solution to this would be to simply remove all |
Different question. What if a batch of merge changes contains a |
@tgolen @marcaaron i updated the PR according to my latest proposal |
I personally like this. I don't think we should try to do anything special for handling
|
I forgot to touch on this. I'm glad you found this! I definitely don't think we ever considered undefined values, so I think it's totally possible that there would be unexpected behaviors around this. |
I agree with @tgolen. The test you wrote makes sense given the fact that we allow That said, I'm not sure if we really need to handle a case like this:
It's hard to say what my expectations would be here because I can't really think of why someone would do it. It seems reasonable to maybe end up with |
The implementation i wrote is only about handling top-level For nested objects, nothing changes by this. Actually, @thienlnam recently created an issue here that claims, that
The use case my PR tries to fix is more for merge queues like this: [{a: 1}, {b: 1}, undefined, {c: 1}] where we would expect the |
That's already happening actually, since we ignore all top-level nullish keys in objects when merging. Not part of my changes. 👍 |
@tgolen @marcaaron just resolved merge conflicts! 👍 I think this PR is still valid, as i explained in this comment. This one only handles nullish values that are directly passed to I opened another PR, which addresses the nested nullish values as discussed on Slack. |
@amyevans @marcaaron @tgolen also just added an additional test which checks for |
This is yours now @marcaaron |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If anyone calls merge()
with undefined
ever we should just ignore it. We are filtering out these values, but why let them even enter the mergeQueue at all? 🤔
I think it's a bit simpler to just do:
diff --git a/lib/Onyx.js b/lib/Onyx.js
index d4d329e..c9f8311 100644
--- a/lib/Onyx.js
+++ b/lib/Onyx.js
@@ -1120,6 +1120,10 @@ function applyMerge(existingValue, changes) {
* @returns {Promise}
*/
function merge(key, changes) {
+ if (_.isUndefined(changes)) {
+ return Promise.resolve();
+ }
Yes, you're right. The only thing i changed is that we need to return the current |
Can we merge this already? 🚀 |
@marcaaron want a final look? |
@tgolen @marcaaron
Moving code from this PR as it became kind of a scope creep.
Details
This PR intends to improve how we merge a batch of changes which includes nullish values and objects.
Notice: This PR does not handle nullish values in nested objects, there's another PR for this change.
In general, we have the following possible values that are passed directly to
Onyx.merge
:object
: Merges the existing value with the merge changesarrays
: Just replaces the existing valueundefined
: Also replaces the existing valuenull
: Will remove the key from storageWhen we call
Onyx.merge
multiple times with different types of data, we can eventually get a batch of changes like this:With the current implementation of
applyMerge
,batchedChanges
would evaluate to{a: 1, b: 2, c: 3, d: 5, e: 6}
. This behaviour ignores the fact, that we had anundefined
change in the merge queue, which should basically reset the existing value.Therefore, we need to check if the
mergeQueue
contains any nullish value, and if so, we have to ignore theexistingValue
in the process of merging the delta changes.Related Issues
Automated Tests
Linked PRs