-
-
Notifications
You must be signed in to change notification settings - Fork 984
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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">|
- Loading branch information
1 parent
6a7a128
commit d93bfe3
Showing
7 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/handlers/gestures/GestureDetector/useMountReactions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { transformIntoHandlerTags } from '../../utils'; | ||
import { MountRegistry } from '../../../mountRegistry'; | ||
import { AttachedGestureState } from './types'; | ||
import { useEffect } from 'react'; | ||
import { GestureRef } from '../gesture'; | ||
|
||
function shouldUpdateDetector( | ||
relation: GestureRef[] | undefined, | ||
gesture: { handlerTag: number } | ||
) { | ||
if (relation === undefined) { | ||
return false; | ||
} | ||
|
||
for (const tag of transformIntoHandlerTags(relation)) { | ||
if (tag === gesture.handlerTag) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export function useMountReactions( | ||
updateDetector: () => void, | ||
state: AttachedGestureState | ||
) { | ||
useEffect(() => { | ||
return MountRegistry.addMountListener((gesture) => { | ||
// At this point the ref in the gesture config should be updated, so we can check if one of the gestures | ||
// set in a relation with the gesture got mounted. If so, we need to update the detector to propagate | ||
// the changes to the native side. | ||
for (const attachedGesture of state.attachedGestures) { | ||
const blocksHandlers = attachedGesture.config.blocksHandlers; | ||
const requireToFail = attachedGesture.config.requireToFail; | ||
const simultaneousWith = attachedGesture.config.simultaneousWith; | ||
|
||
if ( | ||
shouldUpdateDetector(blocksHandlers, gesture) || | ||
shouldUpdateDetector(requireToFail, gesture) || | ||
shouldUpdateDetector(simultaneousWith, gesture) | ||
) { | ||
updateDetector(); | ||
|
||
// We can safely return here, if any other gestures should be updated, they will be by the above call | ||
return; | ||
} | ||
} | ||
}); | ||
}, [updateDetector, state]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { GestureType } from './handlers/gestures/gesture'; | ||
|
||
interface ReactComponentWithHandlerTag extends React.Component { | ||
handlerTag: number; | ||
} | ||
|
||
export type GestureMountListener = ( | ||
gesture: GestureType | ReactComponentWithHandlerTag | ||
) => void; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-extraneous-class | ||
export class MountRegistry { | ||
private static mountListeners = new Set<GestureMountListener>(); | ||
private static unmountListeners = new Set<GestureMountListener>(); | ||
|
||
static addMountListener(listener: GestureMountListener): () => void { | ||
this.mountListeners.add(listener); | ||
|
||
return () => { | ||
this.mountListeners.delete(listener); | ||
}; | ||
} | ||
|
||
static addUnmountListener(listener: GestureMountListener): () => void { | ||
this.unmountListeners.add(listener); | ||
|
||
return () => { | ||
this.unmountListeners.delete(listener); | ||
}; | ||
} | ||
|
||
static gestureHandlerWillMount(handler: React.Component) { | ||
this.mountListeners.forEach((listener) => | ||
listener(handler as ReactComponentWithHandlerTag) | ||
); | ||
} | ||
|
||
static gestureHandlerWillUnmount(handler: React.Component) { | ||
this.unmountListeners.forEach((listener) => | ||
listener(handler as ReactComponentWithHandlerTag) | ||
); | ||
} | ||
|
||
static gestureWillMount(gesture: GestureType) { | ||
this.mountListeners.forEach((listener) => listener(gesture)); | ||
} | ||
|
||
static gestureWillUnmount(gesture: GestureType) { | ||
this.unmountListeners.forEach((listener) => listener(gesture)); | ||
} | ||
} |