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

Update ClassMap before notifying schema change listeners #5955

Merged
merged 7 commits into from
Jul 3, 2023
Merged

Conversation

elle-j
Copy link
Contributor

@elle-j elle-j commented Jun 29, 2023

What, How & Why?

When calling Realm._updateSchema() in a write transaction, the schema change listener is fired after the call into Core, but before _updateSchema() returns (and thus before the write transaction is completed).

The ClassMap instance on the Realm was previously updated/reinitialized in _updateSchema(), but since the listener was fired before that, the ClassMap had not been updated in time. This PR moves that update into the schemaDidChange callback, updating it before notifying schema change listeners.

This closes #5574

☑️ ToDos

  • 📝 Changelog entry
  • 🚦 Tests

@@ -599,7 +599,7 @@ export class Realm {
public readonly syncSession: SyncSession | null;

private schemaExtras: RealmSchemaExtra = {};
private classes: ClassMap;
private classes!: ClassMap;
Copy link
Member

@kraenhansen kraenhansen Jun 29, 2023

Choose a reason for hiding this comment

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

I'm almost thinking that this drawback from deferring to a method at line 688 isn't worth it. As we don't expect to have to initialize the class map more than twice and it's not too complex, I would probably suggest duplicating and inlining it on 662 and 688 instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You make a good point, I'll inline those calls 👍

P.S. would be nice to add something in function signatures that tells TS that a certain property will be set. Similar to asserts or is. That would remove the risks of having to use ! as in the above case.

packages/realm/src/Realm.ts Outdated Show resolved Hide resolved
@elle-j elle-j merged commit 768a632 into main Jul 3, 2023
30 of 31 checks passed
@elle-j elle-j deleted the lj/class-map branch July 3, 2023 13:22
@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
Development

Successfully merging this pull request may close these issues.

Re-initialize the ClassMap when schema is updated (in a listener)
4 participants