[Statsig] Slightly block the UI on gates (#3608)

This commit is contained in:
dan 2024-04-18 17:53:51 +01:00 committed by GitHub
parent 6101c32bd9
commit bef7d8a325
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 1 deletions

View file

@ -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