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

feat: don't emit SIGNED_IN events on visibility change #684

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hf
Copy link
Contributor

@hf hf commented May 18, 2023

Each time a tab became visible, the SIGNED_IN event was emitted. Although this is normal, it's unnecessary overhead. In some implementations, it could result in unnecessary requests or UI re-renders.

Fixes #579

@hf hf force-pushed the hf/fix-onauthstatechange-with-signed-in-onvisibilitychanged branch from 989a43b to 3497bce Compare May 18, 2023 12:16
Comment on lines -1208 to -1222
const timeNow = Math.round(Date.now() / 1000)

if ((currentSession.expires_at ?? Infinity) < timeNow + EXPIRY_MARGIN) {
if (this.autoRefreshToken && currentSession.refresh_token) {
const { error } = await this._callRefreshToken(currentSession.refresh_token)

if (error) {
console.log(error.message)
await this._removeSession()
}
}
} else {
if (this.persistSession) {
await this._saveSession(currentSession)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No need to have 2 different versions that do refreshing when getSession does it all. It uses _getSession because _recoverAndRefresh is part of the _initialize flow and it means that getSession would never complete.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah okay, so to expand - if we called getSession instead of _getSession() this would fail as getSession would call intializePromise which would call _initialize() which would then call _recoverAndRefresh leading to an infinite loop?

kangmingtay
kangmingtay previously approved these changes May 18, 2023
@@ -1194,32 +1216,17 @@ export default class GoTrueClient {
* Recovers the session from LocalStorage and refreshes
* Note: this method is async to accommodate for AsyncStorage e.g. in React native.
*/
private async _recoverAndRefresh() {
private async _recoverAndRefresh(isInitial: boolean) {
Copy link
Member

Choose a reason for hiding this comment

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

what does the isInitial param do here? can we document it above?

@@ -709,7 +731,7 @@ export default class GoTrueClient {
}

const hasExpired = currentSession.expires_at
? currentSession.expires_at <= Date.now() / 1000
? currentSession.expires_at - EXPIRY_MARGIN <= Date.now() / 1000
Copy link
Member

Choose a reason for hiding this comment

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

hmm i rmb we didn't do this before because there were some other places where we used getSession and checked the expiry margin too which would result in double counting - but just need to check again to be sure

Copy link
Contributor

Choose a reason for hiding this comment

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

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're right there was an issue... I'll actually add the Date.now() in the param so when called from _recoverAndRefresh it will use the current time + margin, and otherwise just the current time.

@@ -660,10 +660,6 @@ export default class GoTrueClient {
}
}

/**
* Returns the session, refreshing it if necessary.
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah is this section no longer needed/are we making getSession internal? Not sure if removing the comments will remove the description from reference spec

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm I'll investigate.

J0
J0 previously approved these changes May 18, 2023
@hf
Copy link
Contributor Author

hf commented Jun 14, 2023

I need more time to figure this out.

@hf hf dismissed kangmingtay’s stale review June 14, 2023 09:29

Need more time.

@hf hf requested a review from J0 June 14, 2023 09:29
@hf
Copy link
Contributor Author

hf commented Jun 29, 2023

Found something else: in multi-tab this SIGNED_IN event is emitted to all other tabs so it may be a bit too much.

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.

onAuthStateChange fires every time we focus the tab
3 participants