Skip to content

Make Onyx.getAllKeys public#94

Closed
Gonals wants to merge 14 commits into
masterfrom
alberto-clearCollections
Closed

Make Onyx.getAllKeys public#94
Gonals wants to merge 14 commits into
masterfrom
alberto-clearCollections

Conversation

@Gonals

@Gonals Gonals commented Aug 6, 2021

Copy link
Copy Markdown
Contributor

@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

@Gonals Gonals requested a review from a team August 6, 2021 13:27
@Gonals Gonals self-assigned this Aug 6, 2021
@MelvinBot MelvinBot requested review from NikkiWines and removed request for a team August 6, 2021 13:27
@github-actions

github-actions Bot commented Aug 6, 2021

Copy link
Copy Markdown
Contributor

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@Gonals Gonals requested a review from a team as a code owner August 6, 2021 13:34
@MelvinBot MelvinBot requested review from alex-mechler and removed request for a team August 6, 2021 13:34
@Gonals Gonals removed the request for review from NikkiWines August 6, 2021 15:32
@Gonals Gonals changed the title [WIP] Allow mergeCollection to also clear any removed keys Allow mergeCollection to also clear any removed keys Aug 6, 2021
@Gonals Gonals requested a review from marcaaron August 6, 2021 15:33
@Gonals

Gonals commented Aug 6, 2021

Copy link
Copy Markdown
Contributor Author

I have read the CLA Document and I hereby sign the CLA

alex-mechler
alex-mechler previously approved these changes Aug 6, 2021
@marcaaron

Copy link
Copy Markdown
Contributor

I needed the ability to, basically, replace a whole collection with a new one

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.

Comment thread lib/Onyx.js Outdated
}

// If the flag is set, clear any removed keys in the collection
if (clearRemoved) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is a good point, I've cleared my approval because I agree that this isnt something merge should be doing

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.

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?

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.

I've moved the logic to a new function: setCollection. LMK what you think

@alex-mechler alex-mechler self-requested a review August 6, 2021 16:53
@Gonals

Gonals commented Aug 7, 2021

Copy link
Copy Markdown
Contributor Author

I needed the ability to, basically, replace a whole collection with a new one

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.

Yep, that is correct (and it is what the code does). This is just a helper function.
My current flow is:

  • We have a list of policies in the backend.
  • We load them to onyx as a collection. We have them there.
  • Some of those policies get deleted.
  • We load them to onyx as a collection. We update any existing ones and add the new ones, but the deleted ones are not removed.
  • This functionality finds those and removes them.

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.
Thoughts?

@Gonals Gonals changed the title Allow mergeCollection to also clear any removed keys Create setCollection Aug 9, 2021
@Gonals Gonals changed the title Create setCollection Make Onyx.getAllKeys public Aug 9, 2021
Comment thread lib/Onyx.js
const Onyx = {
connect,
disconnect,
getAllKeys,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

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.

Ah, very true. Closing this PR, since we no longer need it. Thanks for all the input!

@Gonals Gonals closed this Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants