-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Simplify VirtualDetector/InterceptingGestureDetector and reduce the number of renders
#3813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
j-piasecki
wants to merge
11
commits into
next
Choose a base branch
from
@jpiasecki/optimize-virtual-detector
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
86423e1
Memoze event handlers
j-piasecki 9700adf
Simplify ref handling
j-piasecki 62e2a1a
Remove ref
j-piasecki ea3d095
Don't keep virtual callbacks in ref
j-piasecki 95b0d6f
Memo reanimated handlers
j-piasecki 22744bf
Fix `viewRef` passing
j-piasecki 8bde66d
Move validation to its own hook
j-piasecki a8549b0
Keep intercepting gesture detector value stable
j-piasecki 19ea1a9
Memoize intercepting detector props
j-piasecki 2403e06
Rename variable
j-piasecki cb724a5
Merge branch 'next' into @jpiasecki/optimize-virtual-detector
j-piasecki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| import React, { RefObject, useCallback, useRef, useState } from 'react'; | ||
| import React, { useCallback, useMemo, useState } from 'react'; | ||
| import HostGestureDetector from '../HostGestureDetector'; | ||
| import { | ||
| VirtualChildren, | ||
| VirtualChild, | ||
| GestureHandlerEvent, | ||
| DetectorCallbacks, | ||
| } from '../../types'; | ||
| import { DetectorContext } from './useDetectorContext'; | ||
| import { DetectorContext, DetectorContextValue } from './useDetectorContext'; | ||
| import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper'; | ||
| import { configureRelations, ensureNativeDetectorComponent } from '../utils'; | ||
| import { isComposedGesture } from '../../hooks/utils/relationUtils'; | ||
|
|
@@ -21,61 +21,57 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({ | |
| gesture, | ||
| children, | ||
| }: InterceptingGestureDetectorProps<THandlerData, TConfig>) { | ||
| const [virtualChildren, setVirtualChildren] = useState<VirtualChildren[]>([]); | ||
|
|
||
| const virtualMethods = useRef< | ||
| Map<number, RefObject<DetectorCallbacks<unknown>>> | ||
| >(new Map()); | ||
|
|
||
| const [shouldUseReanimated, setShouldUseReanimated] = useState( | ||
| gesture ? gesture.config.shouldUseReanimatedDetector : false | ||
| const [virtualChildren, setVirtualChildren] = useState<VirtualChild[]>([]); | ||
|
|
||
| const shouldUseReanimatedDetector = useMemo( | ||
| () => | ||
| virtualChildren.reduce( | ||
| (acc, child) => acc || child.forReanimated, | ||
| gesture?.config.shouldUseReanimatedDetector ?? false | ||
| ), | ||
| [virtualChildren, gesture] | ||
| ); | ||
| const [dispatchesAnimatedEvents, setDispatchesAnimatedEvents] = useState( | ||
| gesture ? gesture.config.dispatchesAnimatedEvents : false | ||
|
|
||
| const dispatchesAnimatedEvents = useMemo( | ||
| () => | ||
| virtualChildren.reduce( | ||
| (acc, child) => acc || child.forAnimated, | ||
| gesture?.config.dispatchesAnimatedEvents ?? false | ||
| ), | ||
| [virtualChildren, gesture] | ||
| ); | ||
|
|
||
| const NativeDetectorComponent = dispatchesAnimatedEvents | ||
| ? AnimatedNativeDetector | ||
| : shouldUseReanimated | ||
| : shouldUseReanimatedDetector | ||
| ? ReanimatedNativeDetector | ||
| : HostGestureDetector; | ||
|
|
||
| const register = useCallback( | ||
| ( | ||
| child: VirtualChildren, | ||
| methods: RefObject<DetectorCallbacks<unknown>>, | ||
| forReanimated: boolean | undefined, | ||
| forAnimated: boolean | undefined | ||
| ) => { | ||
| setShouldUseReanimated(!!forReanimated); | ||
| setDispatchesAnimatedEvents(!!forAnimated); | ||
|
|
||
| setVirtualChildren((prev) => { | ||
| const index = prev.findIndex((c) => c.viewTag === child.viewTag); | ||
| if (index !== -1) { | ||
| const updated = [...prev]; | ||
| updated[index] = child; | ||
| return updated; | ||
| } | ||
|
|
||
| return [...prev, child]; | ||
| }); | ||
|
|
||
| child.handlerTags.forEach((tag) => { | ||
| virtualMethods.current.set(tag, methods); | ||
| }); | ||
| }, | ||
| [] | ||
| ); | ||
| const register = useCallback((child: VirtualChild) => { | ||
| setVirtualChildren((prev) => { | ||
| const index = prev.findIndex((c) => c.viewTag === child.viewTag); | ||
| if (index !== -1) { | ||
| const updated = [...prev]; | ||
| updated[index] = child; | ||
| return updated; | ||
| } | ||
|
|
||
| const unregister = useCallback((childTag: number, handlerTags: number[]) => { | ||
| handlerTags.forEach((tag) => { | ||
| virtualMethods.current.delete(tag); | ||
| return [...prev, child]; | ||
| }); | ||
| }, []); | ||
|
|
||
| const unregister = useCallback((childTag: number) => { | ||
| setVirtualChildren((prev) => prev.filter((c) => c.viewTag !== childTag)); | ||
| }, []); | ||
|
|
||
| const contextValue: DetectorContextValue = useMemo( | ||
| () => ({ | ||
| register, | ||
| unregister, | ||
| }), | ||
| [register, unregister] | ||
| ); | ||
|
|
||
| // It might happen only with ReanimatedNativeDetector | ||
| if (!NativeDetectorComponent) { | ||
| throw new Error( | ||
|
|
@@ -85,20 +81,23 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({ | |
| ); | ||
| } | ||
|
|
||
| const handleGestureEvent = (key: keyof DetectorCallbacks<THandlerData>) => { | ||
| return (e: GestureHandlerEvent<THandlerData>) => { | ||
| if (gesture?.detectorCallbacks[key]) { | ||
| gesture.detectorCallbacks[key](e); | ||
| } | ||
|
|
||
| virtualMethods.current.forEach((ref) => { | ||
| const method = ref.current?.[key]; | ||
| if (method) { | ||
| method(e); | ||
| const createGestureEventHandler = useCallback( | ||
| (key: keyof DetectorCallbacks<THandlerData>) => { | ||
| return (e: GestureHandlerEvent<THandlerData>) => { | ||
| if (gesture?.detectorCallbacks[key]) { | ||
| gesture.detectorCallbacks[key](e); | ||
| } | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| virtualChildren.forEach((child) => { | ||
| const method = child.methods[key]; | ||
| if (method) { | ||
| method(e); | ||
| } | ||
| }); | ||
| }; | ||
| }, | ||
| [gesture, virtualChildren] | ||
| ); | ||
|
|
||
| const getHandlers = useCallback( | ||
| (key: keyof DetectorCallbacks<unknown>) => { | ||
|
|
@@ -112,8 +111,8 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({ | |
| ); | ||
| } | ||
|
|
||
| virtualMethods.current.forEach((ref) => { | ||
| const handler = ref.current?.[key]; | ||
| virtualChildren.forEach((child) => { | ||
| const handler = child.methods[key]; | ||
| if (handler) { | ||
| handlers.push( | ||
| handler as (e: GestureHandlerEvent<THandlerData>) => void | ||
|
|
@@ -126,14 +125,28 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({ | |
| [virtualChildren, gesture?.detectorCallbacks] | ||
| ); | ||
|
|
||
| const reanimatedUpdateEvents = useMemo( | ||
| () => getHandlers('onReanimatedUpdateEvent'), | ||
| [getHandlers] | ||
| ); | ||
| const reanimatedEventHandler = Reanimated?.useComposedEventHandler( | ||
| getHandlers('onReanimatedUpdateEvent') | ||
| reanimatedUpdateEvents | ||
| ); | ||
|
|
||
| const reanimatedStateChangeEvents = useMemo( | ||
| () => getHandlers('onReanimatedStateChange'), | ||
| [getHandlers] | ||
| ); | ||
| const reanimatedStateChangeHandler = Reanimated?.useComposedEventHandler( | ||
| getHandlers('onReanimatedStateChange') | ||
| reanimatedStateChangeEvents | ||
| ); | ||
|
|
||
| const reanimatedTouchEvents = useMemo( | ||
| () => getHandlers('onReanimatedTouchEvent'), | ||
| [getHandlers] | ||
| ); | ||
| const reanimatedTouchEventHandler = Reanimated?.useComposedEventHandler( | ||
| getHandlers('onReanimatedTouchEvent') | ||
| reanimatedTouchEvents | ||
| ); | ||
|
|
||
| ensureNativeDetectorComponent(NativeDetectorComponent); | ||
|
|
@@ -142,42 +155,48 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({ | |
| configureRelations(gesture); | ||
| } | ||
|
|
||
| const handlerTags = useMemo(() => { | ||
| if (gesture) { | ||
| return isComposedGesture(gesture) ? gesture.tags : [gesture.tag]; | ||
| } | ||
| return []; | ||
| }, [gesture]); | ||
|
|
||
| return ( | ||
| <DetectorContext value={{ register, unregister }}> | ||
| <DetectorContext value={contextValue}> | ||
| <NativeDetectorComponent | ||
| // @ts-ignore This is a type mismatch between RNGH types and RN Codegen types | ||
| onGestureHandlerStateChange={handleGestureEvent( | ||
| 'onGestureHandlerStateChange' | ||
| onGestureHandlerStateChange={useMemo( | ||
| () => createGestureEventHandler('onGestureHandlerStateChange'), | ||
| [createGestureEventHandler] | ||
| )} | ||
| // @ts-ignore This is a type mismatch between RNGH types and RN Codegen types | ||
| onGestureHandlerEvent={handleGestureEvent('onGestureHandlerEvent')} | ||
| onGestureHandlerEvent={useMemo( | ||
| () => createGestureEventHandler('onGestureHandlerEvent'), | ||
| [createGestureEventHandler] | ||
| )} | ||
| // @ts-ignore This is a type mismatch between RNGH types and RN Codegen types | ||
| onGestureHandlerAnimatedEvent={ | ||
| gesture?.detectorCallbacks.onGestureHandlerAnimatedEvent | ||
| } | ||
|
Comment on lines
179
to
181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the new approach with useMemo( () => createGestureEventHandler allow us to allow support for AnimatedEvents? |
||
| // @ts-ignore This is a type mismatch between RNGH types and RN Codegen types | ||
| onGestureHandlerTouchEvent={handleGestureEvent( | ||
| 'onGestureHandlerTouchEvent' | ||
| onGestureHandlerTouchEvent={useMemo( | ||
| () => createGestureEventHandler('onGestureHandlerTouchEvent'), | ||
| [createGestureEventHandler] | ||
| )} | ||
| // @ts-ignore This is a type mismatch between RNGH types and RN Codegen types | ||
| onGestureHandlerReanimatedStateChange={ | ||
| shouldUseReanimated ? reanimatedStateChangeHandler : undefined | ||
| shouldUseReanimatedDetector ? reanimatedStateChangeHandler : undefined | ||
| } | ||
| // @ts-ignore This is a type mismatch between RNGH types and RN Codegen types | ||
| onGestureHandlerReanimatedEvent={ | ||
| shouldUseReanimated ? reanimatedEventHandler : undefined | ||
| shouldUseReanimatedDetector ? reanimatedEventHandler : undefined | ||
| } | ||
| // @ts-ignore This is a type mismatch between RNGH types and RN Codegen types | ||
| onGestureHandlerReanimatedTouchEvent={ | ||
| shouldUseReanimated ? reanimatedTouchEventHandler : undefined | ||
| } | ||
| handlerTags={ | ||
| gesture | ||
| ? isComposedGesture(gesture) | ||
| ? gesture.tags | ||
| : [gesture.tag] | ||
| : [] | ||
| shouldUseReanimatedDetector ? reanimatedTouchEventHandler : undefined | ||
| } | ||
| handlerTags={handlerTags} | ||
| style={nativeDetectorStyles.detector} | ||
| virtualChildren={virtualChildren} | ||
| moduleId={globalThis._RNGH_MODULE_ID}> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 6 additions & 11 deletions
17
packages/react-native-gesture-handler/src/v3/detectors/VirtualDetector/useDetectorContext.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we throw an error when both
shouldUseReanimatedDetectoranddispatchesAnimatedEventsare true?