[Session] Dispose of stale agents immediately

zio/stable
Dan Abramov 2024-05-23 02:45:50 +01:00 committed by dan
parent acf1def6c1
commit 8938fc87a0
1 changed files with 13 additions and 0 deletions

View File

@ -208,6 +208,19 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
if (IS_DEV && isWeb) window.agent = state.currentAgentState.agent if (IS_DEV && isWeb) window.agent = state.currentAgentState.agent
const agent = state.currentAgentState.agent as BskyAgent const agent = state.currentAgentState.agent as BskyAgent
const currentAgentRef = React.useRef(agent)
React.useEffect(() => {
if (currentAgentRef.current !== agent) {
// Read the previous value and immediately advance the pointer.
const prevAgent = currentAgentRef.current
currentAgentRef.current = agent
// We never reuse agents so let's fully neutralize the previous one.
// This ensures it won't try to consume any refresh tokens.
prevAgent.session = undefined
prevAgent.setPersistSessionHandler(undefined)
}
}, [agent])
return ( return (
<AgentContext.Provider value={agent}> <AgentContext.Provider value={agent}>
<StateContext.Provider value={stateContext}> <StateContext.Provider value={stateContext}>