From 22192f830f2f816399b73477eb84b5f6c36b61a9 Mon Sep 17 00:00:00 2001 From: Daniel Merrill Date: Wed, 10 Jan 2024 06:58:50 -0800 Subject: [PATCH] fix(worklet): fix workletization of touch event gesture callbacks (#2715) ## 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 https://github.com/software-mansion/react-native-reanimated/issues/5555 This case was previously broken: ```tsx function Demo() { const panGesture = Gesture.Pan() panGesture.onTouchesMove((evt, mgr) => { 'worklet' console.log('move!!') }) return ( ) } ``` --- src/handlers/gestures/gestureStateManager.ts | 82 ++++++++++---------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/src/handlers/gestures/gestureStateManager.ts b/src/handlers/gestures/gestureStateManager.ts index 35ff56acf0..7dee92bbfe 100644 --- a/src/handlers/gestures/gestureStateManager.ts +++ b/src/handlers/gestures/gestureStateManager.ts @@ -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, };