diff --git a/packages/react-native-gesture-handler/src/v3/NativeDetector/HostGestureDetector.web.tsx b/packages/react-native-gesture-handler/src/v3/NativeDetector/HostGestureDetector.web.tsx index bea9959a52..0ab4d714ae 100644 --- a/packages/react-native-gesture-handler/src/v3/NativeDetector/HostGestureDetector.web.tsx +++ b/packages/react-native-gesture-handler/src/v3/NativeDetector/HostGestureDetector.web.tsx @@ -4,7 +4,6 @@ import { ActionType } from '../../ActionType'; import { PropsRef } from '../../web/interfaces'; import { View } from 'react-native'; import { tagMessage } from '../../utils'; -import { EMPTY_SET } from './utils'; export interface GestureHandlerDetectorProps extends PropsRef { handlerTags: number[]; @@ -19,6 +18,8 @@ export interface LogicChildrenWeb { viewRef: RefObject; } +const EMPTY_HANDLERS = new Set(); + const HostGestureDetector = (props: GestureHandlerDetectorProps) => { const { handlerTags, children } = props; @@ -96,9 +97,9 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => { ); return () => { - detachHandlers(EMPTY_SET, attachedHandlers.current); + detachHandlers(EMPTY_HANDLERS, attachedHandlers.current); attachedLogicHandlers?.current.forEach((childHandlerTags) => { - detachHandlers(EMPTY_SET, childHandlerTags); + detachHandlers(EMPTY_HANDLERS, childHandlerTags); }); }; }, [handlerTags, children]); @@ -130,7 +131,7 @@ const HostGestureDetector = (props: GestureHandlerDetectorProps) => { }); logicChildrenToDetach.forEach((tag) => { - detachHandlers(EMPTY_SET, attachedLogicHandlers.current.get(tag)!); + detachHandlers(EMPTY_HANDLERS, attachedLogicHandlers.current.get(tag)!); }); }, [props.logicChildren]); diff --git a/packages/react-native-gesture-handler/src/v3/NativeDetector/utils.ts b/packages/react-native-gesture-handler/src/v3/NativeDetector/utils.ts index d6abd31075..1051c3b809 100644 --- a/packages/react-native-gesture-handler/src/v3/NativeDetector/utils.ts +++ b/packages/react-native-gesture-handler/src/v3/NativeDetector/utils.ts @@ -149,5 +149,3 @@ export function configureRelations( RNGestureHandlerModule.flushOperations(); } - -export const EMPTY_SET = new Set(); diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureStateChangeEvent.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureStateChangeEvent.ts index 3c8fa64286..d0832597f1 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureStateChangeEvent.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureStateChangeEvent.ts @@ -1,5 +1,5 @@ import { BaseGestureConfig } from '../../../types'; -import { extractStateChangeHandlers } from '../../utils/eventHandlersUtils'; +import { extractStateChangeHandlers } from '../../utils'; import { getStateChangeHandler } from '../stateChangeHandler'; export function useGestureStateChangeEvent( diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureTouchEvent.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureTouchEvent.ts index 0271d2ad4d..f7eea78714 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureTouchEvent.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureTouchEvent.ts @@ -1,5 +1,5 @@ import { BaseGestureConfig } from '../../../types'; -import { extractTouchHandlers } from '../../utils/eventHandlersUtils'; +import { extractTouchHandlers } from '../../utils'; import { getTouchEventHandler } from '../touchEventHandler'; export function useGestureTouchEvent( diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureUpdateEvent.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureUpdateEvent.ts index c3122a4224..6179273192 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureUpdateEvent.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/js/useGestureUpdateEvent.ts @@ -1,6 +1,5 @@ -import { isAnimatedEvent } from '../../utils'; +import { extractUpdateHandlers, isAnimatedEvent } from '../../utils'; import { ReanimatedContext } from '../../../../handlers/gestures/reanimatedWrapper'; -import { extractUpdateHandlers } from '../../utils/eventHandlersUtils'; import { getUpdateHandler } from '../updateHandler'; import { BaseGestureConfig } from '../../../types'; diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedStateChangeEvent.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedStateChangeEvent.ts index 99863e9ea9..c5315db996 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedStateChangeEvent.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedStateChangeEvent.ts @@ -1,6 +1,6 @@ import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper'; import { BaseGestureConfig } from '../../../types'; -import { extractStateChangeHandlers } from '../../utils/eventHandlersUtils'; +import { extractStateChangeHandlers } from '../../utils'; import { getStateChangeHandler } from '../stateChangeHandler'; export function useReanimatedStateChangeEvent( diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedTouchEvent.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedTouchEvent.ts index b1c27188f3..d388bbfcf3 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedTouchEvent.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedTouchEvent.ts @@ -1,6 +1,6 @@ import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper'; import { BaseGestureConfig } from '../../../types'; -import { extractTouchHandlers } from '../../utils/eventHandlersUtils'; +import { extractTouchHandlers } from '../../utils'; import { getTouchEventHandler } from '../touchEventHandler'; export function useReanimatedTouchEvent( diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedUpdateEvent.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedUpdateEvent.ts index c86d790c20..7e1fe6b0b7 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedUpdateEvent.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/reanimated/useReanimatedUpdateEvent.ts @@ -1,6 +1,6 @@ import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper'; import { BaseGestureConfig } from '../../../types'; -import { extractUpdateHandlers } from '../../utils/eventHandlersUtils'; +import { extractUpdateHandlers } from '../../utils'; import { getUpdateHandler } from '../updateHandler'; export function useReanimatedUpdateEvent( diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/stateChangeHandler.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/stateChangeHandler.ts index b8852bb505..034aa8eb96 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/stateChangeHandler.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/stateChangeHandler.ts @@ -5,8 +5,11 @@ import { GestureStateChangeEvent, StateChangeEvent, } from '../../types'; -import { isEventForHandlerWithTag, maybeExtractNativeEvent } from '../utils'; -import { runCallback } from '../utils/eventHandlersUtils'; +import { + isEventForHandlerWithTag, + maybeExtractNativeEvent, + runCallback, +} from '../utils'; export function getStateChangeHandler( handlerTag: number, diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/touchEventHandler.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/touchEventHandler.ts index 65f915d269..ef65483218 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/touchEventHandler.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/touchEventHandler.ts @@ -1,11 +1,12 @@ import { GestureCallbacks, TouchEvent } from '../../types'; -import { isEventForHandlerWithTag, maybeExtractNativeEvent } from '../utils'; -import { TouchEventType } from '../../../TouchEventType'; -import { GestureTouchEvent } from '../../../handlers/gestureHandlerCommon'; import { + isEventForHandlerWithTag, + maybeExtractNativeEvent, runCallback, touchEventTypeToCallbackType, -} from '../utils/eventHandlersUtils'; +} from '../utils'; +import { TouchEventType } from '../../../TouchEventType'; +import { GestureTouchEvent } from '../../../handlers/gestureHandlerCommon'; export function getTouchEventHandler( handlerTag: number, diff --git a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/updateHandler.ts b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/updateHandler.ts index 032f666e60..4f258ee251 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/callbacks/updateHandler.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/callbacks/updateHandler.ts @@ -7,8 +7,11 @@ import { GestureUpdateEvent, UpdateEvent, } from '../../types'; -import { isEventForHandlerWithTag, maybeExtractNativeEvent } from '../utils'; -import { runCallback } from '../utils/eventHandlersUtils'; +import { + isEventForHandlerWithTag, + maybeExtractNativeEvent, + runCallback, +} from '../utils'; export function getUpdateHandler( handlerTag: number, diff --git a/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts b/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts index 88369ba7c3..f942b51e28 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/relations/useComposedGesture.ts @@ -8,7 +8,7 @@ import { } from '../../types'; import { tagMessage } from '../../../utils'; import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper'; -import { containsDuplicates, isComposedGesture } from '../utils/relationUtils'; +import { containsDuplicates, isComposedGesture } from '../utils'; // TODO: Simplify repeated relations (Simultaneous with Simultaneous, Exclusive with Exclusive, etc.) export function useComposedGesture( diff --git a/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts b/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts index 916df6d3c0..0d994c7a36 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts @@ -7,15 +7,13 @@ import { prepareConfig, isAnimatedEvent, shouldHandleTouchEvents, -} from './utils'; -import { tagMessage } from '../../utils'; -import { BaseGestureConfig, SingleGesture, SingleGestureName } from '../types'; -import { + prepareRelations, bindSharedValues, hasWorkletEventHandlers, unbindSharedValues, -} from './utils/reanimatedUtils'; -import { prepareRelations } from './utils/relationUtils'; +} from './utils'; +import { tagMessage } from '../../utils'; +import { BaseGestureConfig, SingleGesture, SingleGestureName } from '../types'; export function useGesture( type: SingleGestureName, diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts similarity index 50% rename from packages/react-native-gesture-handler/src/v3/hooks/utils.ts rename to packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts index b2dbbde081..39c2dd5dcc 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/configUtils.ts @@ -1,94 +1,20 @@ -import { NativeSyntheticEvent } from 'react-native'; +import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper'; +import { tagMessage } from '../../../utils'; import { - AnimatedEvent, BaseGestureConfig, - ChangeCalculatorType, CommonGestureConfig, - DiffCalculatorType, ExcludeInternalConfigProps, - UnpackedGestureHandlerEvent, - GestureHandlerEvent, - GestureStateChangeEvent, - GestureUpdateEvent, - SharedValueOrT, - SingleGestureName, - InternalConfigProps, - HandlersPropsWhiteList, GestureCallbacks, -} from '../types'; -import { GestureTouchEvent } from '../../handlers/gestureHandlerCommon'; -import { tagMessage } from '../../utils'; -import { Reanimated } from '../../handlers/gestures/reanimatedWrapper'; -import { PanNativeProperties } from './gestures/pan/PanProperties'; -import { FlingNativeProperties } from './gestures/fling/FlingProperties'; -import { HoverNativeProperties } from './gestures/hover/HoverProperties'; -import { LongPressNativeProperties } from './gestures/longPress/LongPressProperties'; -import { NativeHandlerNativeProperties } from './gestures/native/NativeProperties'; -import { TapNativeProperties } from './gestures/tap/TapProperties'; - -export function isNativeEvent( - event: GestureHandlerEvent -): event is - | NativeSyntheticEvent> - | NativeSyntheticEvent> - | NativeSyntheticEvent { - 'worklet'; - - return 'nativeEvent' in event; -} - -export function maybeExtractNativeEvent( - event: GestureHandlerEvent -) { - 'worklet'; - - return isNativeEvent(event) ? event.nativeEvent : event; -} - -export function isEventForHandlerWithTag( - handlerTag: number, - event: UnpackedGestureHandlerEvent -) { - 'worklet'; - - return event.handlerTag === handlerTag; -} - -export function isAnimatedEvent( - callback: - | ((event: GestureUpdateEvent) => void) - | AnimatedEvent - | undefined -): callback is AnimatedEvent { - 'worklet'; - - return !!callback && '_argMapping' in callback; -} - -export function maybeUnpackValue(v: SharedValueOrT) { - 'worklet'; - - return (Reanimated?.isSharedValue(v) ? v.value : v) as T; -} - -export function checkMappingForChangeProperties(animatedEvent: AnimatedEvent) { - for (const mapping of animatedEvent._argMapping) { - if ( - !mapping || - !('nativeEvent' in mapping && 'handlerData' in mapping.nativeEvent) - ) { - continue; - } - - for (const key in mapping.nativeEvent.handlerData) { - if (key.startsWith('change')) { - throw new Error( - tagMessage(`${key} is not available when using Animated.Event.`) - ); - } - } - } -} + HandlersPropsWhiteList, + InternalConfigProps, + SingleGestureName, +} from '../../types'; +import { PanNativeProperties } from '../gestures/pan/PanProperties'; +import { FlingNativeProperties } from '../gestures/fling/FlingProperties'; +import { HoverNativeProperties } from '../gestures/hover/HoverProperties'; +import { LongPressNativeProperties } from '../gestures/longPress/LongPressProperties'; +import { NativeHandlerNativeProperties } from '../gestures/native/NativeProperties'; +import { TapNativeProperties } from '../gestures/tap/TapProperties'; const allowedNativeProps = new Set< keyof CommonGestureConfig | keyof InternalConfigProps @@ -181,17 +107,6 @@ export function prepareConfig( return filteredConfig; } -export function shouldHandleTouchEvents( - config: BaseGestureConfig -) { - return ( - !!config.onTouchesDown || - !!config.onTouchesMove || - !!config.onTouchesUp || - !!config.onTouchesCancelled - ); -} - export function cloneConfig( config: ExcludeInternalConfigProps> ): BaseGestureConfig { @@ -216,23 +131,3 @@ export function remapProps( return config; } - -export function getChangeEventCalculator( - diffCalculator: DiffCalculatorType -): ChangeCalculatorType { - 'worklet'; - return ( - current: GestureUpdateEvent, - previous?: GestureUpdateEvent - ) => { - 'worklet'; - const currentEventData = current.handlerData; - const previousEventData = previous ? previous.handlerData : null; - - const changePayload = diffCalculator(currentEventData, previousEventData); - - current.handlerData = { ...currentEventData, ...changePayload }; - - return current; - }; -} diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/eventUtils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/eventUtils.ts new file mode 100644 index 0000000000..1e061d9b6a --- /dev/null +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/eventUtils.ts @@ -0,0 +1,102 @@ +import { NativeSyntheticEvent } from 'react-native'; +import { + AnimatedEvent, + BaseGestureConfig, + ChangeCalculatorType, + DiffCalculatorType, + UnpackedGestureHandlerEvent, + GestureHandlerEvent, + GestureStateChangeEvent, + GestureUpdateEvent, +} from '../../types'; +import { GestureTouchEvent } from '../../../handlers/gestureHandlerCommon'; +import { tagMessage } from '../../../utils'; + +export function isNativeEvent( + event: GestureHandlerEvent +): event is + | NativeSyntheticEvent> + | NativeSyntheticEvent> + | NativeSyntheticEvent { + 'worklet'; + + return 'nativeEvent' in event; +} + +export function maybeExtractNativeEvent( + event: GestureHandlerEvent +) { + 'worklet'; + + return isNativeEvent(event) ? event.nativeEvent : event; +} + +export function isEventForHandlerWithTag( + handlerTag: number, + event: UnpackedGestureHandlerEvent +) { + 'worklet'; + + return event.handlerTag === handlerTag; +} + +export function isAnimatedEvent( + callback: + | ((event: GestureUpdateEvent) => void) + | AnimatedEvent + | undefined +): callback is AnimatedEvent { + 'worklet'; + + return !!callback && '_argMapping' in callback; +} + +export function checkMappingForChangeProperties(animatedEvent: AnimatedEvent) { + for (const mapping of animatedEvent._argMapping) { + if ( + !mapping || + !('nativeEvent' in mapping && 'handlerData' in mapping.nativeEvent) + ) { + continue; + } + + for (const key in mapping.nativeEvent.handlerData) { + if (key.startsWith('change')) { + throw new Error( + tagMessage(`${key} is not available when using Animated.Event.`) + ); + } + } + } +} + +export function shouldHandleTouchEvents( + config: BaseGestureConfig +) { + return ( + !!config.onTouchesDown || + !!config.onTouchesMove || + !!config.onTouchesUp || + !!config.onTouchesCancelled + ); +} + +export function getChangeEventCalculator( + diffCalculator: DiffCalculatorType +): ChangeCalculatorType { + 'worklet'; + return ( + current: GestureUpdateEvent, + previous?: GestureUpdateEvent + ) => { + 'worklet'; + const currentEventData = current.handlerData; + const previousEventData = previous ? previous.handlerData : null; + + const changePayload = diffCalculator(currentEventData, previousEventData); + + current.handlerData = { ...currentEventData, ...changePayload }; + + return current; + }; +} diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/index.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/index.ts new file mode 100644 index 0000000000..753d4398e2 --- /dev/null +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/index.ts @@ -0,0 +1,5 @@ +export * from './configUtils'; +export * from './eventHandlersUtils'; +export * from './eventUtils'; +export * from './reanimatedUtils'; +export * from './relationUtils'; diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts index e80a8092a0..03e9b4dc75 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/reanimatedUtils.ts @@ -1,7 +1,7 @@ import RNGestureHandlerModule from '../../../RNGestureHandlerModule'; import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper'; -import { BaseGestureConfig, SharedValue } from '../../types'; -import { HandlerCallbacks } from '../utils'; +import { BaseGestureConfig, SharedValue, SharedValueOrT } from '../../types'; +import { HandlerCallbacks } from './configUtils'; // Variant of djb2 hash function. // Taken from https://gist.github.com/eplawless/52813b1d8ad9af510d85?permalink_comment_id=3367765#gistcomment-3367765 @@ -86,3 +86,9 @@ export function hasWorkletEventHandlers( '__workletHash' in value ); } + +export function maybeUnpackValue(v: SharedValueOrT) { + 'worklet'; + + return (Reanimated?.isSharedValue(v) ? v.value : v) as T; +}