From c97ff978ee258af698628b6a4632074dec451ddb Mon Sep 17 00:00:00 2001 From: Getabalew Tesfaye Date: Tue, 7 Nov 2023 15:36:17 +0300 Subject: [PATCH] split useSignleExecution for web and native --- src/hooks/useSingleExecution/index.native.js | 40 ------------------- .../index.native.ts} | 0 .../useSingleExecution/{index.js => index.ts} | 8 ++-- 3 files changed, 4 insertions(+), 44 deletions(-) delete mode 100644 src/hooks/useSingleExecution/index.native.js rename src/hooks/{useSingleExecution.ts => useSingleExecution/index.native.ts} (100%) rename src/hooks/useSingleExecution/{index.js => index.ts} (75%) diff --git a/src/hooks/useSingleExecution/index.native.js b/src/hooks/useSingleExecution/index.native.js deleted file mode 100644 index 16a98152def1..000000000000 --- a/src/hooks/useSingleExecution/index.native.js +++ /dev/null @@ -1,40 +0,0 @@ -import {useCallback, useRef, useState} from 'react'; -import {InteractionManager} from 'react-native'; - -type Action = (...params: T) => void | Promise; - -/** - * With any action passed in, it will only allow 1 such action to occur at a time. - */ -export default function useSingleExecution() { - const [isExecuting, setIsExecuting] = useState(false); - const isExecutingRef = useRef(); - - isExecutingRef.current = isExecuting; - - const singleExecution = useCallback( - (action: Action) => - (...params: T) => { - if (isExecutingRef.current) { - return; - } - - setIsExecuting(true); - isExecutingRef.current = true; - - const execution = action(...params); - InteractionManager.runAfterInteractions(() => { - if (!(execution instanceof Promise)) { - setIsExecuting(false); - return; - } - execution.finally(() => { - setIsExecuting(false); - }); - }); - }, - [], - ); - - return {isExecuting, singleExecution}; -} diff --git a/src/hooks/useSingleExecution.ts b/src/hooks/useSingleExecution/index.native.ts similarity index 100% rename from src/hooks/useSingleExecution.ts rename to src/hooks/useSingleExecution/index.native.ts diff --git a/src/hooks/useSingleExecution/index.js b/src/hooks/useSingleExecution/index.ts similarity index 75% rename from src/hooks/useSingleExecution/index.js rename to src/hooks/useSingleExecution/index.ts index 0b2e565c46bc..c37087d27c5f 100644 --- a/src/hooks/useSingleExecution/index.js +++ b/src/hooks/useSingleExecution/index.ts @@ -1,16 +1,16 @@ import {useCallback} from 'react'; +type Action = (...params: T) => void | Promise; + /** * This hook was specifically written for native issue * more information: https://github.com/Expensify/App/pull/24614 https://github.com/Expensify/App/pull/24173 * on web we don't need this mechanism so we just call the action directly. - * - * @returns {Object} */ export default function useSingleExecution() { const singleExecution = useCallback( - (action) => - (...params) => { + (action: Action) => + (...params: T) => { action(...params); }, [],