about summary refs log tree commit diff
path: root/src/lib/statsig
diff options
context:
space:
mode:
authordan <dan.abramov@gmail.com>2024-04-18 17:53:51 +0100
committerGitHub <noreply@github.com>2024-04-18 17:53:51 +0100
commitbef7d8a325f2bf63fd096093fe1b0eac05c711a3 (patch)
treeebac0789406d4c29306434380b719ece4a9707da /src/lib/statsig
parent6101c32bd992c871497203bcb6d509ba4610fea5 (diff)
downloadvoidsky-bef7d8a325f2bf63fd096093fe1b0eac05c711a3.tar.zst
[Statsig] Slightly block the UI on gates (#3608)
Diffstat (limited to 'src/lib/statsig')
-rw-r--r--src/lib/statsig/statsig.tsx26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx
index 151b365d3..c43d2bf8a 100644
--- a/src/lib/statsig/statsig.tsx
+++ b/src/lib/statsig/statsig.tsx
@@ -8,6 +8,7 @@ import {logger} from '#/logger'
 import {isWeb} from '#/platform/detection'
 import {IS_TESTFLIGHT} from 'lib/app-info'
 import {useSession} from '../../state/session'
+import {timeout} from '../async/timeout'
 import {useNonReactiveCallback} from '../hooks/useNonReactiveCallback'
 import {LogEvents} from './events'
 import {Gate} from './gates'
@@ -164,6 +165,31 @@ AppState.addEventListener('change', (state: AppStateStatus) => {
   }
 })
 
+export async function tryFetchGates(
+  did: string,
+  strategy: 'prefer-low-latency' | 'prefer-fresh-gates',
+) {
+  try {
+    let timeoutMs = 250 // Don't block the UI if we can't do this fast.
+    if (strategy === 'prefer-fresh-gates') {
+      // Use this for less common operations where the user would be OK with a delay.
+      timeoutMs = 1500
+    }
+    // Note: This condition is currently false the very first render because
+    // Statsig has not initialized yet. In the future, we can fix this by
+    // doing the initialization ourselves instead of relying on the provider.
+    if (Statsig.initializeCalled()) {
+      await Promise.race([
+        timeout(timeoutMs),
+        Statsig.prefetchUsers([toStatsigUser(did)]),
+      ])
+    }
+  } catch (e) {
+    // Don't leak errors to the calling code, this is meant to be always safe.
+    console.error(e)
+  }
+}
+
 export function Provider({children}: {children: React.ReactNode}) {
   const {currentAccount, accounts} = useSession()
   const did = currentAccount?.did