Dedupe navigation events (push, navigate, pop, etc) (#3179)
This commit is contained in:
parent
b8afb935f4
commit
ee57d74765
7 changed files with 118 additions and 25 deletions
17
src/lib/hooks/useDedupe.ts
Normal file
17
src/lib/hooks/useDedupe.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import React from 'react'
|
||||
|
||||
export const useDedupe = () => {
|
||||
const canDo = React.useRef(true)
|
||||
|
||||
return React.useRef((cb: () => unknown) => {
|
||||
if (canDo.current) {
|
||||
canDo.current = false
|
||||
setTimeout(() => {
|
||||
canDo.current = true
|
||||
}, 250)
|
||||
cb()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}).current
|
||||
}
|
80
src/lib/hooks/useNavigationDeduped.ts
Normal file
80
src/lib/hooks/useNavigationDeduped.ts
Normal file
|
@ -0,0 +1,80 @@
|
|||
import React from 'react'
|
||||
import {useNavigation} from '@react-navigation/core'
|
||||
import {AllNavigatorParams, NavigationProp} from 'lib/routes/types'
|
||||
import type {NavigationAction} from '@react-navigation/routers'
|
||||
import {NavigationState} from '@react-navigation/native'
|
||||
import {useDedupe} from 'lib/hooks/useDedupe'
|
||||
|
||||
export type DebouncedNavigationProp = Pick<
|
||||
NavigationProp,
|
||||
| 'popToTop'
|
||||
| 'push'
|
||||
| 'navigate'
|
||||
| 'canGoBack'
|
||||
| 'replace'
|
||||
| 'dispatch'
|
||||
| 'goBack'
|
||||
| 'getState'
|
||||
>
|
||||
|
||||
export function useNavigationDeduped() {
|
||||
const navigation = useNavigation<NavigationProp>()
|
||||
const dedupe = useDedupe()
|
||||
|
||||
return React.useMemo(
|
||||
(): DebouncedNavigationProp => ({
|
||||
// Types from @react-navigation/routers/lib/typescript/src/StackRouter.ts
|
||||
push: <RouteName extends keyof AllNavigatorParams>(
|
||||
...args: undefined extends AllNavigatorParams[RouteName]
|
||||
?
|
||||
| [screen: RouteName]
|
||||
| [screen: RouteName, params: AllNavigatorParams[RouteName]]
|
||||
: [screen: RouteName, params: AllNavigatorParams[RouteName]]
|
||||
) => {
|
||||
dedupe(() => navigation.push(...args))
|
||||
},
|
||||
// Types from @react-navigation/core/src/types.tsx
|
||||
navigate: <RouteName extends keyof AllNavigatorParams>(
|
||||
...args: RouteName extends unknown
|
||||
? undefined extends AllNavigatorParams[RouteName]
|
||||
?
|
||||
| [screen: RouteName]
|
||||
| [screen: RouteName, params: AllNavigatorParams[RouteName]]
|
||||
: [screen: RouteName, params: AllNavigatorParams[RouteName]]
|
||||
: never
|
||||
) => {
|
||||
dedupe(() => navigation.navigate(...args))
|
||||
},
|
||||
// Types from @react-navigation/routers/lib/typescript/src/StackRouter.ts
|
||||
replace: <RouteName extends keyof AllNavigatorParams>(
|
||||
...args: undefined extends AllNavigatorParams[RouteName]
|
||||
?
|
||||
| [screen: RouteName]
|
||||
| [screen: RouteName, params: AllNavigatorParams[RouteName]]
|
||||
: [screen: RouteName, params: AllNavigatorParams[RouteName]]
|
||||
) => {
|
||||
dedupe(() => navigation.replace(...args))
|
||||
},
|
||||
dispatch: (
|
||||
action:
|
||||
| NavigationAction
|
||||
| ((state: NavigationState) => NavigationAction),
|
||||
) => {
|
||||
dedupe(() => navigation.dispatch(action))
|
||||
},
|
||||
popToTop: () => {
|
||||
dedupe(() => navigation.popToTop())
|
||||
},
|
||||
goBack: () => {
|
||||
dedupe(() => navigation.goBack())
|
||||
},
|
||||
canGoBack: () => {
|
||||
return navigation.canGoBack()
|
||||
},
|
||||
getState: () => {
|
||||
return navigation.getState()
|
||||
},
|
||||
}),
|
||||
[dedupe, navigation],
|
||||
)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue