-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
[Session] Root #3393
[Session] Root #3393
Conversation
8f20337
to
2260868
Compare
|
src/view/com/modals/ChangeEmail.tsx
Outdated
updateCurrentAccount({ | ||
email: email.trim(), | ||
emailConfirmed: false, | ||
}) |
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.
So I think this was actually a Bad Thing™. We treat currentAccount
as a read-only representation of server state, but in this case we also mutated it and used it to dictate UI state.
I've replaced it with a call to refresh the session and derive currentAccount
from that data.
src/state/session/index.tsx
Outdated
// as we migrate, continue to keep this updated | ||
__globalAgent = agent | ||
|
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.
@gaearon special note of this
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.
Should we do a pass to use a hook first? And then replace the implementation.
currentAccount
from active session
src/state/session/index.tsx
Outdated
const [isInitialLoad, setIsInitialLoad] = React.useState(true) | ||
const [isSwitchingAccounts, setIsSwitchingAccounts] = React.useState(false) | ||
const currentAccount = React.useMemo( | ||
() => agentToSessionAccount(agent), |
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.
This assumes that once agent
gets into state, its .session
and nested fields (e.g. .session.refreshJwt
) never changes. Can we make that assumption?
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.
Alternatively, can rename those to nextAgent
.
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.
Yeah good shout. Those fields are managed internally by BskyAgent
. Example: when accessJwt
expires, the agent automatically calls refreshSession
which updates the session
internally. Calls to the currentAgent
in client code then automatically use the updated session.
This made me realize how important it is to ensure that fresh tokens get synced across tabs though, gonna triple check that...
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.
If agent
is something that can be mutated on its own, it isn't right to use it as a useMemo
input. Since nothing will trigger re-evaluation when these fields change. Let's sync up tomorrow to chat more about this?
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.
setAgent
is called when switching accounts, so each account always has its own agent
, and currentAccount
can be derived from it. What I'm explaining above isn't something we need to handle directly in this case.
|
edb4c45
to
fd085fd
Compare
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.
chatted out of band about this, seems like a promising direction with a few things to fix
currentAccount
from active session
Superseded by #3541 and stack |
The core issue being solved here is a race condition between the update of
currentAccount
and the__globalAgent
. #3333 set us up so that changing accounts gives us a fresh query cache, which was the impetus behind__globalAgent
in the first place (stale closures).This PR builds on that by deriving
currentAccount
from the currently activeBskyAgent
, removing the need to manage updates to two objects (currentAccount
and__globalAgent
) that have the same lifetime.In the process, I also split out state handling into separate
useState
calls to help avoid loops and isolate updates.currentAccount
fromBskyAgent.session
4482cbf1436113e1a34c4903bb26635417d1a8cdpersistSession
handler for more clarity 634995db38a2f7a3d6b4871b66cafb8480d86877With this PR, we should now reference the active
BskyAgent
via{ agent } = useSession()
and drop the__globalAgent
.Review
state/session.ts
without whitespace.