[Session] Call persist handler directly (#3828)

zio/stable
dan 2024-05-02 23:05:53 +01:00 committed by GitHub
parent b86c3b486f
commit 6405ad7cba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 62 additions and 95 deletions

View File

@ -1,5 +1,5 @@
import React from 'react'
import {AtpPersistSessionHandler, BskyAgent} from '@atproto/api'
import {AtpSessionData, AtpSessionEvent, BskyAgent} from '@atproto/api'
import {track} from '#/lib/analytics/analytics'
import {networkRetry} from '#/lib/async/retry'
@ -93,28 +93,20 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
}))
}, [setState])
const createPersistSessionHandler = React.useCallback(
const onAgentSessionChange = React.useCallback(
(
agent: BskyAgent,
account: SessionAccount,
persistSessionCallback: (props: {
expired: boolean
refreshedAccount: SessionAccount
}) => void,
{
networkErrorCallback,
}: {
networkErrorCallback?: () => void
} = {},
): AtpPersistSessionHandler => {
return function persistSession(event, session) {
event: AtpSessionEvent,
session: AtpSessionData | undefined,
) => {
const expired = event === 'expired' || event === 'create-failed'
if (event === 'network-error') {
logger.warn(
`session: persistSessionHandler received network-error event`,
)
networkErrorCallback?.()
clearCurrentAccount()
return
}
@ -157,13 +149,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
* to persist this data and wipe their tokens, effectively logging them
* out.
*/
persistSessionCallback({
expired,
refreshedAccount,
})
}
upsertAccount(refreshedAccount, expired)
},
[],
[clearCurrentAccount, upsertAccount],
)
const createAccount = React.useCallback<SessionApiContext['createAccount']>(
@ -191,16 +179,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
},
)
agent.setPersistSessionHandler(
createPersistSessionHandler(
agent,
account,
({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired)
},
{networkErrorCallback: clearCurrentAccount},
),
)
agent.setPersistSessionHandler((event, session) => {
onAgentSessionChange(agent, account, event, session)
})
__globalAgent = agent
await fetchingGates
@ -210,7 +191,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
track('Create Account')
logEvent('account:create:success', {})
},
[upsertAccount, clearCurrentAccount, createPersistSessionHandler],
[upsertAccount, onAgentSessionChange],
)
const login = React.useCallback<SessionApiContext['login']>(
@ -223,16 +204,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
authFactorToken,
})
agent.setPersistSessionHandler(
createPersistSessionHandler(
agent,
account,
({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired)
},
{networkErrorCallback: clearCurrentAccount},
),
)
agent.setPersistSessionHandler((event, session) => {
onAgentSessionChange(agent, account, event, session)
})
__globalAgent = agent
// @ts-ignore
@ -245,7 +219,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
track('Sign In', {resumedSession: false})
logEvent('account:loggedIn', {logContext, withPassword: true})
},
[upsertAccount, clearCurrentAccount, createPersistSessionHandler],
[upsertAccount, onAgentSessionChange],
)
const logout = React.useCallback<SessionApiContext['logout']>(
@ -280,16 +254,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
agent.pdsUrl = agent.api.xrpc.uri = new URL(account.pdsUrl)
}
agent.setPersistSessionHandler(
createPersistSessionHandler(
agent,
account,
({expired, refreshedAccount}) => {
upsertAccount(refreshedAccount, expired)
},
{networkErrorCallback: clearCurrentAccount},
),
)
agent.setPersistSessionHandler((event, session) => {
onAgentSessionChange(agent, account, event, session)
})
// @ts-ignore
if (IS_DEV && isWeb) window.agent = agent
@ -380,7 +347,7 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
return sessionAccount
}
},
[upsertAccount, clearCurrentAccount, createPersistSessionHandler],
[upsertAccount, onAgentSessionChange],
)
const removeAccount = React.useCallback<SessionApiContext['removeAccount']>(