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,4 +1,4 @@
import React from 'react';
import React, { useMemo } from 'react';
import HostGestureDetector from './HostGestureDetector';
import { configureRelations, ensureNativeDetectorComponent } from './utils';
import { isComposedGesture } from '../hooks/utils/relationUtils';
Expand All @@ -22,6 +22,10 @@ export function NativeDetector<THandlerData, TConfig>({
ensureNativeDetectorComponent(NativeDetectorComponent);
configureRelations(gesture);

const handlerTags = useMemo(() => {
return isComposedGesture(gesture) ? gesture.tags : [gesture.tag];
}, [gesture]);

return (
<NativeDetectorComponent
// @ts-ignore This is a type mismatch between RNGH types and RN Codegen types
Expand Down Expand Up @@ -50,7 +54,7 @@ export function NativeDetector<THandlerData, TConfig>({
gesture.detectorCallbacks.onGestureHandlerAnimatedEvent
}
moduleId={globalThis._RNGH_MODULE_ID}
handlerTags={isComposedGesture(gesture) ? gesture.tags : [gesture.tag]}
handlerTags={handlerTags}
style={nativeDetectorStyles.detector}>
{children}
</NativeDetectorComponent>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { useMemo } from 'react';
import { BaseGestureConfig } from '../../../types';
import { extractStateChangeHandlers } from '../../utils';
import { prepareStateChangeHandlers } from '../../utils';
import { getStateChangeHandler } from '../stateChangeHandler';

export function useGestureStateChangeEvent<THandlerData, TConfig>(
handlerTag: number,
config: BaseGestureConfig<THandlerData, TConfig>
) {
return useMemo(() => {
const handlers = extractStateChangeHandlers(config);
const handlers = prepareStateChangeHandlers({
onBegin: config.onBegin,
onStart: config.onStart,
onEnd: config.onEnd,
onFinalize: config.onFinalize,
});
return getStateChangeHandler(handlerTag, handlers);
}, [handlerTag, config]);
}, [
handlerTag,
config.onBegin,
config.onStart,
config.onEnd,
config.onFinalize,
]);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { useMemo } from 'react';
import { BaseGestureConfig } from '../../../types';
import { extractTouchHandlers } from '../../utils';
import { prepareTouchHandlers } from '../../utils';
import { getTouchEventHandler } from '../touchEventHandler';

export function useGestureTouchEvent<THandlerData, TConfig>(
handlerTag: number,
config: BaseGestureConfig<THandlerData, TConfig>
) {
return useMemo(() => {
const handlers = extractTouchHandlers(config);
const handlers = prepareTouchHandlers({
onTouchesDown: config.onTouchesDown,
onTouchesMove: config.onTouchesMove,
onTouchesUp: config.onTouchesUp,
onTouchesCancel: config.onTouchesCancel,
});
return getTouchEventHandler(handlerTag, handlers);
}, [handlerTag, config]);
}, [
handlerTag,
config.onTouchesDown,
config.onTouchesMove,
config.onTouchesUp,
config.onTouchesCancel,
]);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { extractUpdateHandlers, isAnimatedEvent } from '../../utils';
import { prepareUpdateHandlers, isAnimatedEvent } from '../../utils';
import { ReanimatedContext } from '../../../../handlers/gestures/reanimatedWrapper';
import { getUpdateHandler } from '../updateHandler';
import { BaseGestureConfig } from '../../../types';
Expand All @@ -9,7 +9,12 @@ export function useGestureUpdateEvent<THandlerData, TConfig>(
config: BaseGestureConfig<THandlerData, TConfig>
) {
return useMemo(() => {
const { handlers, changeEventCalculator } = extractUpdateHandlers(config);
const { handlers, changeEventCalculator } = prepareUpdateHandlers(
{
onUpdate: config.onUpdate,
},
config.changeEventCalculator
);

const jsContext: ReanimatedContext<THandlerData> = {
lastUpdateEvent: undefined,
Expand All @@ -23,5 +28,5 @@ export function useGestureUpdateEvent<THandlerData, TConfig>(
jsContext,
changeEventCalculator
);
}, [handlerTag, config]);
}, [handlerTag, config.onUpdate, config.changeEventCalculator]);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper';
import { BaseGestureConfig } from '../../../types';
import { extractStateChangeHandlers } from '../../utils';
import { prepareStateChangeHandlers } from '../../utils';
import { getStateChangeHandler } from '../stateChangeHandler';

export function useReanimatedStateChangeEvent<THandlerData, TConfig>(
handlerTag: number,
config: BaseGestureConfig<THandlerData, TConfig>
) {
const handlers = extractStateChangeHandlers(config);
const handlers = prepareStateChangeHandlers({
onBegin: config.onBegin,
onStart: config.onStart,
onEnd: config.onEnd,
onFinalize: config.onFinalize,
});

const callback = getStateChangeHandler(handlerTag, handlers);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper';
import { BaseGestureConfig } from '../../../types';
import { extractTouchHandlers } from '../../utils';
import { prepareTouchHandlers } from '../../utils';
import { getTouchEventHandler } from '../touchEventHandler';

export function useReanimatedTouchEvent<THandlerData, TConfig>(
handlerTag: number,
config: BaseGestureConfig<THandlerData, TConfig>
) {
const handlers = extractTouchHandlers(config);
const handlers = prepareTouchHandlers({
onTouchesDown: config.onTouchesDown,
onTouchesMove: config.onTouchesMove,
onTouchesUp: config.onTouchesUp,
onTouchesCancel: config.onTouchesCancel,
});

const callback = getTouchEventHandler(handlerTag, handlers);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Reanimated } from '../../../../handlers/gestures/reanimatedWrapper';
import { BaseGestureConfig } from '../../../types';
import { extractUpdateHandlers } from '../../utils';
import { prepareUpdateHandlers } from '../../utils';
import { getUpdateHandler } from '../updateHandler';

export function useReanimatedUpdateEvent<THandlerData, TConfig>(
handlerTag: number,
config: BaseGestureConfig<THandlerData, TConfig>
) {
const { handlers, changeEventCalculator } = extractUpdateHandlers(config);
const { handlers, changeEventCalculator } = prepareUpdateHandlers(
{
onUpdate: config.onUpdate,
},
config.changeEventCalculator
);

// We don't want to call hooks conditionally, therefore `useHandler` and `useEvent` will be always called.
// The only difference is whether we will send events to Reanimated or not.
Expand Down
12 changes: 10 additions & 2 deletions packages/react-native-gesture-handler/src/v3/hooks/useGesture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,16 @@ export function useGesture<THandlerData, TConfig>(
}

const gestureRelations = useMemo(
() => prepareRelations(config, tag),
[config, tag]
() =>
prepareRelations(
{
simultaneousWith: config.simultaneousWith,
requireToFail: config.requireToFail,
block: config.block,
},
tag
),
[tag, config.simultaneousWith, config.requireToFail, config.block]
);

const currentGestureRef = useRef({ type: '', tag: -1 });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { TouchEventType } from '../../../TouchEventType';
import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture';
import {
BaseGestureConfig,
ChangeCalculatorType,
UnpackedGestureHandlerEvent,
GestureCallbacks,
} from '../../types';

export function extractStateChangeHandlers<THandlerData, TConfig>(
config: BaseGestureConfig<THandlerData, TConfig>
export function prepareStateChangeHandlers<THandlerData>(
callbacks: GestureCallbacks<THandlerData>
): GestureCallbacks<THandlerData> {
'worklet';
const { onBegin, onStart, onEnd, onFinalize } = config;
const { onBegin, onStart, onEnd, onFinalize } = callbacks;

const handlers: GestureCallbacks<THandlerData> = {
...(onBegin ? { onBegin } : {}),
Expand All @@ -28,11 +27,12 @@ type UpdateHandlersReturnType<THandlerData> = {
changeEventCalculator?: ChangeCalculatorType<THandlerData>;
};

export function extractUpdateHandlers<THandlerData, TConfig>(
config: BaseGestureConfig<THandlerData, TConfig>
export function prepareUpdateHandlers<THandlerData>(
callbacks: GestureCallbacks<THandlerData>,
changeEventCalculator?: ChangeCalculatorType<THandlerData>
): UpdateHandlersReturnType<THandlerData> {
'worklet';
const { onUpdate, changeEventCalculator } = config;
const { onUpdate } = callbacks;

const handlers: GestureCallbacks<THandlerData> = {
...(onUpdate ? { onUpdate } : {}),
Expand All @@ -41,10 +41,11 @@ export function extractUpdateHandlers<THandlerData, TConfig>(
return { handlers, changeEventCalculator };
}

export function extractTouchHandlers<THandlerData, TConfig>(
config: BaseGestureConfig<THandlerData, TConfig>
export function prepareTouchHandlers<THandlerData>(
callbacks: GestureCallbacks<THandlerData>
): GestureCallbacks<THandlerData> {
const { onTouchesDown, onTouchesMove, onTouchesUp, onTouchesCancel } = config;
const { onTouchesDown, onTouchesMove, onTouchesUp, onTouchesCancel } =
callbacks;

const handlers: GestureCallbacks<THandlerData> = {
...(onTouchesDown ? { onTouchesDown } : {}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
BaseGestureConfig,
ComposedGesture,
ExternalRelations,
Gesture,
GestureRelations,
} from '../../types';
Expand Down Expand Up @@ -58,16 +58,16 @@ function makeSimultaneousWithSymmetric(
}
}

export function prepareRelations<THandlerData, TConfig>(
config: BaseGestureConfig<THandlerData, TConfig>,
export function prepareRelations(
relations: ExternalRelations,
handlerTag: number
): GestureRelations {
makeSimultaneousWithSymmetric(config.simultaneousWith, handlerTag);
makeSimultaneousWithSymmetric(relations.simultaneousWith, handlerTag);

return {
simultaneousHandlers: extractHandlerTags(config.simultaneousWith),
waitFor: extractHandlerTags(config.requireToFail),
blocksHandlers: extractHandlerTags(config.block),
simultaneousHandlers: extractHandlerTags(relations.simultaneousWith),
waitFor: extractHandlerTags(relations.requireToFail),
blocksHandlers: extractHandlerTags(relations.block),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { FilterNeverProperties } from './UtilityTypes';

// Unfortunately, this type cannot be moved into ConfigTypes.ts because of circular dependency
type ExternalRelations = {
export type ExternalRelations = {
simultaneousWith?: Gesture | Gesture[];
requireToFail?: Gesture | Gesture[];
block?: Gesture | Gesture[];
Expand Down
Loading