Skip to content

Commit

Permalink
fix: patch race condition when calling a modalfy method as the callba…
Browse files Browse the repository at this point in the history
…ck of another modalfy method

Fixes #147.
  • Loading branch information
CharlesMangwa committed Aug 23, 2024
1 parent 2f24e2f commit 487802b
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/lib/StackItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ type Props<P extends ModalfyParams> = SharedProps<P> & {
pendingClosingAction?: ModalPendingClosingAction
}

const addCallbackToMacroTaskQueue = (fn: (() => void) | undefined) => {
if (typeof fn === 'function') {
const timeout = setTimeout(() => {
clearTimeout(timeout)
fn?.()
}, 0)
}
}

const StackItem = <P extends ModalfyParams>({
stack,
zIndex,
Expand Down Expand Up @@ -115,24 +124,24 @@ const StackItem = <P extends ModalfyParams>({
const updateAnimatedValue = useCallback(
(
toValue: number,
closeModalCallback?: (closingElement: ModalStackItem<P>) => void,
modalStackItemCallback?: () => void,
internalClosingCallback?: (closingElement: ModalStackItem<P>) => void,
stackItemCallback?: () => void,
) => {
if (!closeModalCallback && animationIn) {
animationIn(animatedValue, toValue, modalStackItemCallback)
} else if (closeModalCallback && animationOut) {
if (!internalClosingCallback && animationIn) {
animationIn(animatedValue, toValue, stackItemCallback)
} else if (internalClosingCallback && animationOut) {
animationOut(animatedValue, toValue, () => {
closeModalCallback(stackItem)
modalStackItemCallback?.()
internalClosingCallback(stackItem)
addCallbackToMacroTaskQueue(stackItemCallback)
})
} else {
Animated.timing(animatedValue, {
toValue,
useNativeDriver: true,
...(closeModalCallback ? animateOutConfig : animateInConfig),
...(internalClosingCallback ? animateOutConfig : animateInConfig),
}).start(() => {
closeModalCallback?.(stackItem)
modalStackItemCallback?.()
internalClosingCallback?.(stackItem)
addCallbackToMacroTaskQueue(stackItemCallback)
})
}
},
Expand All @@ -147,7 +156,7 @@ const StackItem = <P extends ModalfyParams>({
onCloseListener.current({ type: 'closeModal', origin: wasClosedByBackdropPress ? 'backdrop' : 'default' })
closeModal(modalName)
if (pendingClosingAction?.action === 'closeModal') removeClosingAction(pendingClosingAction)
if (typeof callback === 'function') callback?.()
addCallbackToMacroTaskQueue(callback)
})
},
[
Expand All @@ -170,7 +179,7 @@ const StackItem = <P extends ModalfyParams>({
onCloseListener.current({ type: 'closeModals', origin: 'default' })
let output = closeModals(closingElement)
if (pendingClosingAction?.action === 'closeModals') removeClosingAction(pendingClosingAction)
if (typeof callback === 'function') callback?.()
addCallbackToMacroTaskQueue(callback)
return output
})
},
Expand All @@ -185,7 +194,7 @@ const StackItem = <P extends ModalfyParams>({
onCloseListener.current({ type: 'closeAllModals', origin: wasClosedByBackdropPress ? 'backdrop' : 'default' })
closeAllModals()
if (pendingClosingAction?.action === 'closeAllModals') removeClosingAction(pendingClosingAction)
if (typeof callback === 'function') callback?.()
addCallbackToMacroTaskQueue(callback)
})
},
[closeAllModals, hideBackdrop, pendingClosingAction, position, updateAnimatedValue, wasClosedByBackdropPress],
Expand Down

0 comments on commit 487802b

Please sign in to comment.