about summary refs log tree commit diff
path: root/src/state
diff options
context:
space:
mode:
Diffstat (limited to 'src/state')
-rw-r--r--src/state/queries/list.ts40
-rw-r--r--src/state/queries/notifications/feed.ts8
-rw-r--r--src/state/queries/notifications/unread.tsx6
-rw-r--r--src/state/queries/post-feed.ts18
-rw-r--r--src/state/queries/profile.ts6
-rw-r--r--src/state/session/index.tsx6
6 files changed, 35 insertions, 49 deletions
diff --git a/src/state/queries/list.ts b/src/state/queries/list.ts
index ef05009d1..550baecb3 100644
--- a/src/state/queries/list.ts
+++ b/src/state/queries/list.ts
@@ -3,7 +3,6 @@ import {
   AppBskyGraphGetList,
   AppBskyGraphList,
   AppBskyGraphDefs,
-  BskyAgent,
 } from '@atproto/api'
 import {Image as RNImage} from 'react-native-image-crop-picker'
 import {useQuery, useMutation, useQueryClient} from '@tanstack/react-query'
@@ -75,13 +74,9 @@ export function useListCreateMutation() {
         )
 
         // wait for the appview to update
-        await whenAppViewReady(
-          getAgent(),
-          res.uri,
-          (v: AppBskyGraphGetList.Response) => {
-            return typeof v?.data?.list.uri === 'string'
-          },
-        )
+        await whenAppViewReady(res.uri, (v: AppBskyGraphGetList.Response) => {
+          return typeof v?.data?.list.uri === 'string'
+        })
         return res
       },
       onSuccess() {
@@ -142,16 +137,12 @@ export function useListMetadataMutation() {
       ).data
 
       // wait for the appview to update
-      await whenAppViewReady(
-        getAgent(),
-        res.uri,
-        (v: AppBskyGraphGetList.Response) => {
-          const list = v.data.list
-          return (
-            list.name === record.name && list.description === record.description
-          )
-        },
-      )
+      await whenAppViewReady(res.uri, (v: AppBskyGraphGetList.Response) => {
+        const list = v.data.list
+        return (
+          list.name === record.name && list.description === record.description
+        )
+      })
       return res
     },
     onSuccess(data, variables) {
@@ -216,13 +207,9 @@ export function useListDeleteMutation() {
       }
 
       // wait for the appview to update
-      await whenAppViewReady(
-        getAgent(),
-        uri,
-        (v: AppBskyGraphGetList.Response) => {
-          return !v?.success
-        },
-      )
+      await whenAppViewReady(uri, (v: AppBskyGraphGetList.Response) => {
+        return !v?.success
+      })
     },
     onSuccess() {
       invalidateMyLists(queryClient)
@@ -271,7 +258,6 @@ export function useListBlockMutation() {
 }
 
 async function whenAppViewReady(
-  agent: BskyAgent,
   uri: string,
   fn: (res: AppBskyGraphGetList.Response) => boolean,
 ) {
@@ -280,7 +266,7 @@ async function whenAppViewReady(
     1e3, // 1s delay between tries
     fn,
     () =>
-      agent.app.bsky.graph.getList({
+      getAgent().app.bsky.graph.getList({
         list: uri,
         limit: 1,
       }),
diff --git a/src/state/queries/notifications/feed.ts b/src/state/queries/notifications/feed.ts
index 54bd87540..68396143c 100644
--- a/src/state/queries/notifications/feed.ts
+++ b/src/state/queries/notifications/feed.ts
@@ -4,7 +4,6 @@ import {
   AppBskyFeedRepost,
   AppBskyFeedLike,
   AppBskyNotificationListNotifications,
-  BskyAgent,
 } from '@atproto/api'
 import chunk from 'lodash.chunk'
 import {
@@ -84,7 +83,7 @@ export function useNotificationFeedQuery(opts?: {enabled?: boolean}) {
 
       // we fetch subjects of notifications (usually posts) now instead of lazily
       // in the UI to avoid relayouts
-      const subjects = await fetchSubjects(getAgent(), notifsGrouped)
+      const subjects = await fetchSubjects(notifsGrouped)
       for (const notif of notifsGrouped) {
         if (notif.subjectUri) {
           notif.subject = subjects.get(notif.subjectUri)
@@ -173,7 +172,6 @@ function groupNotifications(
 }
 
 async function fetchSubjects(
-  agent: BskyAgent,
   groupedNotifs: FeedNotification[],
 ): Promise<Map<string, AppBskyFeedDefs.PostView>> {
   const uris = new Set<string>()
@@ -185,7 +183,9 @@ async function fetchSubjects(
   const uriChunks = chunk(Array.from(uris), 25)
   const postsChunks = await Promise.all(
     uriChunks.map(uris =>
-      agent.app.bsky.feed.getPosts({uris}).then(res => res.data.posts),
+      getAgent()
+        .app.bsky.feed.getPosts({uris})
+        .then(res => res.data.posts),
     ),
   )
   const map = new Map<string, AppBskyFeedDefs.PostView>()
diff --git a/src/state/queries/notifications/unread.tsx b/src/state/queries/notifications/unread.tsx
index 36bc6528f..b93e1dc81 100644
--- a/src/state/queries/notifications/unread.tsx
+++ b/src/state/queries/notifications/unread.tsx
@@ -70,12 +70,10 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
       },
 
       async checkUnread() {
-        const agent = getAgent()
-
-        if (!agent.session) return
+        if (!getAgent().session) return
 
         // count
-        const res = await agent.listNotifications({limit: 40})
+        const res = await getAgent().listNotifications({limit: 40})
         const filtered = res.data.notifications.filter(
           notif => !notif.isRead && !shouldFilterNotif(notif, moderationOpts),
         )
diff --git a/src/state/queries/post-feed.ts b/src/state/queries/post-feed.ts
index 113e6f2fb..c3f0c758f 100644
--- a/src/state/queries/post-feed.ts
+++ b/src/state/queries/post-feed.ts
@@ -7,7 +7,6 @@ import {
   QueryClient,
   useQueryClient,
 } from '@tanstack/react-query'
-import {getAgent} from '../session'
 import {useFeedTuners} from '../preferences/feed-tuners'
 import {FeedTuner, NoopFeedTuner} from 'lib/api/feed-manip'
 import {FeedAPI, ReasonFeedSource} from 'lib/api/feed/types'
@@ -77,30 +76,29 @@ export function usePostFeedQuery(
   const feedTuners = useFeedTuners(feedDesc)
   const enabled = opts?.enabled !== false
   const moderationOpts = useModerationOpts()
-  const agent = getAgent()
 
   const api: FeedAPI = useMemo(() => {
     if (feedDesc === 'home') {
-      return new MergeFeedAPI(agent, params || {}, feedTuners)
+      return new MergeFeedAPI(params || {}, feedTuners)
     } else if (feedDesc === 'following') {
-      return new FollowingFeedAPI(agent)
+      return new FollowingFeedAPI()
     } else if (feedDesc.startsWith('author')) {
       const [_, actor, filter] = feedDesc.split('|')
-      return new AuthorFeedAPI(agent, {actor, filter})
+      return new AuthorFeedAPI({actor, filter})
     } else if (feedDesc.startsWith('likes')) {
       const [_, actor] = feedDesc.split('|')
-      return new LikesFeedAPI(agent, {actor})
+      return new LikesFeedAPI({actor})
     } else if (feedDesc.startsWith('feedgen')) {
       const [_, feed] = feedDesc.split('|')
-      return new CustomFeedAPI(agent, {feed})
+      return new CustomFeedAPI({feed})
     } else if (feedDesc.startsWith('list')) {
       const [_, list] = feedDesc.split('|')
-      return new ListFeedAPI(agent, {list})
+      return new ListFeedAPI({list})
     } else {
       // shouldnt happen
-      return new FollowingFeedAPI(agent)
+      return new FollowingFeedAPI()
     }
-  }, [feedDesc, params, feedTuners, agent])
+  }, [feedDesc, params, feedTuners])
 
   const disableTuner = !!params?.disableTuner
   const tuner = useMemo(
diff --git a/src/state/queries/profile.ts b/src/state/queries/profile.ts
index e27bac9a6..9467a2553 100644
--- a/src/state/queries/profile.ts
+++ b/src/state/queries/profile.ts
@@ -4,7 +4,6 @@ import {
   AppBskyActorDefs,
   AppBskyActorProfile,
   AppBskyActorGetProfile,
-  BskyAgent,
 } from '@atproto/api'
 import {useQuery, useQueryClient, useMutation} from '@tanstack/react-query'
 import {Image as RNImage} from 'react-native-image-crop-picker'
@@ -68,7 +67,7 @@ export function useProfileUpdateMutation() {
         }
         return existing
       })
-      await whenAppViewReady(getAgent(), profile.did, res => {
+      await whenAppViewReady(profile.did, res => {
         if (typeof newUserAvatar !== 'undefined') {
           if (newUserAvatar === null && res.data.avatar) {
             // url hasnt cleared yet
@@ -464,7 +463,6 @@ function useProfileUnblockMutation() {
 }
 
 async function whenAppViewReady(
-  agent: BskyAgent,
   actor: string,
   fn: (res: AppBskyActorGetProfile.Response) => boolean,
 ) {
@@ -472,6 +470,6 @@ async function whenAppViewReady(
     5, // 5 tries
     1e3, // 1s delay between tries
     fn,
-    () => agent.app.bsky.actor.getProfile({actor}),
+    () => getAgent().app.bsky.actor.getProfile({actor}),
   )
 }
diff --git a/src/state/session/index.tsx b/src/state/session/index.tsx
index 946c742ad..e6def1fab 100644
--- a/src/state/session/index.tsx
+++ b/src/state/session/index.tsx
@@ -13,6 +13,12 @@ import {useCloseAllActiveElements} from '#/state/util'
 
 let __globalAgent: BskyAgent = PUBLIC_BSKY_AGENT
 
+/**
+ * NOTE
+ * Never hold on to the object returned by this function.
+ * Call `getAgent()` at the time of invocation to ensure
+ * that you never have a stale agent.
+ */
 export function getAgent() {
   return __globalAgent
 }