Skip to content

Commit 2353a84

Browse files
committed
readded memoisation
1 parent 9c17a96 commit 2353a84

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

packages/react-native-gesture-handler/src/v3/HostGestureDetector.web.tsx

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { View } from 'react-native';
33
import RNGestureHandlerModule from '../RNGestureHandlerModule.web';
44
import { ActionType } from '../ActionType';
55
import { PropsRef } from '../web/interfaces';
6+
67
export interface GestureHandlerDetectorProps extends PropsRef {
78
handlerTags: number[];
89
dispatchesAnimatedEvents: boolean;
@@ -15,33 +16,47 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => {
1516

1617
const viewRef = useRef(null);
1718
const propsRef = useRef<PropsRef>(props);
19+
const attachedHandlerTags = useRef<Set<number>>(new Set<number>());
1820

19-
const detachHandlers = useCallback(() => {
20-
handlerTags.forEach((tag) => {
21+
const detachHandlers = useCallback((oldHandlerTags: Set<number>) => {
22+
oldHandlerTags.forEach((tag) => {
2123
RNGestureHandlerModule.detachGestureHandler(tag);
2224
});
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
3533
);
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]);
3854

3955
useEffect(() => {
40-
attachHandlers();
4156
return () => {
42-
detachHandlers();
57+
detachHandlers(attachedHandlerTags.current);
4358
};
44-
}, [attachHandlers, detachHandlers]);
59+
}, []);
4560

4661
return (
4762
<View style={{ display: 'contents' }} ref={viewRef}>

0 commit comments

Comments
 (0)