Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { useMemo } from 'react';
import { TouchEventType } from '../../../TouchEventType';
import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture';
import { GestureCallbacks, UnpackedGestureHandlerEvent } from '../../types';
import {
GestureCallbacks,
GestureEventCallback,
GestureEventCallbackWithDidSucceed,
GestureTouchEventCallback,
UnpackedGestureHandlerEvent,
} from '../../types';

export function useMemoizedGestureCallbacks<THandlerData>(
callbacks: GestureCallbacks<THandlerData>
Expand Down Expand Up @@ -43,15 +49,19 @@ export function useMemoizedGestureCallbacks<THandlerData>(
function getHandler<THandlerData>(
type: CALLBACK_TYPE,
callbacks: GestureCallbacks<THandlerData>
) {
):
| GestureEventCallback<THandlerData>
| GestureEventCallbackWithDidSucceed<THandlerData>
| GestureTouchEventCallback
| undefined {
'worklet';
switch (type) {
case CALLBACK_TYPE.BEGAN:
return callbacks.onBegin;
case CALLBACK_TYPE.START:
return callbacks.onActivate;
case CALLBACK_TYPE.UPDATE:
return callbacks.onUpdate;
return callbacks.onUpdate as GestureEventCallback<THandlerData>; // Animated event is handled in different place.
case CALLBACK_TYPE.END:
return callbacks.onDeactivate;
case CALLBACK_TYPE.FINALIZE:
Expand Down Expand Up @@ -84,16 +94,25 @@ export function touchEventTypeToCallbackType(
return CALLBACK_TYPE.UNDEFINED;
}

type SingleParameterCallback<T> = (event: T) => void;
type DoubleParameterCallback<T> = (event: T, didSucceed: boolean) => void;

export function runCallback<THandlerData>(
type: CALLBACK_TYPE,
callbacks: GestureCallbacks<THandlerData>,
event: UnpackedGestureHandlerEvent<THandlerData>,
...args: unknown[]
didSucceed?: boolean
) {
'worklet';
const handler = getHandler(type, callbacks);

// TODO: add proper types (likely boolean)
// @ts-ignore It works, duh
handler?.(event, ...args);
if (!handler) {
return;
}

if (didSucceed === undefined) {
(handler as SingleParameterCallback<typeof event>)(event);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that theoretically we have two possibilities - GestureEventCallbackand GestureTouchEventCallback. However, since those calls will differ only by cast, the resulting JS will look like this:

if(...) {
  handler(event)
} else {
  handler(event)
}

So I don't think it makes sense to keep them separated.

} else {
(handler as DoubleParameterCallback<typeof event>)(event, didSucceed);
}
}
32 changes: 20 additions & 12 deletions packages/react-native-gesture-handler/src/v3/types/ConfigTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,27 @@ import {
} from './EventTypes';
import { WithSharedValue } from './ReanimatedTypes';

export type GestureEventCallback<THandlerData> = (
event: GestureEvent<THandlerData>
) => void;

export type GestureEventCallbackWithDidSucceed<THandlerData> = (
event: GestureEvent<THandlerData>,
didSucceed: boolean
) => void;

export type GestureTouchEventCallback = (event: GestureTouchEvent) => void;

export type GestureCallbacks<THandlerData> = {
onBegin?: (event: GestureEvent<THandlerData>) => void;
onActivate?: (event: GestureEvent<THandlerData>) => void;
onDeactivate?: (
event: GestureEvent<THandlerData>,
didSucceed: boolean
) => void;
onFinalize?: (event: GestureEvent<THandlerData>, didSucceed: boolean) => void;
onUpdate?: ((event: GestureEvent<THandlerData>) => void) | AnimatedEvent;
onTouchesDown?: (event: GestureTouchEvent) => void;
onTouchesMove?: (event: GestureTouchEvent) => void;
onTouchesUp?: (event: GestureTouchEvent) => void;
onTouchesCancel?: (event: GestureTouchEvent) => void;
onBegin?: GestureEventCallback<THandlerData>;
onActivate?: GestureEventCallback<THandlerData>;
onDeactivate?: GestureEventCallbackWithDidSucceed<THandlerData>;
onFinalize?: GestureEventCallbackWithDidSucceed<THandlerData>;
onUpdate?: GestureEventCallback<THandlerData> | AnimatedEvent;
onTouchesDown?: GestureTouchEventCallback;
onTouchesMove?: GestureTouchEventCallback;
onTouchesUp?: GestureTouchEventCallback;
onTouchesCancel?: GestureTouchEventCallback;
};

export type GestureRelations = {
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native-gesture-handler/src/v3/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export type {

export type {
GestureCallbacks,
GestureEventCallback,
GestureEventCallbackWithDidSucceed,
GestureTouchEventCallback,
GestureRelations,
InternalConfigProps,
CommonGestureConfig,
Expand Down
Loading