diff options
Diffstat (limited to 'src/lib/react-query.ts')
-rw-r--r-- | src/lib/react-query.ts | 32 |
1 files changed, 30 insertions, 2 deletions
diff --git a/src/lib/react-query.ts b/src/lib/react-query.ts index 6ec620f74..7fe3fe7a4 100644 --- a/src/lib/react-query.ts +++ b/src/lib/react-query.ts @@ -1,11 +1,39 @@ -import {QueryClient} from '@tanstack/react-query' +import {AppState, AppStateStatus} from 'react-native' +import {QueryClient, focusManager} from '@tanstack/react-query' +import {isNative} from '#/platform/detection' + +focusManager.setEventListener(onFocus => { + if (isNative) { + const subscription = AppState.addEventListener( + 'change', + (status: AppStateStatus) => { + focusManager.setFocused(status === 'active') + }, + ) + + return () => subscription.remove() + } else if (typeof window !== 'undefined' && window.addEventListener) { + // these handlers are a bit redundant but focus catches when the browser window + // is blurred/focused while visibilitychange seems to only handle when the + // window minimizes (both of them catch tab changes) + // there's no harm to redundant fires because refetchOnWindowFocus is only + // used with queries that employ stale data times + const handler = () => onFocus() + window.addEventListener('focus', handler, false) + window.addEventListener('visibilitychange', handler, false) + return () => { + window.removeEventListener('visibilitychange', handler) + window.removeEventListener('focus', handler) + } + } +}) export const queryClient = new QueryClient({ defaultOptions: { queries: { // NOTE // refetchOnWindowFocus breaks some UIs (like feeds) - // so we NEVER want to enable this + // so we only selectively want to enable this // -prf refetchOnWindowFocus: false, // Structural sharing between responses makes it impossible to rely on |