Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove shared value read on the JS thread during detector update #2957

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Changes from 1 commit
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
35 changes: 15 additions & 20 deletions src/handlers/gestures/GestureDetector/updateHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,23 @@ export function updateHandlers(
if (!preparedGesture.isMounted) {
return;
}

// if amount of gesture configs changes, we need to update the callbacks in shared value
let shouldUpdateSharedValueIfUsed =
preparedGesture.attachedGestures.length !== newGestures.length;

for (let i = 0; i < newGestures.length; i++) {
const handler = preparedGesture.attachedGestures[i];

// if the gestureId is different (gesture isn't wrapped with useMemo or its dependencies changed),
// we need to update the shared value, assuming the gesture runs on UI thread or the thread changed
if (
handler.handlers.gestureId !== newGestures[i].handlers.gestureId &&
(newGestures[i].shouldUseReanimated || handler.shouldUseReanimated)
) {
shouldUpdateSharedValueIfUsed = true;
}

handler.config = newGestures[i].config;
handler.handlers = newGestures[i].handlers;

Expand All @@ -59,32 +73,13 @@ export function updateHandlers(
}

if (preparedGesture.animatedHandlers) {
const previousHandlersValue =
preparedGesture.animatedHandlers.value ?? [];
const newHandlersValue = preparedGesture.attachedGestures
.filter((g) => g.shouldUseReanimated) // ignore gestures that shouldn't run on UI
.map((g) => g.handlers) as unknown as HandlerCallbacks<
Record<string, unknown>
>[];

// if amount of gesture configs changes, we need to update the callbacks in shared value
let shouldUpdateSharedValue =
previousHandlersValue.length !== newHandlersValue.length;

if (!shouldUpdateSharedValue) {
// if the amount is the same, we need to check if any of the configs inside has changed
for (let i = 0; i < newHandlersValue.length; i++) {
if (
// we can use the `gestureId` prop as it's unique for every config instance
newHandlersValue[i].gestureId !== previousHandlersValue[i].gestureId
) {
shouldUpdateSharedValue = true;
break;
}
}
}

if (shouldUpdateSharedValue) {
if (shouldUpdateSharedValueIfUsed) {
j-piasecki marked this conversation as resolved.
Show resolved Hide resolved
preparedGesture.animatedHandlers.value = newHandlersValue;
}
}
Expand Down
Loading