Skip to content

Commit

Permalink
GestureDetector: more checks for mounted component (#2335)
Browse files Browse the repository at this point in the history
## Description

This builds on top of #2262 - I encountered more cases where a component
that was immediately remounting was running into race conditions, so I
added checks to every `setImmediate` call.
  • Loading branch information
phryneas committed Dec 12, 2022
1 parent 5c7df65 commit 4467b88
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/handlers/gestures/GestureDetector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ interface AttachHandlersConfig {
gesture: GestureType[];
viewTag: number;
webEventHandlersRef: React.RefObject<WebEventHandler>;
mountedRef: React.RefObject<boolean>;
}

function attachHandlers({
Expand All @@ -138,6 +139,7 @@ function attachHandlers({
gesture,
viewTag,
webEventHandlersRef,
mountedRef,
}: AttachHandlersConfig) {
if (!preparedGesture.firstExecution) {
gestureConfig.initialize();
Expand All @@ -148,6 +150,9 @@ function attachHandlers({
// use setImmediate to extract handlerTags, because all refs should be initialized
// when it's ran
setImmediate(() => {
if (!mountedRef.current) {
return;
}
gestureConfig.prepare();
});

Expand All @@ -165,6 +170,9 @@ function attachHandlers({
// use setImmediate to extract handlerTags, because all refs should be initialized
// when it's ran
setImmediate(() => {
if (!mountedRef.current) {
return;
}
for (const handler of gesture) {
let requireToFail: number[] = [];
if (handler.config.requireToFail) {
Expand Down Expand Up @@ -654,6 +662,7 @@ export const GestureDetector = (props: GestureDetectorProps) => {
gesture,
viewTag,
webEventHandlersRef,
mountedRef,
});

return () => {
Expand All @@ -675,6 +684,7 @@ export const GestureDetector = (props: GestureDetectorProps) => {
gesture,
viewTag,
webEventHandlersRef,
mountedRef,
});
} else {
updateHandlers(preparedGesture, gestureConfig, gesture, mountedRef);
Expand Down

0 comments on commit 4467b88

Please sign in to comment.