about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2024-01-04 17:36:27 -0800
committerGitHub <noreply@github.com>2024-01-04 17:36:27 -0800
commitdb62f272412df2c34e1a57200291b53fa1cd07aa (patch)
treea3944eed82535671bd41d6e2234dbc78ef4aa4c7
parent8a4a8af61c47ba315bcf39d6170825e316bfd241 (diff)
downloadvoidsky-db62f272412df2c34e1a57200291b53fa1cd07aa.tar.zst
Reduce web requests (#2420)
* Stop auto-refetching app passwords and invites on an interval

* Don't poll for posts or notifs if the app/tab isnt focused
-rw-r--r--src/state/queries/app-passwords.ts1
-rw-r--r--src/state/queries/invites.ts1
-rw-r--r--src/state/queries/notifications/unread.tsx4
-rw-r--r--src/state/queries/post-feed.ts4
4 files changed, 8 insertions, 2 deletions
diff --git a/src/state/queries/app-passwords.ts b/src/state/queries/app-passwords.ts
index 4b9e09a8d..014244f01 100644
--- a/src/state/queries/app-passwords.ts
+++ b/src/state/queries/app-passwords.ts
@@ -9,7 +9,6 @@ export const RQKEY = () => ['app-passwords']
 export function useAppPasswordsQuery() {
   return useQuery({
     staleTime: STALE.MINUTES.FIVE,
-    refetchInterval: STALE.MINUTES.ONE,
     queryKey: RQKEY(),
     queryFn: async () => {
       const res = await getAgent().com.atproto.server.listAppPasswords({})
diff --git a/src/state/queries/invites.ts b/src/state/queries/invites.ts
index bfea402e1..9ae9c707f 100644
--- a/src/state/queries/invites.ts
+++ b/src/state/queries/invites.ts
@@ -16,7 +16,6 @@ export type InviteCodesQueryResponse = Exclude<
 export function useInviteCodesQuery() {
   return useQuery({
     staleTime: STALE.MINUTES.FIVE,
-    refetchInterval: STALE.MINUTES.FIVE,
     queryKey: ['inviteCodes'],
     queryFn: async () => {
       const res = await getAgent()
diff --git a/src/state/queries/notifications/unread.tsx b/src/state/queries/notifications/unread.tsx
index abaabbf0e..d604e8fe0 100644
--- a/src/state/queries/notifications/unread.tsx
+++ b/src/state/queries/notifications/unread.tsx
@@ -15,6 +15,7 @@ import {useMutedThreads} from '#/state/muted-threads'
 import {RQKEY as RQKEY_NOTIFS} from './feed'
 import {logger} from '#/logger'
 import {truncateAndInvalidate} from '../util'
+import {AppState} from 'react-native'
 
 const UPDATE_INTERVAL = 30 * 1e3 // 30sec
 
@@ -97,6 +98,9 @@ export function Provider({children}: React.PropsWithChildren<{}>) {
       async checkUnread({invalidate}: {invalidate?: boolean} = {}) {
         try {
           if (!getAgent().session) return
+          if (AppState.currentState !== 'active') {
+            return
+          }
 
           // count
           const page = await fetchPage({
diff --git a/src/state/queries/post-feed.ts b/src/state/queries/post-feed.ts
index 0e943622a..dbb729133 100644
--- a/src/state/queries/post-feed.ts
+++ b/src/state/queries/post-feed.ts
@@ -1,4 +1,5 @@
 import React, {useCallback, useEffect, useRef} from 'react'
+import {AppState} from 'react-native'
 import {AppBskyFeedDefs, AppBskyFeedPost, PostModeration} from '@atproto/api'
 import {
   useInfiniteQuery,
@@ -312,6 +313,9 @@ export async function pollLatest(page: FeedPage | undefined) {
   if (!page) {
     return false
   }
+  if (AppState.currentState !== 'active') {
+    return
+  }
 
   logger.debug('usePostFeedQuery: pollLatest')
   const post = await page.api.peekLatest()