diff options
author | dan <dan.abramov@gmail.com> | 2024-04-18 04:39:29 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-18 04:39:29 +0100 |
commit | 02becdf4491cded0f0435e880e1ad4030d500403 (patch) | |
tree | 725ffa94609c380d4a9d8f25609642eb7d4b4748 /src/lib | |
parent | 086dc93a7a6e69b0df2ed084ee68bb4e26c13f88 (diff) | |
download | voidsky-02becdf4491cded0f0435e880e1ad4030d500403.tar.zst |
[Statsig] Make gate checks lazily (#3594)
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/hooks/useOTAUpdates.ts | 4 | ||||
-rw-r--r-- | src/lib/statsig/statsig.tsx | 32 |
2 files changed, 20 insertions, 16 deletions
diff --git a/src/lib/hooks/useOTAUpdates.ts b/src/lib/hooks/useOTAUpdates.ts index 70905c137..b8d331c6f 100644 --- a/src/lib/hooks/useOTAUpdates.ts +++ b/src/lib/hooks/useOTAUpdates.ts @@ -31,8 +31,8 @@ async function setExtraParams() { } export function useOTAUpdates() { - const shouldReceiveUpdates = - useGate('receive_updates') && isEnabled && !__DEV__ + const gate = useGate() + const shouldReceiveUpdates = isEnabled && !__DEV__ && gate('receive_updates') const appState = React.useRef<AppStateStatus>('active') const lastMinimize = React.useRef(0) diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx index df540d79e..62dd79bb2 100644 --- a/src/lib/statsig/statsig.tsx +++ b/src/lib/statsig/statsig.tsx @@ -2,11 +2,7 @@ import React from 'react' import {Platform} from 'react-native' import {AppState, AppStateStatus} from 'react-native' import {sha256} from 'js-sha256' -import { - Statsig, - StatsigProvider, - useGate as useStatsigGate, -} from 'statsig-react-native-expo' +import {Statsig, StatsigProvider} from 'statsig-react-native-expo' import {logger} from '#/logger' import {isWeb} from '#/platform/detection' @@ -98,16 +94,24 @@ export function logEvent<E extends keyof LogEvents>( } } -export function useGate(gateName: Gate): boolean { - const {isLoading, value} = useStatsigGate(gateName) - if (isLoading) { - // This should not happen because of waitForInitialization={true}. - console.error('Did not expected isLoading to ever be true.') +export function useGate(): (gateName: Gate) => boolean { + const cache = React.useRef<Map<Gate, boolean>>() + if (cache.current === undefined) { + cache.current = new Map() } - // This shouldn't technically be necessary but let's get a strong - // guarantee that a gate value can never change while mounted. - const [initialValue] = React.useState(value) - return initialValue + const gate = React.useCallback((gateName: Gate): boolean => { + // TODO: Replace local cache with a proper session one. + const cachedValue = cache.current!.get(gateName) + if (cachedValue !== undefined) { + return cachedValue + } + const value = Statsig.initializeCalled() + ? Statsig.checkGate(gateName) + : false + cache.current!.set(gateName, value) + return value + }, []) + return gate } function toStatsigUser(did: string | undefined): StatsigUser { |