From db62f272412df2c34e1a57200291b53fa1cd07aa Mon Sep 17 00:00:00 2001 From: Paul Frazee Date: Thu, 4 Jan 2024 17:36:27 -0800 Subject: [PATCH] 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 --- src/state/queries/app-passwords.ts | 1 - src/state/queries/invites.ts | 1 - src/state/queries/notifications/unread.tsx | 4 ++++ src/state/queries/post-feed.ts | 4 ++++ 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/state/queries/app-passwords.ts b/src/state/queries/app-passwords.ts index 4b9e09a8..014244f0 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 bfea402e..9ae9c707 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 abaabbf0..d604e8fe 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 0e943622..dbb72913 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()