Skip to content

Commit 590a5cd

Browse files
committed
resolved workaround
1 parent 1d96fa1 commit 590a5cd

File tree

2 files changed

+49
-71
lines changed

2 files changed

+49
-71
lines changed

packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ import {
1010
} from '../types';
1111
import { DetectorContext } from './useDetectorContext';
1212
import { Reanimated } from '../../handlers/gestures/reanimatedWrapper';
13-
import {
14-
configureRelations,
15-
getHandlerTag,
16-
invokeDetectorEvent,
17-
} from './utils';
13+
import { configureRelations } from './utils';
1814
import { isComposedGesture } from '../hooks/utils/relationUtils';
1915

2016
export interface NativeDetectorProps {
@@ -77,19 +73,53 @@ export function NativeDetector({ gesture, children }: NativeDetectorProps) {
7773

7874
const handleGestureEvent = (key: keyof GestureEvents<unknown>) => {
7975
return (e: GestureHandlerEvent<unknown>) => {
80-
const handlerTag = getHandlerTag(e);
81-
82-
const method = !logicMethods.current.has(handlerTag)
83-
? gesture.gestureEvents[key]
84-
: logicMethods.current.get(handlerTag)?.current?.[key];
85-
86-
invokeDetectorEvent(
87-
method as (e: GestureHandlerEvent<unknown>) => void,
88-
e
89-
);
76+
if (gesture.gestureEvents[key]) {
77+
gesture.gestureEvents[key](e);
78+
}
79+
80+
logicMethods.current.forEach((ref) => {
81+
const method = ref.current?.[key];
82+
if (method) {
83+
method(e);
84+
}
85+
});
9086
};
9187
};
9288

89+
const getHandlers = useCallback(
90+
(key: keyof GestureEvents<unknown>) => {
91+
const handlers: ((e: GestureHandlerEvent<unknown>) => void)[] = [];
92+
93+
if (gesture.gestureEvents[key]) {
94+
handlers.push(
95+
gesture.gestureEvents[key] as (
96+
e: GestureHandlerEvent<unknown>
97+
) => void
98+
);
99+
}
100+
101+
logicMethods.current.forEach((ref) => {
102+
const handler = ref.current?.[key];
103+
if (handler) {
104+
handlers.push(handler as (e: GestureHandlerEvent<unknown>) => void);
105+
}
106+
});
107+
108+
return handlers;
109+
},
110+
[logicChildren, gesture.gestureEvents]
111+
);
112+
113+
const reanimatedEventHandler = Reanimated?.useComposedEventHandler(
114+
getHandlers('onReanimatedUpdateEvent')
115+
);
116+
const reanimedStateChangeHandler = Reanimated?.useComposedEventHandler(
117+
getHandlers('onReanimatedStateChange')
118+
);
119+
const reanimatedTouchEventHandler = Reanimated?.useComposedEventHandler(
120+
getHandlers('onReanimatedTouchEvent')
121+
);
122+
93123
return (
94124
<DetectorContext.Provider value={{ register, unregister }}>
95125
<NativeDetectorComponent
@@ -108,17 +138,11 @@ export function NativeDetector({ gesture, children }: NativeDetectorProps) {
108138
'onGestureHandlerTouchEvent'
109139
)}
110140
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
111-
onGestureHandlerReanimatedStateChange={handleGestureEvent(
112-
'onReanimatedStateChange'
113-
)}
141+
onGestureHandlerReanimatedStateChange={reanimatedEventHandler}
114142
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
115-
onGestureHandlerReanimatedEvent={handleGestureEvent(
116-
'onReanimatedUpdateEvent'
117-
)}
143+
onGestureHandlerReanimatedEvent={reanimedStateChangeHandler}
118144
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
119-
onGestureHandlerReanimatedTouchEvent={handleGestureEvent(
120-
'onReanimatedTouchEvent'
121-
)}
145+
onGestureHandlerReanimatedTouchEvent={reanimatedTouchEventHandler}
122146
moduleId={globalThis._RNGH_MODULE_ID}
123147
handlerTags={isComposedGesture(gesture) ? gesture.tags : [gesture.tag]}
124148
style={styles.detector}

packages/react-native-gesture-handler/src/v3/NativeDetector/utils.ts

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
// For `simultaneousHandlers` we use Set as the order doesn't matter.
66

77
import RNGestureHandlerModule from '../../RNGestureHandlerModule';
8-
import { tagMessage } from '../../utils';
98
import {
109
isComposedGesture,
1110
prepareRelations,
1211
} from '../hooks/utils/relationUtils';
13-
import { ComposedGestureName, Gesture, GestureHandlerEvent } from '../types';
12+
import { ComposedGestureName, Gesture } from '../types';
1413

1514
// The tree consists of ComposedGestures and NativeGestures. NativeGestures are always leaf nodes.
1615
export const traverseAndConfigureRelations = (
@@ -146,48 +145,3 @@ export function configureRelations(gesture: Gesture) {
146145
);
147146
}
148147
}
149-
150-
// Some event handlers are plain functions, whereas those marked as worklets
151-
// are wrapped in objects under eventMethod.workletEventHandler.worklet.
152-
// This function normalises invocation so that both forms can be called safely.
153-
// Note: this worklet unpacking is essentially a workaround since we need to
154-
// decide on the JS side which handle logic to execute.
155-
export function invokeDetectorEvent<T>(
156-
method:
157-
| ((event: GestureHandlerEvent<T>) => void)
158-
| {
159-
workletEventHandler: {
160-
worklet: (event: GestureHandlerEvent<T>) => void;
161-
};
162-
}
163-
| null
164-
| undefined,
165-
event: GestureHandlerEvent<T>
166-
): void {
167-
if (!method) {
168-
return;
169-
}
170-
171-
if (typeof method === 'function') {
172-
method(event);
173-
return;
174-
}
175-
176-
if ('workletEventHandler' in method) {
177-
if ('worklet' in method.workletEventHandler) {
178-
invokeDetectorEvent(method.workletEventHandler.worklet, event);
179-
}
180-
return;
181-
}
182-
}
183-
184-
export function getHandlerTag<T>(e: GestureHandlerEvent<T>): number {
185-
if ('nativeEvent' in e) {
186-
return (e.nativeEvent as any).handlerTag;
187-
}
188-
if ('handlerTag' in e) {
189-
return (e as any).handlerTag;
190-
}
191-
192-
throw new Error(tagMessage('Cannot extract handler tag from event'));
193-
}

0 commit comments

Comments
 (0)