Clean up the call once utils
This commit is contained in:
+3
-2
@@ -28,6 +28,7 @@ import {
|
|||||||
storePayloadForAccountSwitch,
|
storePayloadForAccountSwitch,
|
||||||
} from '#/lib/hooks/useNotificationHandler'
|
} from '#/lib/hooks/useNotificationHandler'
|
||||||
import {useWebScrollRestoration} from '#/lib/hooks/useWebScrollRestoration'
|
import {useWebScrollRestoration} from '#/lib/hooks/useWebScrollRestoration'
|
||||||
|
import {useCallOnce} from '#/lib/once'
|
||||||
import {buildStateObject} from '#/lib/routes/helpers'
|
import {buildStateObject} from '#/lib/routes/helpers'
|
||||||
import {
|
import {
|
||||||
type AllNavigatorParams,
|
type AllNavigatorParams,
|
||||||
@@ -41,7 +42,6 @@ import {
|
|||||||
type SearchTabNavigatorParams,
|
type SearchTabNavigatorParams,
|
||||||
type State,
|
type State,
|
||||||
} from '#/lib/routes/types'
|
} from '#/lib/routes/types'
|
||||||
import {useRunCallbackOnce} from '#/lib/runCallbackOnce'
|
|
||||||
import {bskyTitle} from '#/lib/strings/headings'
|
import {bskyTitle} from '#/lib/strings/headings'
|
||||||
import {useUnreadNotifications} from '#/state/queries/notifications/unread'
|
import {useUnreadNotifications} from '#/state/queries/notifications/unread'
|
||||||
import {useSession} from '#/state/session'
|
import {useSession} from '#/state/session'
|
||||||
@@ -980,7 +980,8 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const onNavigationReady = useRunCallbackOnce(() => {
|
const onNavigationReady = useCallOnce(() => {
|
||||||
|
alert('test')
|
||||||
prevLoggedRouteName.current = getCurrentRouteName()
|
prevLoggedRouteName.current = getCurrentRouteName()
|
||||||
handlePushNotificationEntry()
|
handlePushNotificationEntry()
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import {useCallback, useRef} from 'react'
|
||||||
|
|
||||||
|
type Cb = () => void
|
||||||
|
|
||||||
|
export function callOnce() {
|
||||||
|
let ran = false
|
||||||
|
return function runCallbackOnce(cb: Cb) {
|
||||||
|
if (ran) return
|
||||||
|
ran = true
|
||||||
|
cb()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useCallOnce(cb: Cb): () => void
|
||||||
|
export function useCallOnce(cb?: undefined): (cb: Cb) => void
|
||||||
|
export function useCallOnce(cb?: Cb) {
|
||||||
|
const ran = useRef(false)
|
||||||
|
return useCallback(
|
||||||
|
(icb: Cb) => {
|
||||||
|
if (ran.current) return
|
||||||
|
ran.current = true
|
||||||
|
if (icb) icb()
|
||||||
|
else if (cb) cb()
|
||||||
|
},
|
||||||
|
[cb],
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
import {useCallback, useRef} from 'react'
|
|
||||||
|
|
||||||
export function createRunCallbackOnce() {
|
|
||||||
let hasRun = false
|
|
||||||
return function runCallbackOnce(callback: () => void) {
|
|
||||||
if (!hasRun) {
|
|
||||||
hasRun = true
|
|
||||||
callback()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useRunCallbackOnce(callback: () => void) {
|
|
||||||
const hasRunRef = useRef(false)
|
|
||||||
return useCallback(() => {
|
|
||||||
if (!hasRunRef.current) {
|
|
||||||
hasRunRef.current = true
|
|
||||||
callback()
|
|
||||||
}
|
|
||||||
}, [callback])
|
|
||||||
}
|
|
||||||
@@ -2,7 +2,7 @@ import {useCallback, useState} from 'react'
|
|||||||
import {LayoutAnimationConfig} from 'react-native-reanimated'
|
import {LayoutAnimationConfig} from 'react-native-reanimated'
|
||||||
import {SafeAreaView} from 'react-native-safe-area-context'
|
import {SafeAreaView} from 'react-native-safe-area-context'
|
||||||
|
|
||||||
import {useRunCallbackOnce} from '#/lib/runCallbackOnce'
|
import {useCallOnce} from '#/lib/once'
|
||||||
import {FindContactsFlow} from '#/components/contacts/FindContactsFlow'
|
import {FindContactsFlow} from '#/components/contacts/FindContactsFlow'
|
||||||
import {type Action, type State} from '#/components/contacts/state'
|
import {type Action, type State} from '#/components/contacts/state'
|
||||||
import {ScreenTransition} from '#/components/ScreenTransition'
|
import {ScreenTransition} from '#/components/ScreenTransition'
|
||||||
@@ -19,9 +19,9 @@ export function StepFindContacts({
|
|||||||
const {dispatch} = useOnboardingInternalState()
|
const {dispatch} = useOnboardingInternalState()
|
||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
|
|
||||||
useRunCallbackOnce(() => {
|
useCallOnce(() => {
|
||||||
ax.metric('onboarding:contacts:begin', {})
|
ax.metric('onboarding:contacts:begin', {})
|
||||||
})
|
})()
|
||||||
|
|
||||||
const [transitionDirection, setTransitionDirection] = useState<
|
const [transitionDirection, setTransitionDirection] = useState<
|
||||||
'Forward' | 'Backward'
|
'Forward' | 'Backward'
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import {useLingui} from '@lingui/react'
|
|||||||
import {useQuery} from '@tanstack/react-query'
|
import {useQuery} from '@tanstack/react-query'
|
||||||
|
|
||||||
import {urls} from '#/lib/constants'
|
import {urls} from '#/lib/constants'
|
||||||
import {useRunCallbackOnce} from '#/lib/runCallbackOnce'
|
import {useCallOnce} from '#/lib/once'
|
||||||
import {atoms as a} from '#/alf'
|
import {atoms as a} from '#/alf'
|
||||||
import {Admonition} from '#/components/Admonition'
|
import {Admonition} from '#/components/Admonition'
|
||||||
import {Button, ButtonText} from '#/components/Button'
|
import {Button, ButtonText} from '#/components/Button'
|
||||||
@@ -25,9 +25,9 @@ export function StepFindContactsIntro() {
|
|||||||
const {_} = useLingui()
|
const {_} = useLingui()
|
||||||
const {dispatch} = useOnboardingInternalState()
|
const {dispatch} = useOnboardingInternalState()
|
||||||
|
|
||||||
useRunCallbackOnce(() => {
|
useCallOnce(() => {
|
||||||
ax.metric('onboarding:contacts:presented', {})
|
ax.metric('onboarding:contacts:presented', {})
|
||||||
})
|
})()
|
||||||
|
|
||||||
const {data: isAvailable, isSuccess} = useQuery({
|
const {data: isAvailable, isSuccess} = useQuery({
|
||||||
queryKey: ['contacts-available'],
|
queryKey: ['contacts-available'],
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import {useCallback, useMemo, useRef, useState} from 'react'
|
|||||||
import {type AppBskyUnspeccedGetPostThreadV2} from '@atproto/api'
|
import {type AppBskyUnspeccedGetPostThreadV2} from '@atproto/api'
|
||||||
import debounce from 'lodash.debounce'
|
import debounce from 'lodash.debounce'
|
||||||
|
|
||||||
import {OnceKey, useCallOnce} from '#/lib/hooks/useCallOnce'
|
import {useCallOnce} from '#/lib/once'
|
||||||
import {
|
import {
|
||||||
usePreferencesQuery,
|
usePreferencesQuery,
|
||||||
useSetThreadViewPreferencesMutation,
|
useSetThreadViewPreferencesMutation,
|
||||||
@@ -31,7 +31,7 @@ export function useThreadPreferences({
|
|||||||
const ax = useAnalytics()
|
const ax = useAnalytics()
|
||||||
const {data: preferences} = usePreferencesQuery()
|
const {data: preferences} = usePreferencesQuery()
|
||||||
const serverPrefs = preferences?.threadViewPrefs
|
const serverPrefs = preferences?.threadViewPrefs
|
||||||
const once = useCallOnce(OnceKey.PreferencesThread)
|
const once = useCallOnce()
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Create local state representations of server state
|
* Create local state representations of server state
|
||||||
|
|||||||
Reference in New Issue
Block a user