@@ -3,6 +3,7 @@ import { View } from 'react-native';
3
3
import RNGestureHandlerModule from '../RNGestureHandlerModule.web' ;
4
4
import { ActionType } from '../ActionType' ;
5
5
import { PropsRef } from '../web/interfaces' ;
6
+
6
7
export interface GestureHandlerDetectorProps extends PropsRef {
7
8
handlerTags : number [ ] ;
8
9
dispatchesAnimatedEvents : boolean ;
@@ -15,33 +16,47 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
15
16
16
17
const viewRef = useRef ( null ) ;
17
18
const propsRef = useRef < PropsRef > ( props ) ;
19
+ const attachedHandlerTags = useRef < Set < number > > ( new Set < number > ( ) ) ;
18
20
19
- const detachHandlers = useCallback ( ( ) => {
20
- handlerTags . forEach ( ( tag ) => {
21
+ const detachHandlers = useCallback ( ( oldHandlerTags : Set < number > ) => {
22
+ oldHandlerTags . forEach ( ( tag ) => {
21
23
RNGestureHandlerModule . detachGestureHandler ( tag ) ;
22
24
} ) ;
23
- } , [ handlerTags ] ) ;
24
-
25
- const attachHandlers = useCallback ( ( ) => {
26
- // TODO: add memoisation
27
-
28
- handlerTags . forEach ( ( tag ) => {
29
- RNGestureHandlerModule . attachGestureHandler (
30
- tag ,
31
- viewRef . current ,
32
- ActionType . NATIVE_DETECTOR ,
33
- propsRef ,
34
- dispatchesAnimatedEvents
25
+ } , [ ] ) ;
26
+
27
+ const attachHandlers = useCallback (
28
+ ( currentHandlerTags : Set < number > ) => {
29
+ const oldHandlerTags =
30
+ attachedHandlerTags . current . difference ( currentHandlerTags ) ;
31
+ const newHandlerTags = currentHandlerTags . difference (
32
+ attachedHandlerTags . current
35
33
) ;
36
- } ) ;
37
- } , [ handlerTags , dispatchesAnimatedEvents ] ) ;
34
+
35
+ detachHandlers ( oldHandlerTags ) ;
36
+
37
+ newHandlerTags . forEach ( ( tag ) => {
38
+ RNGestureHandlerModule . attachGestureHandler (
39
+ tag ,
40
+ viewRef . current ,
41
+ ActionType . NATIVE_DETECTOR ,
42
+ propsRef ,
43
+ dispatchesAnimatedEvents
44
+ ) ;
45
+ } ) ;
46
+ attachedHandlerTags . current = currentHandlerTags ;
47
+ } ,
48
+ [ dispatchesAnimatedEvents ]
49
+ ) ;
50
+
51
+ useEffect ( ( ) => {
52
+ attachHandlers ( new Set ( handlerTags ) ) ;
53
+ } , [ attachHandlers ] ) ;
38
54
39
55
useEffect ( ( ) => {
40
- attachHandlers ( ) ;
41
56
return ( ) => {
42
- detachHandlers ( ) ;
57
+ detachHandlers ( attachedHandlerTags . current ) ;
43
58
} ;
44
- } , [ attachHandlers , detachHandlers ] ) ;
59
+ } , [ ] ) ;
45
60
46
61
return (
47
62
< View style = { { display : 'contents' } } ref = { viewRef } >
0 commit comments