about summary refs log tree commit diff
path: root/src/lib/react-query.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2024-01-10 22:27:14 -0800
committerGitHub <noreply@github.com>2024-01-10 22:27:14 -0800
commit7ab4be6f7d5a89f752ebd989d9c730a9e135a989 (patch)
treeb6d45ac19bf83af827268fb3c282d6db3643dcc1 /src/lib/react-query.ts
parent0442dcc1a01a613985155c86a5ee042085553f33 (diff)
downloadvoidsky-7ab4be6f7d5a89f752ebd989d9c730a9e135a989.tar.zst
Reduce polling (#2465)
* Move profile and preference polling to polls-on-foreground

* Refetch prefs on feeds screen refresh since polling no longer occurs

* Reduce notifications polling by 50% if there's already an unread

* Disable feed polling if we know we have content

* Disable the hard refresh after 1 hour in case it's the cause of the random feed refresh bug

* Fix types
Diffstat (limited to 'src/lib/react-query.ts')
-rw-r--r--src/lib/react-query.ts32
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