Skip to content

Commit d93bfe3

Browse files
authored
Fix relations not working when one of the child dependencies remounts without a render in the parent (#3269)
## Description Fixes #3265 As @gaearon pointed out in the issue: > I think what's happening here is that the library doesn't consider the fact that the contents of a ref can change over time without the parent knowing about it. Rather, it seems to be operating under assumption that a ref change at the bottom will always be accompanied by a state change near the top (wherever blocksExternalGesture is called). That's unfortunately true, and the current API doesn't allow to use ref callbacks to handle this (nor would it be easy to update it to do that). This is one more point to the pile of reasons why the API needs an overhaul. For the time being, I implemented a mechanism that allows `GestureDetector` to listen to the mounting and unmounting of gestures. This way it can react to a remount of a gesture it depends on and update the native side if necessary. ## Test plan Tested on the Example app and the reproducer from the issue |Android|iOS| |-|-| |<video src="https://github.com/user-attachments/assets/4df2e1d7-3932-4c1e-a305-ac1a0207ba20">|<video src="https://github.com/user-attachments/assets/e522bdb4-98a9-4a78-b9e8-2f0f9b28b62c">|
1 parent 6a7a128 commit d93bfe3

File tree

7 files changed

+120
-1
lines changed

7 files changed

+120
-1
lines changed

src/handlers/createHandler.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { ActionType } from '../ActionType';
2424
import { PressabilityDebugView } from './PressabilityDebugView';
2525
import GestureHandlerRootViewContext from '../GestureHandlerRootViewContext';
2626
import { ghQueueMicrotask } from '../ghQueueMicrotask';
27+
import { MountRegistry } from '../mountRegistry';
2728

2829
const UIManagerAny = UIManager as any;
2930

@@ -262,6 +263,8 @@ export default function createHandler<
262263
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
263264
delete handlerIDToTag[handlerID];
264265
}
266+
267+
MountRegistry.gestureHandlerWillUnmount(this);
265268
}
266269

267270
private onGestureHandlerEvent = (event: GestureEvent<U>) => {
@@ -373,6 +376,10 @@ export default function createHandler<
373376
}
374377

375378
scheduleFlushOperations();
379+
380+
ghQueueMicrotask(() => {
381+
MountRegistry.gestureHandlerWillMount(this);
382+
});
376383
};
377384

378385
private updateGestureHandler = (

src/handlers/gestures/GestureDetector/attachHandlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
checkGestureCallbacksForWorklets,
1515
ALLOWED_PROPS,
1616
} from './utils';
17+
import { MountRegistry } from '../../../mountRegistry';
1718

1819
interface AttachHandlersConfig {
1920
preparedGesture: AttachedGestureState;
@@ -93,6 +94,8 @@ export function attachHandlers({
9394
actionType
9495
);
9596
}
97+
98+
MountRegistry.gestureWillMount(gesture);
9699
}
97100

98101
preparedGesture.attachedGestures = gesturesToAttach;

src/handlers/gestures/GestureDetector/dropHandlers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ import { unregisterHandler } from '../../handlersRegistry';
22
import RNGestureHandlerModule from '../../../RNGestureHandlerModule';
33
import { scheduleFlushOperations } from '../../utils';
44
import { AttachedGestureState } from './types';
5+
import { MountRegistry } from '../../../mountRegistry';
56

67
export function dropHandlers(preparedGesture: AttachedGestureState) {
78
for (const handler of preparedGesture.attachedGestures) {
89
RNGestureHandlerModule.dropGestureHandler(handler.handlerTag);
910

1011
unregisterHandler(handler.handlerTag, handler.config.testId);
12+
13+
MountRegistry.gestureWillUnmount(handler);
1114
}
1215

1316
scheduleFlushOperations();

src/handlers/gestures/GestureDetector/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { useWebEventHandlers } from './utils';
2323
import { Wrap, AnimatedWrap } from './Wrap';
2424
import { useDetectorUpdater } from './useDetectorUpdater';
2525
import { useViewRefHandler } from './useViewRefHandler';
26+
import { useMountReactions } from './useMountReactions';
2627

2728
function propagateDetectorConfig(
2829
props: GestureDetectorProps,
@@ -174,6 +175,8 @@ export const GestureDetector = (props: GestureDetectorProps) => {
174175
}
175176
}, [props]);
176177

178+
useMountReactions(updateAttachedGestures, preparedGesture);
179+
177180
if (shouldUseReanimated) {
178181
return (
179182
<AnimatedWrap
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { transformIntoHandlerTags } from '../../utils';
2+
import { MountRegistry } from '../../../mountRegistry';
3+
import { AttachedGestureState } from './types';
4+
import { useEffect } from 'react';
5+
import { GestureRef } from '../gesture';
6+
7+
function shouldUpdateDetector(
8+
relation: GestureRef[] | undefined,
9+
gesture: { handlerTag: number }
10+
) {
11+
if (relation === undefined) {
12+
return false;
13+
}
14+
15+
for (const tag of transformIntoHandlerTags(relation)) {
16+
if (tag === gesture.handlerTag) {
17+
return true;
18+
}
19+
}
20+
21+
return false;
22+
}
23+
24+
export function useMountReactions(
25+
updateDetector: () => void,
26+
state: AttachedGestureState
27+
) {
28+
useEffect(() => {
29+
return MountRegistry.addMountListener((gesture) => {
30+
// At this point the ref in the gesture config should be updated, so we can check if one of the gestures
31+
// set in a relation with the gesture got mounted. If so, we need to update the detector to propagate
32+
// the changes to the native side.
33+
for (const attachedGesture of state.attachedGestures) {
34+
const blocksHandlers = attachedGesture.config.blocksHandlers;
35+
const requireToFail = attachedGesture.config.requireToFail;
36+
const simultaneousWith = attachedGesture.config.simultaneousWith;
37+
38+
if (
39+
shouldUpdateDetector(blocksHandlers, gesture) ||
40+
shouldUpdateDetector(requireToFail, gesture) ||
41+
shouldUpdateDetector(simultaneousWith, gesture)
42+
) {
43+
updateDetector();
44+
45+
// We can safely return here, if any other gestures should be updated, they will be by the above call
46+
return;
47+
}
48+
}
49+
});
50+
}, [updateDetector, state]);
51+
}

src/handlers/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ export function filterConfig(
3636
}
3737
return filteredConfig;
3838
}
39-
function transformIntoHandlerTags(handlerIDs: any) {
39+
40+
export function transformIntoHandlerTags(handlerIDs: any) {
4041
handlerIDs = toArray(handlerIDs);
4142

4243
if (Platform.OS === 'web') {

src/mountRegistry.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { GestureType } from './handlers/gestures/gesture';
2+
3+
interface ReactComponentWithHandlerTag extends React.Component {
4+
handlerTag: number;
5+
}
6+
7+
export type GestureMountListener = (
8+
gesture: GestureType | ReactComponentWithHandlerTag
9+
) => void;
10+
11+
// eslint-disable-next-line @typescript-eslint/no-extraneous-class
12+
export class MountRegistry {
13+
private static mountListeners = new Set<GestureMountListener>();
14+
private static unmountListeners = new Set<GestureMountListener>();
15+
16+
static addMountListener(listener: GestureMountListener): () => void {
17+
this.mountListeners.add(listener);
18+
19+
return () => {
20+
this.mountListeners.delete(listener);
21+
};
22+
}
23+
24+
static addUnmountListener(listener: GestureMountListener): () => void {
25+
this.unmountListeners.add(listener);
26+
27+
return () => {
28+
this.unmountListeners.delete(listener);
29+
};
30+
}
31+
32+
static gestureHandlerWillMount(handler: React.Component) {
33+
this.mountListeners.forEach((listener) =>
34+
listener(handler as ReactComponentWithHandlerTag)
35+
);
36+
}
37+
38+
static gestureHandlerWillUnmount(handler: React.Component) {
39+
this.unmountListeners.forEach((listener) =>
40+
listener(handler as ReactComponentWithHandlerTag)
41+
);
42+
}
43+
44+
static gestureWillMount(gesture: GestureType) {
45+
this.mountListeners.forEach((listener) => listener(gesture));
46+
}
47+
48+
static gestureWillUnmount(gesture: GestureType) {
49+
this.unmountListeners.forEach((listener) => listener(gesture));
50+
}
51+
}

0 commit comments

Comments
 (0)