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 @@ -77,6 +77,7 @@ let Reanimated:
runOnUI<A extends any[], R>(
fn: (...args: A) => R
): (...args: Parameters<typeof fn>) => void;
makeMutable<T>(value: T): { value: T };
}
| undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { CALLBACK_TYPE } from '../../../handlers/gestures/gesture';
import type { ReanimatedContext } from '../../../handlers/gestures/reanimatedWrapper';
import { State } from '../../../State';
import { TouchEventType } from '../../../TouchEventType';
import { tagMessage } from '../../../utils';
import type {
ChangeCalculatorType,
GestureCallbacks,
Expand Down Expand Up @@ -93,16 +92,11 @@ export function handleUpdateEvent<
: eventWithData;

const event = flattenAndFilterEvent(eventWithChanges);

// This should never happen, but since we don't want to call hooks conditionally, we have to mark
// context as possibly undefined to make TypeScript happy.
if (!context) {
throw new Error(tagMessage('Event handler context is not defined'));
}

runCallback(CALLBACK_TYPE.UPDATE, handlers, event);

context.lastUpdateEvent = eventWithData;
if (context) {
context.lastUpdateEvent = eventWithData;
}
}

export function handleTouchEvent<
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { useEffect, useMemo, useRef } from 'react';

import type { ReanimatedHandler } from '../../../handlers/gestures/reanimatedWrapper';
import type {
ReanimatedContext,
ReanimatedHandler,
} from '../../../handlers/gestures/reanimatedWrapper';
import { Reanimated } from '../../../handlers/gestures/reanimatedWrapper';
import type {
ChangeCalculatorType,
Expand All @@ -21,6 +24,16 @@ const workletNOOP = () => {
// no-op
};

const lastUpdateEventMap = Reanimated?.makeMutable(
new Map<number, ReanimatedContext<unknown>>()
);
Comment on lines +27 to +29
Comment thread
j-piasecki marked this conversation as resolved.

function deleteHandlerEventEntry(handlerTag: number) {
'worklet';
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
lastUpdateEventMap!.value.delete(handlerTag);
}

export function useReanimatedEventHandler<
THandlerData,
TExtendedHandlerData extends THandlerData,
Expand Down Expand Up @@ -53,13 +66,21 @@ export function useReanimatedEventHandler<
>
) => {
'worklet';
// If we're on Reanimated path, lastUpdateEventMap should always be defined
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
let context = lastUpdateEventMap!.value.get(event.handlerTag);
if (context === undefined) {
context = { lastUpdateEvent: undefined };
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
lastUpdateEventMap!.value.set(event.handlerTag, context);
}

eventHandler(
handlerTag,
event,
workletizedHandlers,
changeEventCalculator,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain
reanimatedHandler?.context!,
context as ReanimatedContext<TExtendedHandlerData>,
false,
fillInDefaultValues
);
Expand All @@ -77,6 +98,10 @@ export function useReanimatedEventHandler<
// ref from what was actually committed.
useEffect(() => {
prevHandlerTagRef.current = handlerTag;

return () => {
Reanimated?.runOnUI?.(deleteHandlerEventEntry)(handlerTag);
};
}, [handlerTag]);

const reanimatedEvent = Reanimated?.useEvent(
Expand Down
Loading