···2import {Platform} from 'react-native'
3import {AppState, AppStateStatus} from 'react-native'
4import {sha256} from 'js-sha256'
5-import {
6- Statsig,
7- StatsigProvider,
8- useGate as useStatsigGate,
9-} from 'statsig-react-native-expo'
1011import {logger} from '#/logger'
12import {isWeb} from '#/platform/detection'
···98 }
99}
100101-export function useGate(gateName: Gate): boolean {
102- const {isLoading, value} = useStatsigGate(gateName)
103- if (isLoading) {
104- // This should not happen because of waitForInitialization={true}.
105- console.error('Did not expected isLoading to ever be true.')
106 }
107- // This shouldn't technically be necessary but let's get a strong
108- // guarantee that a gate value can never change while mounted.
109- const [initialValue] = React.useState(value)
110- return initialValue
000000000111}
112113function toStatsigUser(did: string | undefined): StatsigUser {
···2import {Platform} from 'react-native'
3import {AppState, AppStateStatus} from 'react-native'
4import {sha256} from 'js-sha256'
5+import {Statsig, StatsigProvider} from 'statsig-react-native-expo'
000067import {logger} from '#/logger'
8import {isWeb} from '#/platform/detection'
···94 }
95}
9697+export function useGate(): (gateName: Gate) => boolean {
98+ const cache = React.useRef<Map<Gate, boolean>>()
99+ if (cache.current === undefined) {
100+ cache.current = new Map()
0101 }
102+ const gate = React.useCallback((gateName: Gate): boolean => {
103+ // TODO: Replace local cache with a proper session one.
104+ const cachedValue = cache.current!.get(gateName)
105+ if (cachedValue !== undefined) {
106+ return cachedValue
107+ }
108+ const value = Statsig.initializeCalled()
109+ ? Statsig.checkGate(gateName)
110+ : false
111+ cache.current!.set(gateName, value)
112+ return value
113+ }, [])
114+ return gate
115}
116117function toStatsigUser(did: string | undefined): StatsigUser {