about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-05-03 03:47:11 +0100
committerGitHub <noreply@github.com>2024-05-03 03:47:11 +0100
commitcdf7a1957ad6a5bd569f60fb7113c44973f4e2d7 (patch)
treef8099213163ca7036383b25ce4a50299730cb85d /src
parentc9cf608f789943e81bfa32b8da5f6ca4f75d5a66 (diff)
downloadvoidsky-cdf7a1957ad6a5bd569f60fb7113c44973f4e2d7.tar.zst
[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
Diffstat (limited to 'src')
-rw-r--r--src/state/session/index.tsx44
1 files changed, 12 insertions, 32 deletions
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx
index b4799e2f7..b9d67f2e0 100644
--- a/src/state/session/index.tsx
+++ b/src/state/session/index.tsx
@@ -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> {