diff --git a/packages/react-native-gesture-handler/src/__tests__/RelationsTraversal.test.tsx b/packages/react-native-gesture-handler/src/__tests__/RelationsTraversal.test.tsx index 5476c6462a..fb8128dbdd 100644 --- a/packages/react-native-gesture-handler/src/__tests__/RelationsTraversal.test.tsx +++ b/packages/react-native-gesture-handler/src/__tests__/RelationsTraversal.test.tsx @@ -6,7 +6,9 @@ import { SingleGesture, SingleGestureName } from '../v3/types'; import { renderHook } from '@testing-library/react-native'; describe('Ensure only one leaf node', () => { - let pan1: SingleGesture, pan2: SingleGesture, pan3: SingleGesture; + let pan1: SingleGesture, + pan2: SingleGesture, + pan3: SingleGesture; beforeEach(() => { pan1 = renderHook(() => @@ -47,7 +49,8 @@ describe('Ensure only one leaf node', () => { }); describe('Simple relations', () => { - let pan1: SingleGesture, pan2: SingleGesture; + let pan1: SingleGesture, + pan2: SingleGesture; beforeEach(() => { pan1 = renderHook(() => @@ -188,8 +191,12 @@ describe('External relations', () => { }); describe('Complex relations', () => { - let pan1: SingleGesture, pan2: SingleGesture, pan3: SingleGesture; - let tap1: SingleGesture, tap2: SingleGesture, tap3: SingleGesture; + let pan1: SingleGesture, + pan2: SingleGesture, + pan3: SingleGesture; + let tap1: SingleGesture, + tap2: SingleGesture, + tap3: SingleGesture; beforeEach(() => { tap1 = renderHook(() => diff --git a/packages/react-native-gesture-handler/src/handlers/gestures/reanimatedWrapper.ts b/packages/react-native-gesture-handler/src/handlers/gestures/reanimatedWrapper.ts index 9875a7f710..1d73f5b4de 100644 --- a/packages/react-native-gesture-handler/src/handlers/gestures/reanimatedWrapper.ts +++ b/packages/react-native-gesture-handler/src/handlers/gestures/reanimatedWrapper.ts @@ -1,6 +1,6 @@ import { ComponentClass } from 'react'; import { tagMessage } from '../../utils'; -import { UpdateEvent } from '../../v3/types'; +import { GestureCallbacks, UpdateEvent } from '../../v3/types'; export interface SharedValue { value: Value; @@ -14,8 +14,8 @@ export interface SharedValue { ) => void; } -export type ReanimatedContext = { - lastUpdateEvent: UpdateEvent> | undefined; +export type ReanimatedContext = { + lastUpdateEvent: UpdateEvent | undefined; }; interface WorkletProps { @@ -41,9 +41,9 @@ let Reanimated: options?: unknown ): ComponentClass

; }; - useHandler: (handlers: Record) => { + useHandler: (handlers: GestureCallbacks) => { doDependenciesDiffer: boolean; - context: ReanimatedContext; + context: ReanimatedContext; }; useEvent: ( callback: (event: T) => void, diff --git a/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx b/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx index a4ec84720f..a55c2fa040 100644 --- a/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx +++ b/packages/react-native-gesture-handler/src/v3/NativeDetector/NativeDetector.tsx @@ -21,8 +21,7 @@ const ReanimatedNativeDetector = export function NativeDetector({ gesture, children }: NativeDetectorProps) { const NativeDetectorComponent = gesture.config.dispatchesAnimatedEvents ? AnimatedNativeDetector - : // TODO: Remove this cast when we properly type config - (gesture.config.shouldUseReanimated as boolean) + : gesture.config.shouldUseReanimated ? ReanimatedNativeDetector : HostGestureDetector; 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 699bb29c46..3c8fa64286 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,7 +1,11 @@ +import { BaseGestureConfig } from '../../../types'; import { extractStateChangeHandlers } from '../../utils/eventHandlersUtils'; import { getStateChangeHandler } from '../stateChangeHandler'; -export function useGestureStateChangeEvent(handlerTag: number, config: any) { +export function useGestureStateChangeEvent( + handlerTag: number, + config: BaseGestureConfig +) { const handlers = extractStateChangeHandlers(config); return getStateChangeHandler(handlerTag, handlers); 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 f98068ab68..0271d2ad4d 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,7 +1,11 @@ +import { BaseGestureConfig } from '../../../types'; import { extractTouchHandlers } from '../../utils/eventHandlersUtils'; import { getTouchEventHandler } from '../touchEventHandler'; -export function useGestureTouchEvent(handlerTag: number, config: any) { +export function useGestureTouchEvent( + handlerTag: number, + config: BaseGestureConfig +) { const handlers = extractTouchHandlers(config); return getTouchEventHandler(handlerTag, handlers); 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 5cc938339f..c3122a4224 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 @@ -2,11 +2,15 @@ import { isAnimatedEvent } from '../../utils'; import { ReanimatedContext } from '../../../../handlers/gestures/reanimatedWrapper'; import { extractUpdateHandlers } from '../../utils/eventHandlersUtils'; import { getUpdateHandler } from '../updateHandler'; +import { BaseGestureConfig } from '../../../types'; -export function useGestureUpdateEvent(handlerTag: number, config: any) { +export function useGestureUpdateEvent( + handlerTag: number, + config: BaseGestureConfig +) { const { handlers, changeEventCalculator } = extractUpdateHandlers(config); - const jsContext: ReanimatedContext = { + const jsContext: ReanimatedContext = { lastUpdateEvent: undefined, }; 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 86253ae056..99863e9ea9 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,8 +1,12 @@ import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper'; +import { BaseGestureConfig } from '../../../types'; import { extractStateChangeHandlers } from '../../utils/eventHandlersUtils'; import { getStateChangeHandler } from '../stateChangeHandler'; -export function useReanimatedStateChangeEvent(handlerTag: number, config: any) { +export function useReanimatedStateChangeEvent( + handlerTag: number, + config: BaseGestureConfig +) { const handlers = extractStateChangeHandlers(config); const callback = getStateChangeHandler(handlerTag, handlers); 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 81e0e3f204..b1c27188f3 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,8 +1,12 @@ import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper'; +import { BaseGestureConfig } from '../../../types'; import { extractTouchHandlers } from '../../utils/eventHandlersUtils'; import { getTouchEventHandler } from '../touchEventHandler'; -export function useReanimatedTouchEvent(handlerTag: number, config: any) { +export function useReanimatedTouchEvent( + handlerTag: number, + config: BaseGestureConfig +) { const handlers = extractTouchHandlers(config); const callback = getTouchEventHandler(handlerTag, handlers); 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 7f4f0083d1..c86d790c20 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,8 +1,12 @@ import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper'; +import { BaseGestureConfig } from '../../../types'; import { extractUpdateHandlers } from '../../utils/eventHandlersUtils'; import { getUpdateHandler } from '../updateHandler'; -export function useReanimatedUpdateEvent(handlerTag: number, config: any) { +export function useReanimatedUpdateEvent( + handlerTag: number, + config: BaseGestureConfig +) { const { handlers, changeEventCalculator } = extractUpdateHandlers(config); // We don't want to call hooks conditionally, therefore `useHandler` and `useEvent` will be always called. 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 ad6a0ef511..afd268e45b 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 @@ -1,14 +1,14 @@ import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture'; import { State } from '../../../State'; -import { CallbackHandlers, StateChangeEvent } from '../../types'; +import { GestureCallbacks, StateChangeEvent } from '../../types'; import { isEventForHandlerWithTag, isNativeEvent } from '../utils'; import { runCallback } from '../utils/eventHandlersUtils'; -export function getStateChangeHandler( +export function getStateChangeHandler( handlerTag: number, - callbacks: CallbackHandlers + callbacks: GestureCallbacks ) { - return (event: StateChangeEvent>) => { + return (event: StateChangeEvent) => { 'worklet'; if (!isEventForHandlerWithTag(handlerTag, event)) { 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 7f58c888ef..6b39e82fb8 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,5 +1,5 @@ import { NativeSyntheticEvent } from 'react-native'; -import { CallbackHandlers, TouchEvent } from '../../types'; +import { GestureCallbacks, TouchEvent } from '../../types'; import { isEventForHandlerWithTag, isNativeEvent } from '../utils'; import { TouchEventType } from '../../../TouchEventType'; import { GestureTouchEvent } from '../../../handlers/gestureHandlerCommon'; @@ -8,9 +8,9 @@ import { touchEventTypeToCallbackType, } from '../utils/eventHandlersUtils'; -export function getTouchEventHandler( +export function getTouchEventHandler( handlerTag: number, - callbacks: CallbackHandlers + callbacks: GestureCallbacks ) { return (event: TouchEvent) => { 'worklet'; 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 e18dd31c8c..8f37633857 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 @@ -1,20 +1,21 @@ import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture'; import { tagMessage } from '../../../utils'; import { ReanimatedContext } from '../../../handlers/gestures/reanimatedWrapper'; -import { CallbackHandlers, UpdateEvent } from '../../types'; +import { + ChangeCalculatorType, + GestureCallbacks, + UpdateEvent, +} from '../../types'; import { isEventForHandlerWithTag } from '../utils'; import { runCallback } from '../utils/eventHandlersUtils'; -export function getUpdateHandler( +export function getUpdateHandler( handlerTag: number, - callbacks: CallbackHandlers, - context: ReanimatedContext | undefined, - changeEventCalculator?: ( - current: UpdateEvent>, - previous?: UpdateEvent> - ) => UpdateEvent> + callbacks: GestureCallbacks, + context: ReanimatedContext | undefined, + changeEventCalculator?: ChangeCalculatorType ) { - return (event: UpdateEvent>) => { + return (event: UpdateEvent) => { 'worklet'; if (!isEventForHandlerWithTag(handlerTag, event)) { 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 689e965829..bdb8d11f2c 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 @@ -44,9 +44,7 @@ export function useComposedGesture( ); } - const onGestureHandlerStateChange = ( - event: StateChangeEvent> - ) => { + const onGestureHandlerStateChange = (event: StateChangeEvent) => { for (const gesture of gestures) { if (gesture.gestureEvents.onGestureHandlerStateChange) { gesture.gestureEvents.onGestureHandlerStateChange(event); @@ -54,9 +52,7 @@ export function useComposedGesture( } }; - const onGestureHandlerEvent = ( - event: UpdateEvent> - ) => { + const onGestureHandlerEvent = (event: UpdateEvent) => { for (const gesture of gestures) { if (gesture.gestureEvents.onGestureHandlerEvent) { gesture.gestureEvents.onGestureHandlerEvent(event); 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 2b6b99e303..1f58af928d 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts @@ -9,7 +9,7 @@ import { shouldHandleTouchEvents, } from './utils'; import { tagMessage } from '../../utils'; -import { SingleGesture, SingleGestureName } from '../types'; +import { BaseGestureConfig, SingleGesture, SingleGestureName } from '../types'; import { bindSharedValues, hasWorkletEventHandlers, @@ -17,10 +17,10 @@ import { } from './utils/reanimatedUtils'; import { prepareRelations } from './utils/relationUtils'; -export function useGesture( +export function useGesture( type: SingleGestureName, - config: Record -): SingleGesture { + config: BaseGestureConfig +): SingleGesture { const tag = useMemo(() => getNextHandlerTag(), []); const disableReanimated = useMemo(() => config.disableReanimated, []); @@ -38,8 +38,7 @@ export function useGesture( Reanimated !== undefined && hasWorkletEventHandlers(config); config.needsPointerData = shouldHandleTouchEvents(config); - // TODO: Remove this when we properly type config - config.dispatchesAnimatedEvents = isAnimatedEvent(config.onUpdate as any); + config.dispatchesAnimatedEvents = isAnimatedEvent(config.onUpdate); if (config.dispatchesAnimatedEvents && config.shouldUseReanimated) { throw new Error( diff --git a/packages/react-native-gesture-handler/src/v3/hooks/useGestureCallbacks.ts b/packages/react-native-gesture-handler/src/v3/hooks/useGestureCallbacks.ts index 37c3b641e1..b4ccedfd9a 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/useGestureCallbacks.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/useGestureCallbacks.ts @@ -1,13 +1,16 @@ import { useGestureStateChangeEvent } from './callbacks/js/useGestureStateChangeEvent'; import { useGestureUpdateEvent } from './callbacks/js/useGestureUpdateEvent'; import { useGestureTouchEvent } from './callbacks/js/useGestureTouchEvent'; -import { AnimatedEvent } from '../types'; +import { AnimatedEvent, BaseGestureConfig } from '../types'; import { checkMappingForChangeProperties, isAnimatedEvent } from './utils'; import { useReanimatedStateChangeEvent } from './callbacks/reanimated/useReanimatedStateChangeEvent'; import { useReanimatedUpdateEvent } from './callbacks/reanimated/useReanimatedUpdateEvent'; import { useReanimatedTouchEvent } from './callbacks/reanimated/useReanimatedTouchEvent'; -export function useGestureCallbacks(handlerTag: number, config: any) { +export function useGestureCallbacks( + handlerTag: number, + config: BaseGestureConfig +) { const onGestureHandlerStateChange = useGestureStateChangeEvent( handlerTag, config @@ -31,8 +34,7 @@ export function useGestureCallbacks(handlerTag: number, config: any) { let onGestureHandlerAnimatedEvent: AnimatedEvent | undefined; if (isAnimatedEvent(config.onUpdate)) { checkMappingForChangeProperties(config.onUpdate); - // TODO: Remove cast when config is properly typed. - onGestureHandlerAnimatedEvent = config.onUpdate as AnimatedEvent; + onGestureHandlerAnimatedEvent = config.onUpdate; } return { diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils.ts index 57c5bee592..d90fc773d1 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils.ts @@ -1,28 +1,29 @@ import { NativeSyntheticEvent } from 'react-native'; import { AnimatedEvent, + BaseGestureConfig, GestureHandlerEvent, - GestureStateChangeEventWithData, - GestureUpdateEventWithData, + GestureStateChangeEvent, + GestureUpdateEvent, } from '../types'; import { GestureTouchEvent } from '../../handlers/gestureHandlerCommon'; import { tagMessage } from '../../utils'; import { Reanimated } from '../../handlers/gestures/reanimatedWrapper'; -export function isNativeEvent( - event: GestureHandlerEvent +export function isNativeEvent( + event: GestureHandlerEvent ): event is - | NativeSyntheticEvent> - | NativeSyntheticEvent> + | NativeSyntheticEvent> + | NativeSyntheticEvent> | NativeSyntheticEvent { 'worklet'; return 'nativeEvent' in event; } -export function isEventForHandlerWithTag( +export function isEventForHandlerWithTag( handlerTag: number, - event: GestureHandlerEvent> + event: GestureHandlerEvent ) { 'worklet'; @@ -31,8 +32,11 @@ export function isEventForHandlerWithTag( : event.handlerTag === handlerTag; } -export function isAnimatedEvent( - callback: ((event: any) => void) | AnimatedEvent | undefined +export function isAnimatedEvent( + callback: + | ((event: GestureUpdateEvent) => void) + | AnimatedEvent + | undefined ): callback is AnimatedEvent { 'worklet'; @@ -58,26 +62,32 @@ export function checkMappingForChangeProperties(animatedEvent: AnimatedEvent) { } } -export function prepareConfig(config: any) { +export function prepareConfig( + config: BaseGestureConfig +) { const copy = { ...config }; for (const key in copy) { + // @ts-ignore It is fine to use string as index if (Reanimated?.isSharedValue(copy[key])) { + // @ts-ignore It is fine to use string as index copy[key] = copy[key].value; } } // TODO: Filter changes - passing functions (and possibly other types) // causes a native crash - copy.onUpdate = null; - copy.simultaneousWithExternalGesture = null; - copy.requireExternalGestureToFail = null; - copy.blocksExternalGesture = null; + delete copy.onUpdate; + delete copy.simultaneousWithExternalGesture; + delete copy.requireExternalGestureToFail; + delete copy.blocksExternalGesture; return copy; } -export function shouldHandleTouchEvents(config: Record) { +export function shouldHandleTouchEvents( + config: BaseGestureConfig +) { return ( !!config.onTouchesDown || !!config.onTouchesMove || diff --git a/packages/react-native-gesture-handler/src/v3/hooks/utils/eventHandlersUtils.ts b/packages/react-native-gesture-handler/src/v3/hooks/utils/eventHandlersUtils.ts index 963e5c5ecd..2503541d79 100644 --- a/packages/react-native-gesture-handler/src/v3/hooks/utils/eventHandlersUtils.ts +++ b/packages/react-native-gesture-handler/src/v3/hooks/utils/eventHandlersUtils.ts @@ -1,17 +1,20 @@ import { TouchEventType } from '../../../TouchEventType'; import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture'; import { - CallbackHandlers, + BaseGestureConfig, + ChangeCalculatorType, + GestureCallbacks, GestureHandlerEvent, - UpdateEvent, } from '../../types'; import { isNativeEvent } from '../utils'; -export function extractStateChangeHandlers(config: any): CallbackHandlers { +export function extractStateChangeHandlers( + config: BaseGestureConfig +): GestureCallbacks { 'worklet'; const { onBegin, onStart, onEnd, onFinalize } = config; - const handlers: CallbackHandlers = { + const handlers: GestureCallbacks = { ...(onBegin ? { onBegin } : {}), ...(onStart ? { onStart } : {}), ...(onEnd ? { onEnd } : {}), @@ -21,26 +24,31 @@ export function extractStateChangeHandlers(config: any): CallbackHandlers { return handlers; } -export function extractUpdateHandlers(config: any): { - handlers: CallbackHandlers; - changeEventCalculator?: ( - current: UpdateEvent>, - previous?: UpdateEvent> - ) => UpdateEvent>; -} { +type UpdateHandlersReturnType = { + handlers: GestureCallbacks; + changeEventCalculator?: ChangeCalculatorType; +}; + +export function extractUpdateHandlers( + config: BaseGestureConfig +): UpdateHandlersReturnType { 'worklet'; const { onUpdate, changeEventCalculator } = config; - const handlers: CallbackHandlers = { ...(onUpdate ? { onUpdate } : {}) }; + const handlers: GestureCallbacks = { + ...(onUpdate ? { onUpdate } : {}), + }; return { handlers, changeEventCalculator }; } -export function extractTouchHandlers(config: any): CallbackHandlers { +export function extractTouchHandlers( + config: BaseGestureConfig +): GestureCallbacks { const { onTouchesDown, onTouchesMove, onTouchesUp, onTouchesCancelled } = config; - const handlers: CallbackHandlers = { + const handlers: GestureCallbacks = { ...(onTouchesDown ? { onTouchesDown } : {}), ...(onTouchesMove ? { onTouchesMove } : {}), ...(onTouchesUp ? { onTouchesUp } : {}), @@ -50,27 +58,30 @@ export function extractTouchHandlers(config: any): CallbackHandlers { return handlers; } -export function getHandler(type: CALLBACK_TYPE, config: CallbackHandlers) { +export function getHandler( + type: CALLBACK_TYPE, + callbacks: GestureCallbacks +) { 'worklet'; switch (type) { case CALLBACK_TYPE.BEGAN: - return config.onBegin; + return callbacks.onBegin; case CALLBACK_TYPE.START: - return config.onStart; + return callbacks.onStart; case CALLBACK_TYPE.UPDATE: - return config.onUpdate; + return callbacks.onUpdate; case CALLBACK_TYPE.END: - return config.onEnd; + return callbacks.onEnd; case CALLBACK_TYPE.FINALIZE: - return config.onFinalize; + return callbacks.onFinalize; case CALLBACK_TYPE.TOUCHES_DOWN: - return config.onTouchesDown; + return callbacks.onTouchesDown; case CALLBACK_TYPE.TOUCHES_MOVE: - return config.onTouchesMove; + return callbacks.onTouchesMove; case CALLBACK_TYPE.TOUCHES_UP: - return config.onTouchesUp; + return callbacks.onTouchesUp; case CALLBACK_TYPE.TOUCHES_CANCELLED: - return config.onTouchesCancelled; + return callbacks.onTouchesCancelled; } } @@ -91,14 +102,14 @@ export function touchEventTypeToCallbackType( return CALLBACK_TYPE.UNDEFINED; } -export function runCallback( +export function runCallback( type: CALLBACK_TYPE, - config: CallbackHandlers, - event: GestureHandlerEvent>, + callbacks: GestureCallbacks, + event: GestureHandlerEvent, ...args: unknown[] ) { 'worklet'; - const handler = getHandler(type, config); + const handler = getHandler(type, callbacks); // TODO: add proper types (likely boolean) // @ts-ignore It works, duh 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 c0cf19cc81..dd3c3b9a1e 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 @@ -3,6 +3,7 @@ import { Reanimated, SharedValue, } from '../../../handlers/gestures/reanimatedWrapper'; +import { BaseGestureConfig } from '../../types'; // Variant of djb2 hash function. // Taken from https://gist.github.com/eplawless/52813b1d8ad9af510d85?permalink_comment_id=3367765#gistcomment-3367765 @@ -22,7 +23,10 @@ const SHARED_VALUE_OFFSET = 1.618; // This is used to obtain HostFunction that can be executed on the UI thread const { updateGestureHandlerConfig, flushOperations } = RNGestureHandlerModule; -export function bindSharedValues(config: any, handlerTag: number) { +export function bindSharedValues( + config: BaseGestureConfig, + handlerTag: number +) { if (Reanimated === undefined) { return; } @@ -49,7 +53,10 @@ export function bindSharedValues(config: any, handlerTag: number) { } } -export function unbindSharedValues(config: any, handlerTag: number) { +export function unbindSharedValues( + config: BaseGestureConfig, + handlerTag: number +) { if (Reanimated === undefined) { return; } @@ -70,7 +77,9 @@ export function unbindSharedValues(config: any, handlerTag: number) { } } -export function hasWorkletEventHandlers(config: Record) { +export function hasWorkletEventHandlers( + config: BaseGestureConfig +) { return Object.values(config).some( (prop) => typeof prop === 'function' && '__workletHash' in prop ); diff --git a/packages/react-native-gesture-handler/src/v3/types.ts b/packages/react-native-gesture-handler/src/v3/types.ts index 0a3f157fa4..8c2bc7451f 100644 --- a/packages/react-native-gesture-handler/src/v3/types.ts +++ b/packages/react-native-gesture-handler/src/v3/types.ts @@ -4,44 +4,33 @@ import { GestureTouchEvent, HandlerStateChangeEventPayload, } from '../handlers/gestureHandlerCommon'; -import { HandlerCallbacks } from '../handlers/gestures/gesture'; -export type GestureUpdateEventWithData = GestureEventPayload & { - handlerData: T; +export type GestureUpdateEvent = GestureEventPayload & { + handlerData: THandlerData; }; -export type GestureStateChangeEventWithData = +export type GestureStateChangeEvent = HandlerStateChangeEventPayload & { - handlerData: T; + handlerData: THandlerData; }; -export type GestureHandlerEvent = - | UpdateEvent - | StateChangeEvent +export type GestureHandlerEvent = + | UpdateEvent + | StateChangeEvent | TouchEvent; -export type UpdateEvent = - | GestureUpdateEventWithData - | NativeSyntheticEvent>; +export type UpdateEvent = + | GestureUpdateEvent + | NativeSyntheticEvent>; -export type StateChangeEvent = - | GestureStateChangeEventWithData - | NativeSyntheticEvent>; +export type StateChangeEvent = + | GestureStateChangeEvent + | NativeSyntheticEvent>; export type TouchEvent = | GestureTouchEvent | NativeSyntheticEvent; -// TODO: Replace with v3 specific types -export type CallbackHandlers = Omit< - HandlerCallbacks>, - | 'gestureId' - | 'handlerTag' - | 'isWorklet' - | 'changeEventCalculator' - | 'onChange' ->; - // This is almost how Animated.event is typed in React Native. We add _argMapping in order to: // 1. Distinguish it from a regular function, // 2. Have access to the _argMapping property to check for usage of `change*` callbacks. @@ -66,20 +55,18 @@ export enum ComposedGestureName { Race = 'RaceGesture', } -export type GestureEvents = { - onGestureHandlerStateChange: ( - event: StateChangeEvent> - ) => void; +export type GestureEvents = { + onGestureHandlerStateChange: (event: StateChangeEvent) => void; onGestureHandlerEvent: | undefined - | ((event: UpdateEvent>) => void); + | ((event: UpdateEvent) => void); onGestureHandlerTouchEvent: (event: TouchEvent) => void; onReanimatedStateChange: | undefined - | ((event: StateChangeEvent>) => void); + | ((event: StateChangeEvent) => void); onReanimatedUpdateEvent: | undefined - | ((event: UpdateEvent>) => void); + | ((event: UpdateEvent) => void); onReanimatedTouchEvent: undefined | ((event: TouchEvent) => void); onGestureHandlerAnimatedEvent: undefined | AnimatedEvent; }; @@ -90,11 +77,11 @@ export type GestureRelations = { blocksHandlers: number[]; }; -export type SingleGesture = { +export type SingleGesture = { tag: number; type: SingleGestureName; - config: Record; - gestureEvents: GestureEvents; + config: BaseGestureConfig; + gestureEvents: GestureEvents; gestureRelations: GestureRelations; }; @@ -105,9 +92,50 @@ export type ComposedGesture = { shouldUseReanimated: boolean; dispatchesAnimatedEvents: boolean; }; - gestureEvents: GestureEvents; + gestureEvents: GestureEvents; externalSimultaneousHandlers: number[]; gestures: Gesture[]; }; -export type Gesture = SingleGesture | ComposedGesture; +export type ChangeCalculatorType = ( + current: UpdateEvent, + previous?: UpdateEvent +) => UpdateEvent; + +export type Gesture = + | SingleGesture + | ComposedGesture; + +interface ExternalRelations { + simultaneousWithExternalGesture?: Gesture | Gesture[]; + requireExternalGestureToFail?: Gesture | Gesture[]; + blocksExternalGesture?: Gesture | Gesture[]; +} + +export interface GestureCallbacks { + onBegin?: (event: GestureStateChangeEvent) => void; + onStart?: (event: GestureStateChangeEvent) => void; + onEnd?: ( + event: GestureStateChangeEvent, + success: boolean + ) => void; + onFinalize?: ( + event: GestureStateChangeEvent, + success: boolean + ) => void; + onUpdate?: (event: GestureUpdateEvent) => void | AnimatedEvent; + onTouchesDown?: (event: GestureTouchEvent) => void; + onTouchesMove?: (event: GestureTouchEvent) => void; + onTouchesUp?: (event: GestureTouchEvent) => void; + onTouchesCancelled?: (event: GestureTouchEvent) => void; +} + +export type BaseGestureConfig = ExternalRelations & + GestureCallbacks & + TConfig & { + disableReanimated?: boolean; + shouldUseReanimated?: boolean; + dispatchesAnimatedEvents?: boolean; + needsPointerData?: boolean; + changeEventCalculator?: ChangeCalculatorType; + }; diff --git a/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts b/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts index 71df67ac4c..f1a17360db 100644 --- a/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts +++ b/packages/react-native-gesture-handler/src/web/handlers/GestureHandler.ts @@ -26,10 +26,7 @@ import { PointerType } from '../../PointerType'; import { GestureHandlerDelegate } from '../tools/GestureHandlerDelegate'; import { ActionType } from '../../ActionType'; import { tagMessage } from '../../utils'; -import { - GestureStateChangeEventWithData, - GestureUpdateEventWithData, -} from '../../v3/types'; +import { GestureStateChangeEvent, GestureUpdateEvent } from '../../v3/types'; import { TouchEventType } from '../../TouchEventType'; export default abstract class GestureHandler implements IGestureHandler { @@ -464,7 +461,7 @@ export default abstract class GestureHandler implements IGestureHandler { private transformStateChangeEvent( newState: State, oldState: State - ): ResultEvent> { + ): ResultEvent> { this.ensureViewRef(this.viewRef); return { nativeEvent: { @@ -487,7 +484,7 @@ export default abstract class GestureHandler implements IGestureHandler { private transformUpdateEvent( newState: State - ): ResultEvent> { + ): ResultEvent> { this.ensureViewRef(this.viewRef); return { nativeEvent: { diff --git a/packages/react-native-gesture-handler/src/web/interfaces.ts b/packages/react-native-gesture-handler/src/web/interfaces.ts index 49d0d81c76..614bb46b64 100644 --- a/packages/react-native-gesture-handler/src/web/interfaces.ts +++ b/packages/react-native-gesture-handler/src/web/interfaces.ts @@ -8,10 +8,7 @@ import { } from '../handlers/gestureHandlerCommon'; import { Directions } from '../Directions'; import { PointerType } from '../PointerType'; -import { - GestureStateChangeEventWithData, - GestureUpdateEventWithData, -} from '../v3/types'; +import { GestureStateChangeEvent, GestureUpdateEvent } from '../v3/types'; import { State } from '../State'; export interface HitSlop { @@ -116,8 +113,8 @@ export interface PointerData { // Native event has to stay for v2 compatibility type ResultEventType = - | GestureUpdateEventWithData - | GestureStateChangeEventWithData + | GestureUpdateEvent + | GestureStateChangeEvent | GestureTouchEvent | GestureHandlerNativeEvent;