about summary refs log tree commit diff
path: root/src/state/session/index.tsx
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-05-03 17:57:09 +0100
committerGitHub <noreply@github.com>2024-05-03 17:57:09 +0100
commit4a2d4253e54f1bf3a375c6c6ffdbd5a9b6bcc24a (patch)
tree870e9113c57708dd3076237d61d838e2889de7f7 /src/state/session/index.tsx
parent85b34418ef31247f8d88bdb08248a149192c5b46 (diff)
downloadvoidsky-4a2d4253e54f1bf3a375c6c6ffdbd5a9b6bcc24a.tar.zst
[Session] Align state and global agent switchpoints (#3845)
* Adopt synced accounts unconditionally

* Remove try/catch around resuming session

* Move to login form on resume failure

* Restructure code flow for easier reading

---------

Co-authored-by: Eric Bailey <git@esb.lol>
Diffstat (limited to 'src/state/session/index.tsx')
-rw-r--r--src/state/session/index.tsx58
1 files changed, 22 insertions, 36 deletions
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx
index 4894ad696..276e3b97b 100644
--- a/src/state/session/index.tsx
+++ b/src/state/session/index.tsx
@@ -337,35 +337,22 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
       if (isSessionExpired(account)) {
         logger.debug(`session: attempting to resume using previous session`)
 
-        try {
-          const freshAccount = await resumeSessionWithFreshAccount()
-          __globalAgent = agent
-          await fetchingGates
-          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.
-        }
+        const freshAccount = await resumeSessionWithFreshAccount()
+        __globalAgent = agent
+        await fetchingGates
+        setState(s => {
+          return {
+            accounts: [
+              freshAccount,
+              ...s.accounts.filter(a => a.did !== freshAccount.did),
+            ],
+            currentAgentState: {
+              did: freshAccount.did,
+              agent: agent,
+            },
+            needsPersist: true,
+          }
+        })
       } else {
         logger.debug(`session: attempting to reuse previous session`)
 
@@ -480,6 +467,11 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
       const persistedSession = persisted.get('session')
 
       logger.debug(`session: persisted onUpdate`, {})
+      setState(s => ({
+        accounts: persistedSession.accounts,
+        currentAgentState: s.currentAgentState,
+        needsPersist: false, // Synced from another tab. Don't persist to avoid cycles.
+      }))
 
       const selectedAccount = persistedSession.accounts.find(
         a => a.did === persistedSession.currentAccount?.did,
@@ -531,15 +523,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
             did: undefined,
             agent: PUBLIC_BSKY_AGENT,
           },
-          needsPersist: true, // TODO: This seems bad in this codepath. Existing behavior.
+          needsPersist: false, // Synced from another tab. Don't persist to avoid cycles.
         }))
       }
-
-      setState(s => ({
-        accounts: persistedSession.accounts,
-        currentAgentState: s.currentAgentState,
-        needsPersist: false, // Synced from another tab. Don't persist to avoid cycles.
-      }))
     })
   }, [state, setState, initSession])