[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 resumeszio/stable
parent
c9cf608f78
commit
cdf7a1957a
|
@ -158,6 +158,18 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
* out.
|
* out.
|
||||||
*/
|
*/
|
||||||
setState(s => {
|
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 {
|
return {
|
||||||
accounts: [
|
accounts: [
|
||||||
refreshedAccount,
|
refreshedAccount,
|
||||||
|
@ -379,38 +391,6 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
|
||||||
|
|
||||||
// Intentionally not awaited to unblock the UI:
|
// Intentionally not awaited to unblock the UI:
|
||||||
resumeSessionWithFreshAccount()
|
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> {
|
async function resumeSessionWithFreshAccount(): Promise<SessionAccount> {
|
||||||
|
|
Loading…
Reference in New Issue