Skip to content
Open
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
Expand Up @@ -3,6 +3,10 @@ import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture';
import {
ChangeCalculatorType,
GestureCallbacks,
GestureEvent,
GestureEventCallback,
GestureEventCallbackWithSuccess,
GestureTouchEventCallback,
UnpackedGestureHandlerEvent,
} from '../../types';

Expand Down Expand Up @@ -60,15 +64,19 @@ export function prepareTouchHandlers<THandlerData>(
export function getHandler<THandlerData>(
type: CALLBACK_TYPE,
callbacks: GestureCallbacks<THandlerData>
) {
):
| GestureEventCallback<THandlerData>
| GestureEventCallbackWithSuccess<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 @@ -105,12 +113,26 @@ export function runCallback<THandlerData>(
type: CALLBACK_TYPE,
callbacks: GestureCallbacks<THandlerData>,
event: UnpackedGestureHandlerEvent<THandlerData>,
...args: unknown[]
success?: 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 (success !== undefined) {
(handler as GestureEventCallbackWithSuccess<THandlerData>)?.(
event as GestureEvent<THandlerData>,
success
);
} else {
// @ts-ignore event is an object.
if ('allTouches' in event) {
(handler as GestureTouchEventCallback)?.(event);
} else {
(handler as GestureEventCallback<THandlerData>)?.(event);
}
}
}
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 GestureEventCallbackWithSuccess<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?: GestureEventCallbackWithSuccess<THandlerData>;
onFinalize?: GestureEventCallbackWithSuccess<THandlerData>;
onUpdate?: GestureEventCallback<THandlerData> | AnimatedEvent;
onTouchesDown?: GestureTouchEventCallback;
onTouchesMove?: GestureTouchEventCallback;
onTouchesUp?: GestureTouchEventCallback;
onTouchesCancel?: GestureTouchEventCallback;
};

export type GestureRelations = {
Expand Down
Loading