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

[RNMobile] Fix a crash related to Reanimated when closing the editor #52320

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
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
Next Next commit
Update block drop position using Reanimated's shared value
Seems there's some kind of incompatibility on calling a JS function from a worklet invoked from a gesture handler. For this reason, the logic to set the dropping insertion point has been updated. It now uses a Reanimated's shared value to keep the dragging over position and  `useDerivedValue` hook to listen for changes.
  • Loading branch information
fluiddot committed Jul 5, 2023
commit 62a00fce3da524436ebb69654e446f3f08369d95
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ const BlockDraggableWrapper = ( { children, isRTL } ) => {
draggingScrollHandler( event );
};

const { onBlockDragOver, onBlockDragEnd, onBlockDrop, targetBlockIndex } =
useBlockDropZone();
const {
onBlockDragOverWorklet,
onBlockDragEnd,
onBlockDrop,
targetBlockIndex,
} = useBlockDropZone();

// Stop dragging blocks if the block draggable is unmounted.
useEffect( () => {
Expand Down Expand Up @@ -184,7 +188,7 @@ const BlockDraggableWrapper = ( { children, isRTL } ) => {
chip.y.value = dragPosition.y;
currentYPosition.value = dragPosition.y;

runOnJS( onBlockDragOver )( { x, y: y + scroll.offsetY.value } );
onBlockDragOverWorklet( { x, y: y + scroll.offsetY.value } );

// Update scrolling velocity
scrollOnDragOver( dragPosition.y );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
/**
* External dependencies
*/
import { useSharedValue } from 'react-native-reanimated';
import {
runOnJS,
useDerivedValue,
useSharedValue,
} from 'react-native-reanimated';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useCallback } from '@wordpress/element';
import { useThrottle } from '@wordpress/compose';

/**
* Internal dependencies
Expand All @@ -18,6 +21,8 @@ import { useBlockListContext } from '../block-list/block-list-context';
import { getDistanceToNearestEdge } from '../../utils/math';
import useOnBlockDrop from '../use-on-block-drop';

const UPDATE_TARGET_BLOCK_INDEX_THRESHOLD = 20; // In pixels

/** @typedef {import('../../utils/math').WPPoint} WPPoint */

/**
Expand Down Expand Up @@ -111,50 +116,82 @@ export default function useBlockDropZone( {
rootClientId: targetRootClientId = '',
} = {} ) {
const targetBlockIndex = useSharedValue( null );
const dragPosition = {
x: useSharedValue( 0 ),
y: useSharedValue( 0 ),
};
const prevDragPosition = {
x: useSharedValue( 0 ),
y: useSharedValue( 0 ),
};

const { getBlockListSettings, getSettings } = useSelect( blockEditorStore );
const { blocksLayouts, getBlockLayoutsOrderedByYCoord } =
useBlockListContext();

const getSortedBlocksLayouts = useCallback( () => {
return getBlockLayoutsOrderedByYCoord( blocksLayouts.current );
// We use the value of `blocksLayouts` as the dependency.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [ blocksLayouts.current ] );
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved

const isRTL = getSettings().isRTL;

const onBlockDrop = useOnBlockDrop();

const throttled = useThrottle(
useCallback(
( event ) => {
const sortedBlockLayouts = getSortedBlocksLayouts();

const targetIndex = getNearestBlockIndex(
sortedBlockLayouts,
{ x: event.x, y: event.y },
getBlockListSettings( targetRootClientId )?.orientation,
isRTL
);
if ( targetIndex !== null ) {
targetBlockIndex.value = targetIndex ?? 0;
}
},
[
getSortedBlocksLayouts,
getNearestBlockIndex,
getBlockListSettings,
targetBlockIndex,
]
),
200
const updateTargetBlockIndex = useCallback(
( event ) => {
const sortedBlockLayouts = getSortedBlocksLayouts();

const targetIndex = getNearestBlockIndex(
sortedBlockLayouts,
{ x: event.x, y: event.y },
getBlockListSettings( targetRootClientId )?.orientation,
isRTL
);
if ( targetIndex !== null ) {
targetBlockIndex.value = targetIndex ?? 0;
}
},
[
getSortedBlocksLayouts,
getBlockListSettings,
targetRootClientId,
isRTL,
targetBlockIndex,
]
);

useDerivedValue( () => {
const x = dragPosition.x.value;
const y = dragPosition.y.value;
const prevX = prevDragPosition.x.value;
const prevY = prevDragPosition.y.value;
// `updateTargetBlockIndex` performs expensive calculations, so we throttle
// the call using a offset threshold based on the dragging position.
if (
Math.abs( x - prevX ) >= UPDATE_TARGET_BLOCK_INDEX_THRESHOLD ||
Math.abs( y - prevY ) >= UPDATE_TARGET_BLOCK_INDEX_THRESHOLD
Comment on lines +173 to +174
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool and nice approach!

) {
runOnJS( updateTargetBlockIndex )( { x, y } );
prevDragPosition.x.value = x;
prevDragPosition.y.value = y;
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
return false;
} );

return {
onBlockDragOver( event ) {
throttled( event );
onBlockDragOver( { x, y } ) {
dragPosition.x.value = x;
dragPosition.y.value = y;
},
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
onBlockDragOverWorklet( { x, y } ) {
'worklet';
dragPosition.x.value = x;
dragPosition.y.value = y;
},
onBlockDragEnd() {
throttled.cancel();
dcalhoun marked this conversation as resolved.
Show resolved Hide resolved
targetBlockIndex.value = null;
},
onBlockDrop: ( event ) => {
Expand Down