Skip to content
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

Refactor useObject #5208

Merged
merged 4 commits into from
Jan 11, 2023
Merged

Refactor useObject #5208

merged 4 commits into from
Jan 11, 2023

Conversation

takameyer
Copy link
Contributor

What, How & Why?

This closes #5185 and #4514

☑️ ToDos

  • 📝 Changelog entry
  • 📝 Compatibility label is updated or copied from previous entry
  • 📝 Update COMPATIBILITY.md
  • 🚦 Tests
  • 🔀 Executed flexible sync tests locally if modifying flexible sync
  • 📦 Updated internal package version in consuming package.jsons (if updating internal packages)
  • 📱 Check the React Native/other sample apps work if necessary
  • 📝 Public documentation PR created or is not necessary
  • 💥 Breaking label has been applied or is not necessary

If this PR adds or changes public API's:

  • typescript definitions file is updated
  • jsdoc files updated

* Prime any list properties with an cachedCollection so that updates fire correctly (Fixes #5185)
* Primary Keys as non-primative values would reset the cached objects, since their reference always changes
* Create a listener on the collection if the object doesn't exist, and rerender when it is created (Fixes #4514)
Copy link
Member

@elle-j elle-j left a comment

Choose a reason for hiding this comment

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

Very interesting PR :) LGTM

const listCaches = new Map();
const listTearDowns: Array<() => void> = [];
// If the object doesn't exist, just return it with an noop tearDown
if (object === null) {
return { object, tearDown: () => undefined };
}

// Create a cache for any Realm.List properties on the object
object.keys().forEach((key) => {
Copy link
Member

Choose a reason for hiding this comment

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

Not sure how significant it would be in this particular case, but for of loops are usually a lot faster than forEach. So I assume in this case it would depend on how many times the function will be called and the number of keys on the object.

Copy link
Member

Choose a reason for hiding this comment

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

I agree - I would opt for a for-of loop over the forEach here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done 👍🏼 thanks!

packages/realm-react/src/useObject.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/useObject.tsx Outdated Show resolved Hide resolved
Copy link
Contributor

@gagik gagik left a comment

Choose a reason for hiding this comment

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

Agree with LJ's comments but beyond those LGTM!

@takameyer takameyer self-assigned this Jan 4, 2023
const listCaches = new Map();
const listTearDowns: Array<() => void> = [];
// If the object doesn't exist, just return it with an noop tearDown
if (object === null) {
return { object, tearDown: () => undefined };
}

// Create a cache for any Realm.List properties on the object
object.keys().forEach((key) => {
Copy link
Member

Choose a reason for hiding this comment

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

I agree - I would opt for a for-of loop over the forEach here.

packages/realm-react/src/useObject.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/useObject.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/useObject.tsx Show resolved Hide resolved
packages/realm-react/src/useObject.tsx Outdated Show resolved Hide resolved
packages/realm-react/src/useObject.tsx Show resolved Hide resolved
takameyer and others added 2 commits January 10, 2023 14:41
Co-authored-by: LJ <81748770+elle-j@users.noreply.github.com>
Co-authored-by: Kræn Hansen <kraen.hansen@mongodb.com>
@takameyer
Copy link
Contributor Author

I've gotten all the PR changes. Unless someone yells, I'll get this merged sometime tomorrow.

@takameyer takameyer merged commit 89d02db into master Jan 11, 2023
@takameyer takameyer deleted the andrew/realm-react/useObject branch January 11, 2023 14:55
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
5 participants