-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24173 from tienifr/fix/23867-app-opens-all-presse…
…d-option Fix: Quickly press different menu items opens them one by one
- Loading branch information
Showing
4 changed files
with
93 additions
and
29 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {InteractionManager} from 'react-native'; | ||
import {useCallback, useState} from 'react'; | ||
|
||
/** | ||
* With any action passed in, it will only allow 1 such action to occur at a time. | ||
* | ||
* @returns {Object} | ||
*/ | ||
export default function useSingleExecution() { | ||
const [isExecuting, setIsExecuting] = useState(false); | ||
|
||
const singleExecution = useCallback( | ||
(action) => () => { | ||
if (isExecuting) { | ||
return; | ||
} | ||
|
||
setIsExecuting(true); | ||
|
||
const execution = action(); | ||
InteractionManager.runAfterInteractions(() => { | ||
if (!(execution instanceof Promise)) { | ||
setIsExecuting(false); | ||
return; | ||
} | ||
execution.finally(() => { | ||
setIsExecuting(false); | ||
}); | ||
}); | ||
}, | ||
[isExecuting], | ||
); | ||
|
||
return {isExecuting, singleExecution}; | ||
} |
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,33 @@ | ||
import {useEffect, useRef} from 'react'; | ||
import {useNavigation} from '@react-navigation/native'; | ||
|
||
/** | ||
* Returns a promise that resolves when navigation finishes. | ||
* Only use when navigating by react-navigation | ||
* | ||
* @returns {function} | ||
*/ | ||
export default function useWaitForNavigation() { | ||
const navigation = useNavigation(); | ||
const resolvePromises = useRef([]); | ||
|
||
useEffect(() => { | ||
const unsubscribeBlur = navigation.addListener('blur', () => { | ||
resolvePromises.current.forEach((resolve) => { | ||
resolve(); | ||
}); | ||
resolvePromises.current = []; | ||
}); | ||
|
||
return () => { | ||
unsubscribeBlur(); | ||
}; | ||
}, [navigation]); | ||
|
||
return (navigate) => () => { | ||
navigate(); | ||
return new Promise((resolve) => { | ||
resolvePromises.current.push(resolve); | ||
}); | ||
}; | ||
} |
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