1- import React , { useCallback , useRef , useState } from 'react' ;
1+ import React , { useCallback , useMemo , useState } from 'react' ;
22import HostGestureDetector from '../HostGestureDetector' ;
33import {
4- VirtualChildren ,
4+ VirtualChild ,
55 GestureHandlerEvent ,
66 DetectorCallbacks ,
77} from '../../types' ;
@@ -21,17 +21,24 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
2121 gesture,
2222 children,
2323} : InterceptingGestureDetectorProps < THandlerData , TConfig > ) {
24- const [ virtualChildren , setVirtualChildren ] = useState < VirtualChildren [ ] > ( [ ] ) ;
25-
26- const virtualMethods = useRef < Map < number , DetectorCallbacks < unknown > > > (
27- new Map ( )
24+ const [ virtualChildren , setVirtualChildren ] = useState < VirtualChild [ ] > ( [ ] ) ;
25+
26+ const shouldUseReanimated = useMemo (
27+ ( ) =>
28+ virtualChildren . reduce (
29+ ( acc , child ) => acc || child . forReanimated ,
30+ gesture ?. config . shouldUseReanimatedDetector ?? false
31+ ) ,
32+ [ virtualChildren , gesture ]
2833 ) ;
2934
30- const [ shouldUseReanimated , setShouldUseReanimated ] = useState (
31- gesture ? gesture . config . shouldUseReanimatedDetector : false
32- ) ;
33- const [ dispatchesAnimatedEvents , setDispatchesAnimatedEvents ] = useState (
34- gesture ? gesture . config . dispatchesAnimatedEvents : false
35+ const dispatchesAnimatedEvents = useMemo (
36+ ( ) =>
37+ virtualChildren . reduce (
38+ ( acc , child ) => acc || child . forAnimated ,
39+ gesture ?. config . dispatchesAnimatedEvents ?? false
40+ ) ,
41+ [ virtualChildren , gesture ]
3542 ) ;
3643
3744 const NativeDetectorComponent = dispatchesAnimatedEvents
@@ -40,41 +47,20 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
4047 ? ReanimatedNativeDetector
4148 : HostGestureDetector ;
4249
43- const register = useCallback (
44- (
45- child : VirtualChildren ,
46- methods : DetectorCallbacks < unknown > ,
47- forReanimated : boolean | undefined ,
48- forAnimated : boolean | undefined
49- ) => {
50- // console.log('[IGD] Registering virtual detector', child.viewTag, child.handlerTags);
51- setShouldUseReanimated ( ! ! forReanimated ) ;
52- setDispatchesAnimatedEvents ( ! ! forAnimated ) ;
53-
54- setVirtualChildren ( ( prev ) => {
55- const index = prev . findIndex ( ( c ) => c . viewTag === child . viewTag ) ;
56- if ( index !== - 1 ) {
57- const updated = [ ...prev ] ;
58- updated [ index ] = child ;
59- return updated ;
60- }
61-
62- return [ ...prev , child ] ;
63- } ) ;
64-
65- child . handlerTags . forEach ( ( tag ) => {
66- virtualMethods . current . set ( tag , methods ) ;
67- } ) ;
68- } ,
69- [ ]
70- ) ;
50+ const register = useCallback ( ( child : VirtualChild ) => {
51+ setVirtualChildren ( ( prev ) => {
52+ const index = prev . findIndex ( ( c ) => c . viewTag === child . viewTag ) ;
53+ if ( index !== - 1 ) {
54+ const updated = [ ...prev ] ;
55+ updated [ index ] = child ;
56+ return updated ;
57+ }
7158
72- const unregister = useCallback ( ( childTag : number , handlerTags : number [ ] ) => {
73- // console.log('[IGD] Unregistering virtual detector', childTag, handlerTags);
74- handlerTags . forEach ( ( tag ) => {
75- virtualMethods . current . delete ( tag ) ;
59+ return [ ...prev , child ] ;
7660 } ) ;
61+ } , [ ] ) ;
7762
63+ const unregister = useCallback ( ( childTag : number ) => {
7864 setVirtualChildren ( ( prev ) => prev . filter ( ( c ) => c . viewTag !== childTag ) ) ;
7965 } , [ ] ) ;
8066
@@ -93,8 +79,8 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
9379 gesture . detectorCallbacks [ key ] ( e ) ;
9480 }
9581
96- virtualMethods . current . forEach ( ( callbacks ) => {
97- const method = callbacks [ key ] ;
82+ virtualChildren . forEach ( ( child ) => {
83+ const method = child . methods [ key ] ;
9884 if ( method ) {
9985 method ( e ) ;
10086 }
@@ -114,8 +100,8 @@ export function InterceptingGestureDetector<THandlerData, TConfig>({
114100 ) ;
115101 }
116102
117- virtualMethods . current . forEach ( ( callbacks ) => {
118- const handler = callbacks [ key ] ;
103+ virtualChildren . forEach ( ( child ) => {
104+ const handler = child . methods [ key ] ;
119105 if ( handler ) {
120106 handlers . push (
121107 handler as ( e : GestureHandlerEvent < THandlerData > ) => void
0 commit comments