Make Onyx.getAllKeys public#94
Conversation
|
CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
Hmm can you maybe explain what you are trying to do exactly? If we want to remove an item from a collection we can remove the key for that item from Onyx. Collections are aggregates of many individual keys. |
| } | ||
|
|
||
| // If the flag is set, clear any removed keys in the collection | ||
| if (clearRemoved) { |
There was a problem hiding this comment.
I'm not sure I agree with adding this functionality to mergeCollection().
In Onyx, "merge" is supposed to merge a new value (usually an object) into an older one. If the new value is missing a key that the old value has I wouldn't expect that it would be removed at all (even with the optional flag). So it feels like mergeCollection() should follow that same pattern.
If we need to remove a key from Onyx we should maybe just allow explicitly removing a key instead of having it happen in the mergeCollection()? Looks like Onyx.remove() is not a public method, but I'm not sure why it shouldn't be?
Could that work ?
There was a problem hiding this comment.
This is a good point, I've cleared my approval because I agree that this isnt something merge should be doing
There was a problem hiding this comment.
Yep, I did address this in my first comment. My reasoning behind putting it here was to avoid the alternative, which was creating a new method (setCollection) that would basically first call mergeCollection, and then, again, get AllKeys and do the new functionality. Would that work?
If we need to remove a key from Onyx we should maybe just allow explicitly removing a key instead of having it happen in the mergeCollection()? Looks like Onyx.remove() is not a public method, but I'm not sure why it shouldn't be?
Removing the key is setting it to null, right? That is basically what the new code does for all keys in a collection that are not in the "new" collection passed. I think it is a useful method to have for a number of things. Basically, any collection where we can remove elements in the backend could benefit from this.
We can totally replicate the functionality by manually deleting the keys in each case, but why would we?
There was a problem hiding this comment.
I've moved the logic to a new function: setCollection. LMK what you think
Yep, that is correct (and it is what the code does). This is just a helper function.
I could do it manually for this specific case, but it sounded like a good helper function to have in the future for similar cases. |
| const Onyx = { | ||
| connect, | ||
| disconnect, | ||
| getAllKeys, |
There was a problem hiding this comment.
I think we can just see which keys exist by subscribing to the collection? If the new value that comes back from the API is different then what we have here then just remove those keys?
There was a problem hiding this comment.
Ah, very true. Closing this PR, since we no longer need it. Thanks for all the input!
@alex-mechler 🐻
cc @marcaaron to make sure I'm not doing anything that goes against our standards.
Details
For Expensify/App#4453, I needed the ability to, basically, replace a whole collection with a new one. Since we currently don't have an easy way of doing so, I need to retrieve all existing keys so I can decide which ones to clear.
Related Issues
https://github.com/Expensify/Expensify/issues/173112
Automated Tests
None
Linked PRs
Expensify/App#4453