Skip to content

Commit

Permalink
fix(worklet): fix workletization of touch event gesture callbacks (#2715
Browse files Browse the repository at this point in the history
)

## Description

Workletized touch event gesture callbacks were causing Reanimated errors
to be thrown in RN 73 (Expo 50 beta).
Not sure if this is a bug in RNGH or Reanimated, but moving the function
definition outside of the object seems to fix the issue (sounds like a
bug in the reanimated babel plugin if nested functions should be able to
be workiletized?)

Fixes
software-mansion/react-native-reanimated#5555

This case was previously broken:

```tsx
function Demo() {

  const panGesture = Gesture.Pan()
  
  panGesture.onTouchesMove((evt, mgr) => {
    'worklet'
    console.log('move!!')
  })

  return (
    <GestureHandlerRootView style={{ flex: 1, backgroundColor: 'seashell' }}>
      <GestureDetector gesture={panGesture}>
        <Animated.View style={{ flex: 1 }} />
      </GestureDetector>
    </GestureHandlerRootView>
  )
}
```
  • Loading branch information
computerjazz authored and j-piasecki committed Jan 12, 2024
1 parent 55dac2c commit 22192f8
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions src/handlers/gestures/gestureStateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,47 @@ const warningMessage = tagMessage(
const REANIMATED_AVAILABLE = Reanimated?.useSharedValue !== undefined;
const setGestureState = Reanimated?.setGestureState;

function create(handlerTag: number): GestureStateManagerType {
'worklet';
return {
begin: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.BEGAN);
} else {
console.warn(warningMessage);
}
},

activate: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.ACTIVE);
} else {
console.warn(warningMessage);
}
},

fail: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.FAILED);
} else {
console.warn(warningMessage);
}
},

end: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.END);
} else {
console.warn(warningMessage);
}
},
};
}

export const GestureStateManager = {
create(handlerTag: number): GestureStateManagerType {
'worklet';
return {
begin: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.BEGAN);
} else {
console.warn(warningMessage);
}
},

activate: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.ACTIVE);
} else {
console.warn(warningMessage);
}
},

fail: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.FAILED);
} else {
console.warn(warningMessage);
}
},

end: () => {
'worklet';
if (REANIMATED_AVAILABLE) {
setGestureState(handlerTag, State.END);
} else {
console.warn(warningMessage);
}
},
};
},
create,
};

0 comments on commit 22192f8

Please sign in to comment.