Skip to content

Commit aed7c4c

Browse files
authored
[Web] Remove invokeNullableMethod (#4084)
## Description This PR removes `invokeNullableMethod` function from `web`. From what I've found, this function is no longer necessary and makes web codebase less readable. On web we only deal with functions. The only exception would be an `Animated.event`, but this can be an object iff `useNativeDriver` is set to `true`. However, [this is not supported on `web`](https://necolas.github.io/react-native-web/docs/react-native-compatibility/?utm_source=openai). Also, part of the function refers to the old Reanimated 1 API, which is no longer supported. My findings are that calling the method when it exists is enough. If not, we are still in beta phase of v3 so we can bring it back if it turns out that it was necessary. ## Test plan Tested on expo example on web. <details> <summary>Tested on the following Animated.event example:</summary> ```tsx import * as React from 'react'; import { Animated, Button } from 'react-native'; import { GestureHandlerRootView, GestureDetector, usePanGesture, } from 'react-native-gesture-handler'; function useAnimatedValue( initialValue: number, config?: Animated.AnimatedConfig ): Animated.Value { const ref = React.useRef<null | Animated.Value>(null); if (ref.current == null) { ref.current = new Animated.Value(initialValue, config); } return ref.current; } export default function App() { const [visible, setVisible] = React.useState(true); const value = useAnimatedValue(0); const event = Animated.event( [{ nativeEvent: { handlerData: { translationX: value } } }], { useNativeDriver: true, } ); const gesture = usePanGesture({ onUpdate: event, useAnimated: true, }); return ( <GestureHandlerRootView style={{ flex: 1, backgroundColor: 'white', paddingTop: 8 }}> <Button title="Toggle visibility" onPress={() => { setVisible(!visible); }} /> {visible && ( <GestureDetector gesture={gesture}> <Animated.View style={[ { width: 150, height: 150, backgroundColor: 'blue', opacity: 0.5, borderWidth: 10, borderColor: 'green', marginTop: 20, marginLeft: 40, }, { transform: [{ translateX: value }] }, ]} /> </GestureDetector> )} </GestureHandlerRootView> ); } ``` </details>
1 parent dac0e6a commit aed7c4c

1 file changed

Lines changed: 35 additions & 89 deletions

File tree

packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts

Lines changed: 35 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,15 @@ export default abstract class GestureHandler implements IGestureHandler {
404404
return;
405405
}
406406

407-
if (usesNativeOrVirtualDetector(this.actionType)) {
408-
invokeNullableMethod(
409-
this.forReanimated
410-
? onGestureHandlerReanimatedTouchEvent
411-
: onGestureHandlerTouchEvent,
412-
touchEvent
413-
);
407+
if (!usesNativeOrVirtualDetector(this.actionType)) {
408+
onGestureHandlerEvent?.(touchEvent);
409+
return;
410+
}
411+
412+
if (this.forReanimated) {
413+
onGestureHandlerReanimatedTouchEvent?.(touchEvent);
414414
} else {
415-
invokeNullableMethod(onGestureHandlerEvent, touchEvent);
415+
onGestureHandlerTouchEvent?.(touchEvent);
416416
}
417417
}
418418

@@ -443,25 +443,26 @@ export default abstract class GestureHandler implements IGestureHandler {
443443
// However, this may cause trouble in the future (but for now we don't know that)
444444
if (this.lastSentState !== newState) {
445445
this.lastSentState = newState;
446-
invokeNullableMethod(
447-
this.forReanimated
448-
? onGestureHandlerReanimatedStateChange
449-
: onGestureHandlerStateChange,
450-
resultEvent
451-
);
446+
447+
if (this.forReanimated) {
448+
onGestureHandlerReanimatedStateChange?.(resultEvent);
449+
} else {
450+
onGestureHandlerStateChange?.(resultEvent);
451+
}
452452
}
453-
if (this.state === State.ACTIVE) {
454-
(resultEvent.nativeEvent as GestureHandlerNativeEvent).oldState =
455-
undefined;
456-
457-
invokeNullableMethod(
458-
this.forReanimated
459-
? onGestureHandlerReanimatedEvent
460-
: this.forAnimated
461-
? onGestureHandlerAnimatedEvent
462-
: onGestureHandlerEvent,
463-
resultEvent
464-
);
453+
454+
if (this.state !== State.ACTIVE) {
455+
return;
456+
}
457+
458+
(resultEvent.nativeEvent as GestureHandlerNativeEvent).oldState = undefined;
459+
460+
if (this.forReanimated) {
461+
onGestureHandlerReanimatedEvent?.(resultEvent);
462+
} else if (this.forAnimated) {
463+
onGestureHandlerAnimatedEvent?.(resultEvent);
464+
} else {
465+
onGestureHandlerEvent?.(resultEvent);
465466
}
466467
};
467468

@@ -672,15 +673,15 @@ export default abstract class GestureHandler implements IGestureHandler {
672673
onGestureHandlerTouchEvent,
673674
}: PropsRef = this.propsRef!.current;
674675

675-
if (this.actionType === ActionType.NATIVE_DETECTOR) {
676-
invokeNullableMethod(
677-
this.forReanimated
678-
? onGestureHandlerReanimatedTouchEvent
679-
: onGestureHandlerTouchEvent,
680-
cancelEvent
681-
);
676+
if (this.actionType !== ActionType.NATIVE_DETECTOR) {
677+
onGestureHandlerEvent?.(cancelEvent);
678+
return;
679+
}
680+
681+
if (this.forReanimated) {
682+
onGestureHandlerReanimatedTouchEvent?.(cancelEvent);
682683
} else {
683-
invokeNullableMethod(onGestureHandlerEvent, cancelEvent);
684+
onGestureHandlerTouchEvent?.(cancelEvent);
684685
}
685686
}
686687

@@ -1082,58 +1083,3 @@ export default abstract class GestureHandler implements IGestureHandler {
10821083
);
10831084
}
10841085
}
1085-
1086-
function invokeNullableMethod(
1087-
method:
1088-
| ((event: ResultEvent) => void)
1089-
| { __getHandler: () => (event: ResultEvent) => void }
1090-
| { __nodeConfig: { argMapping: unknown[] } }
1091-
| null
1092-
| undefined,
1093-
event: ResultEvent
1094-
): void {
1095-
if (!method) {
1096-
return;
1097-
}
1098-
1099-
if (typeof method === 'function') {
1100-
method(event);
1101-
return;
1102-
}
1103-
1104-
if ('__getHandler' in method && typeof method.__getHandler === 'function') {
1105-
const handler = method.__getHandler();
1106-
invokeNullableMethod(handler, event);
1107-
return;
1108-
}
1109-
1110-
if (!('__nodeConfig' in method)) {
1111-
return;
1112-
}
1113-
1114-
const { argMapping }: { argMapping: unknown } = method.__nodeConfig;
1115-
if (!Array.isArray(argMapping)) {
1116-
return;
1117-
}
1118-
1119-
for (const [index, [key, value]] of argMapping.entries()) {
1120-
if (!(key in event.nativeEvent)) {
1121-
continue;
1122-
}
1123-
1124-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
1125-
const nativeValue = (event.nativeEvent as any)[key];
1126-
1127-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
1128-
if (value?.setValue) {
1129-
// Reanimated API
1130-
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
1131-
value.setValue(nativeValue);
1132-
} else {
1133-
// RN Animated API
1134-
method.__nodeConfig.argMapping[index] = [key, nativeValue];
1135-
}
1136-
}
1137-
1138-
return;
1139-
}

0 commit comments

Comments
 (0)