-
Notifications
You must be signed in to change notification settings - Fork 74
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
add setCollection method #594
add setCollection method #594
Conversation
All contributors have signed the CLA ✍️ ✅ |
I have read the CLA Document and I hereby sign the CLA |
lib/Onyx.ts
Outdated
* @param collection Object collection keyed by individual collection member keys and values | ||
*/ | ||
function setCollection<TKey extends CollectionKeyBase, TMap>(collectionKey: TKey, collection: OnyxMergeCollectionInput<TKey, TMap>): Promise<void> { | ||
if (!OnyxUtils.isValidNonEmptyCollectionForMerge(collection)) { |
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.
I think it's totally valid to set a collection to empty if you want to clear it, not sure why we would prevent that
lib/Onyx.ts
Outdated
|
||
return OnyxUtils.getAllKeys().then((persistedKeys) => { | ||
// Find existing collection members | ||
const existingCollectionKeys = Array.from(persistedKeys).filter((key) => key.startsWith(collectionKey)); |
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.
Isn't this creating a potentially huge array from the set only to select a few entries from it and discard the set? Why not iterate the set and get the entries we want, reducing the memory footprint?
lib/Onyx.ts
Outdated
); | ||
|
||
// Combine removals with new collection data | ||
const finalCollection = { |
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.
Similarly here, isn't this creating a new variable with the combination of both removalCollection
and collection
and thus using more memory and CPU and instead would be better to avoid that by adding the removalCollection
keys to the collection
directly?
Reviewer Checklist
Screenshots/VideosMacOS: Chrome / SafariScreen.Recording.2024-11-19.at.3.11.18.AM.mov |
if (!key.startsWith(collectionKey)) { | ||
return; | ||
} |
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.
Is this even possible? If so how?
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.
@iwiznia there will be other persisted keys in the store which do not correspond to collectionKey
, therefore we need to skip such keys.
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.
Oh, right 🤦
let result: OnyxCollection<unknown>; | ||
const routeA = `${ONYX_KEYS.COLLECTION.ROUTES}A`; | ||
const routeB = `${ONYX_KEYS.COLLECTION.ROUTES}B`; | ||
const routeB1 = `${ONYX_KEYS.COLLECTION.ROUTES}B1`; |
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.
NAB why call this B1 and not C (and make the next one D)?
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.
@iwiznia the intention here was to showcase that the key (routeB1
) might start with a similar value as the other key (routeB
) but it would be properly filtered out in the setCollection
@zirgulis Are you aware that your changes don't work on dev environment because of https://github.com/callstack-internal/react-native-onyx/blob/b58dbc56f485ad8b5f1d44f8b5f26f87dacf2481/lib/OnyxUtils.ts#L1243? |
@zirgulis This does not work for collections like |
Weird, why do we have that code? |
Could you please elaborate why it won't work? I tested this locally and it seem to work without any issues video: |
@zirgulis Please see the video below: Screen.Recording.2024-11-16.at.2.02.22.AM.movAm I using an incorrect collection? |
@allroundexperts I think you are incorrectly using Line 735 in da8f578
Expensify/App#51864 (comment) |
@zirgulis That is what I was trying to do. I was trying to get rid of all actions, and create a single new action. Can you point me to a collection on which I can use this method? |
@allroundexperts to get rid of all report actions you need to pass the first param Using the collection in your video the code would be:
|
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.
Works good.
Screen.Recording.2024-11-19.at.3.11.18.AM.mov
For anyone interested in the test steps, here is what I did:
- Pull the PR and run
npm run build
. - Copy the
dist
folder, and replace it with thedist
folder inside thenode_modules/react-native-onyx
directory in the expensify web app. - Run
npx patch-package react-native-onyx
inside the web app. - Remove the
node_modules
folder, and runnpm install
- Run
npm run web
, and in the console test the new method.
Details
In order to better align the Expensify client data with the server data there's a need to introduce a new
setCollection
method which will set the new collection data and remove the old one.Related Issues
Expensify/App#51864
Automated Tests
Added test to cover all possible cases
Manual Tests
Author Checklist
### Related Issues
section aboveTests
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
MacOS: Desktop