[Session] Rely on agent session change event for persisting resumed session (#3836)

* Rely on agent session change handler for resumption

* Add a fast path for noop resumes
zio/stable
dan 2024-05-03 03:47:11 +01:00 committed by GitHub
parent c9cf608f78
commit cdf7a1957a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 32 deletions

View File

@ -158,6 +158,18 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
* out.
*/
setState(s => {
const existingAccount = s.accounts.find(
a => a.did === refreshedAccount.did,
)
if (
!expired &&
existingAccount &&
refreshedAccount &&
JSON.stringify(existingAccount) === JSON.stringify(refreshedAccount)
) {
// Fast path without a state update.
return s
}
return {
accounts: [
refreshedAccount,
@ -379,38 +391,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
// Intentionally not awaited to unblock the UI:
resumeSessionWithFreshAccount()
.then(freshAccount => {
if (JSON.stringify(account) !== JSON.stringify(freshAccount)) {
logger.info(
`session: reuse of previous session returned a fresh account, upserting`,
)
setState(s => {
return {
accounts: [
freshAccount,
...s.accounts.filter(a => a.did !== freshAccount.did),
],
currentAgentState: {
did: freshAccount.did,
agent: agent,
},
needsPersist: true,
}
})
}
})
.catch(e => {
/*
* Note: `agent.persistSession` is also called when this fails, and
* we handle that failure via `createPersistSessionHandler`
*/
logger.info(`session: resumeSessionWithFreshAccount failed`, {
message: e,
})
__globalAgent = PUBLIC_BSKY_AGENT
// TODO: This needs a setState.
})
}
async function resumeSessionWithFreshAccount(): Promise<SessionAccount> {