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

Implement User.state #5712

Merged
merged 5 commits into from
Apr 17, 2023
Merged

Implement User.state #5712

merged 5 commits into from
Apr 17, 2023

Conversation

elle-j
Copy link
Contributor

@elle-j elle-j commented Apr 5, 2023

What, How & Why?

Added missing implementation for User.state and updated the UserState enum to align with the v11 implementation.

Related v11 issue:

This closes #5686.

☑️ ToDos

  • 📝 Changelog entry
  • 🚦 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

CHANGELOG.md Outdated Show resolved Hide resolved
@@ -323,6 +324,26 @@ describe.skipIf(environment.missingServer, "User", () => {
expect(user2).to.be.undefined;
expect(didFail).to.be.true;
});

describe("state", () => {
it("can fetch state when logged in with email password", async function (this: AppContext & RealmContext) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: Following it description convention of this file.

@takameyer
Copy link
Contributor

LGTM :shipit:

CHANGELOG.md Outdated Show resolved Hide resolved
Comment on lines 45 to 57
export enum UserState {
/** Authenticated and available to communicate with services. */
Active = "active",
LoggedIn = "LoggedIn",
/** Logged out, but ready to be logged in. */
LoggedOut = "logged-out",
LoggedOut = "LoggedOut",
/** Removed from the app entirely. */
Removed = "removed",
Removed = "Removed",
}
Copy link
Member

@kraenhansen kraenhansen Apr 13, 2023

Choose a reason for hiding this comment

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

I don't think we should make this change.

I like that the string value of these are lowercase, if users compare with strings instead of enums (non TS and potential future users, if we choose to adopt string unions over string enums as we've had conversations about elsewhere). I believe this is the case for other string enums across the code-base too.

While I understand this was the implementation on v11, it wasn't what we declared: https://github.com/realm/realm-js/blob/v11/types/app.d.ts#L538-L545

Copy link
Contributor Author

@elle-j elle-j Apr 13, 2023

Choose a reason for hiding this comment

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

Yeah this was the most uncertain change, whether to:

  • (A) Break the declared types (in order to not break the implementation of v11), or
  • (B) Break the implementation (in order to not break the declared types, and fix the implementation bug)

Since both do break something, it's less clear which we should opt for to make a better transition from v11 - v12.

Fixing the implementation bug also seems reasonable, but if people have been using these types, they were probably depending on the (bug) implementation rather than the declarations. But I do agree on keeping the enum values lowercase, just wasn't sure if that should be a fix for v12 or v13.

What are your thoughts on (A) and (B) and how do you reason about what to break specifically for adopting v12?

Copy link
Member

@kraenhansen kraenhansen Apr 14, 2023

Choose a reason for hiding this comment

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

Worth noting though, is the JS docs is aligned with the implementation: https://www.mongodb.com/docs/realm-sdks/js/latest/Realm.User.html#state
I'd personally vote for B as it'll make the implementation consistent with other enums and the types, which also declare our API (admittedly conflicting with both implementation and docs 😞). In a situation like this, I'd generally lean towards the declared API, but since that's not even consistent across types and docs, we'll probably have to make a tough decision.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ty for the link! Okay yeah definitely a tougher choice but interesting to get perspective on how to navigate this as well. Very helpful input 🙂

@takameyer and @kneth, what are your thoughts on this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Now that i'm looking into it, i'm very concerned about how much is mismatched regarding this state. If one does a search in our project for LoggedOut and then a search for logged-out then it is visible that we are not aligned throughout the project.

With that in mind, it is imperative that on both v11 and v12, we align with the C++ implementation (LoggedOut). This could also mean a breaking change release for realm-web in order to stay aligned throughout all packages.

Then I would say, we create a ticket for v13 to do a breaking change and set these to lower cased (logged-out).

My 2-cents

Copy link
Member

Choose a reason for hiding this comment

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

@takameyer to ensure I understand you, which of the two alternatives suggested by LJ are you voting for? 🙂 Are you suggesting A + following up with a ticket to break this back to lower-case with v13?

This could also mean a breaking change release for realm-web

realm-web is using the lower-cased variant: https://github.com/realm/realm-js/blob/main/packages/realm-web/src/User.ts#L57-L64

Copy link
Contributor

Choose a reason for hiding this comment

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

A + follow up breaking change

Although we might need to break realm-web to make it work, if the types are being shared

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tend to lean slightly more toward not changing the implementation as well (i.e. using LoggedOut etc.) for v12. But I do see both points.

To make it less annoying for users, we could actually also keep the UserState.Active but have the value be LoggedIn. This would still break the declared type, but writing UserState.Active would work. (Then mark it deprecated and remove it in v13):

  • v12:
export enum UserState {
  /** @deprecated Will be removed in v13. Please use {@link LoggedIn} */
  Active = "LoggedIn",
  LoggedIn = "LoggedIn",
  LoggedOut = "LoggedOut",
  Removed = "Removed",
}
  • v13:
export enum UserState {
  LoggedIn = "logged-in",
  LoggedOut = "logged-out",
  Removed = "removed",
}

Copy link
Member

Choose a reason for hiding this comment

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

Since no apps have ever been able to use UserState.Active I think it would be better to leave its value as "Active" to avoid introducing a subtle bug in an end-users app, but just never return that from the user.state.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed 🙂

@elle-j elle-j merged commit 301b6ce into main Apr 17, 2023
@elle-j elle-j deleted the lj/user-state branch April 17, 2023 14:20
papafe added a commit that referenced this pull request Apr 17, 2023
* main:
  Update core to v13.9.1 (#5739)
  Implement `User.state` (#5712)
  Rename `local-mongodb` to enable app replacements (#5734)
  Update return type of 'assert.never'. (#5718)
  Run tests after builds are done, regardless of they failed or succeeded (#5729)
@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.

Implement User.state (v12)
4 participants