Skip to content

Commit

Permalink
fix update loop between swipeableMethods and callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
latekvo committed Jun 11, 2024
1 parent 0e34933 commit 3ff0111
Showing 1 changed file with 48 additions and 23 deletions.
71 changes: 48 additions & 23 deletions example/src/new_api/swipeable/Swipeable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import React, {
ForwardedRef,
forwardRef,
useCallback,
useEffect,
useImperativeHandle,
useMemo,
useRef,
} from 'react';
import {
Gesture,
Expand Down Expand Up @@ -217,7 +218,20 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
const showLeftProgress = useSharedValue<number>(0);
const showRightProgress = useSharedValue<number>(0);

let swipeableMethods: SwipeableMethods | undefined;
const swipeableMethods = useRef<SwipeableMethods>({
close: () => {
'worklet';
},
openLeft: () => {
'worklet';
},
openRight: () => {
'worklet';
},
reset: () => {
'worklet';
},
});

const defaultProps = {
friction: 1,
Expand Down Expand Up @@ -321,14 +335,20 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
(isFinished) => {
if (isFinished) {
if (toValue > 0 && props.onSwipeableOpen) {
runOnJS(props.onSwipeableOpen)('left', swipeableMethods!);
runOnJS(props.onSwipeableOpen)(
'left',
swipeableMethods.current
);
} else if (toValue < 0 && props.onSwipeableOpen) {
runOnJS(props.onSwipeableOpen)('right', swipeableMethods!);
runOnJS(props.onSwipeableOpen)(
'right',
swipeableMethods.current
);
} else if (props.onSwipeableClose) {
const closingDirection = fromValue > 0 ? 'left' : 'right';
runOnJS(props.onSwipeableClose)(
closingDirection,
swipeableMethods!
swipeableMethods.current
);
}
}
Expand Down Expand Up @@ -359,8 +379,10 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
dragOffsetFromRightEdge = 10,
} = props;

swipeableMethods = useMemo<SwipeableMethods>(
() => ({
useEffect(() => {
console.log('useEffect');

swipeableMethods.current = {
close() {
'worklet';
animateRow(calculateCurrentOffset(), 0);
Expand All @@ -380,19 +402,18 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
appliedTranslation.value = 0;
rowState.value = 0;
},
}),
[
animateRow,
calculateCurrentOffset,
appliedTranslation,
leftWidth,
rightOffset,
rightWidth,
rowState,
rowWidth,
userDrag,
]
);
};
}, [
animateRow,
calculateCurrentOffset,
appliedTranslation,
leftWidth,
rightOffset,
rightWidth,
rowState,
rowWidth,
userDrag,
]);

const leftAnimatedStyle = useAnimatedStyle(() => ({
transform: [
Expand All @@ -407,7 +428,7 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
{renderLeftActions(
showLeftProgress,
appliedTranslation,
swipeableMethods
swipeableMethods.current
)}
<View
onLayout={({ nativeEvent }) =>
Expand All @@ -430,7 +451,7 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
{renderRightActions(
showRightProgress,
appliedTranslation,
swipeableMethods
swipeableMethods.current
)}
<View
onLayout={({ nativeEvent }) =>
Expand Down Expand Up @@ -521,13 +542,17 @@ const Swipeable = forwardRef<SwipeableMethods, SwipeableProps>(
]);
tapGesture.shouldCancelWhenOutside(true);

useImperativeHandle(ref, () => swipeableMethods, [swipeableMethods]);
useImperativeHandle(ref, () => swipeableMethods.current, [
swipeableMethods,
]);

const animatedStyle = useAnimatedStyle(() => ({
transform: [{ translateX: appliedTranslation.value }],
pointerEvents: rowState.value === 0 ? 'auto' : 'box-only',
}));

console.log('rerender');

const composedGesture = Gesture.Race(panGesture, tapGesture);
return (
<Animated.View
Expand Down

0 comments on commit 3ff0111

Please sign in to comment.