mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {AppState, AppStateStatus} from 'react-native'
2import {QueryClient, focusManager} from '@tanstack/react-query'
3import {isNative} from '#/platform/detection'
4
5focusManager.setEventListener(onFocus => {
6 if (isNative) {
7 const subscription = AppState.addEventListener(
8 'change',
9 (status: AppStateStatus) => {
10 focusManager.setFocused(status === 'active')
11 },
12 )
13
14 return () => subscription.remove()
15 } else if (typeof window !== 'undefined' && window.addEventListener) {
16 // these handlers are a bit redundant but focus catches when the browser window
17 // is blurred/focused while visibilitychange seems to only handle when the
18 // window minimizes (both of them catch tab changes)
19 // there's no harm to redundant fires because refetchOnWindowFocus is only
20 // used with queries that employ stale data times
21 const handler = () => onFocus()
22 window.addEventListener('focus', handler, false)
23 window.addEventListener('visibilitychange', handler, false)
24 return () => {
25 window.removeEventListener('visibilitychange', handler)
26 window.removeEventListener('focus', handler)
27 }
28 }
29})
30
31export const queryClient = new QueryClient({
32 defaultOptions: {
33 queries: {
34 // NOTE
35 // refetchOnWindowFocus breaks some UIs (like feeds)
36 // so we only selectively want to enable this
37 // -prf
38 refetchOnWindowFocus: false,
39 // Structural sharing between responses makes it impossible to rely on
40 // "first seen" timestamps on objects to determine if they're fresh.
41 // Disable this optimization so that we can rely on "first seen" timestamps.
42 structuralSharing: false,
43 // We don't want to retry queries by default, because in most cases we
44 // want to fail early and show a response to the user. There are
45 // exceptions, and those can be made on a per-query basis. For others, we
46 // should give users controls to retry.
47 retry: false,
48 },
49 },
50})