From 4d00e776aa83f324db6c0f063639d29e811035dd Mon Sep 17 00:00:00 2001 From: brusher_ru Date: Fri, 4 Oct 2024 17:06:15 -0300 Subject: [PATCH 1/3] refactor: add default `ms` value for `postpone` function --- src/hooks/useConfirmation.ts | 2 +- src/screens/welcome/ImportScreen.tsx | 3 +-- src/store/usePassword.ts | 2 +- src/utils/promises.ts | 13 +++++++++---- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/hooks/useConfirmation.ts b/src/hooks/useConfirmation.ts index 10905a5..5983917 100644 --- a/src/hooks/useConfirmation.ts +++ b/src/hooks/useConfirmation.ts @@ -50,7 +50,7 @@ const usePassword = (): UseConfirmationReturnType => { const res = await callback(); _onClose(); eventEmitter.emit('success', res); - }, 1); + }); const onClose = () => { eventEmitter.emit('close', true); diff --git a/src/screens/welcome/ImportScreen.tsx b/src/screens/welcome/ImportScreen.tsx index 86c1c07..b4ab79d 100644 --- a/src/screens/welcome/ImportScreen.tsx +++ b/src/screens/welcome/ImportScreen.tsx @@ -82,8 +82,7 @@ function ImportScreen(): JSX.Element { const success = await postpone( // We need to postpone it for one tick // to allow component to re-render - () => openWallet(walletFileContent, password), - 1 + () => openWallet(walletFileContent, password) ); setIsLoading(false); if (!success) { diff --git a/src/store/usePassword.ts b/src/store/usePassword.ts index 0731887..93246d1 100644 --- a/src/store/usePassword.ts +++ b/src/store/usePassword.ts @@ -137,7 +137,7 @@ const usePassword = (): UsePasswordReturnType => { } catch (err) { setError('password', { message: 'Incorrect password' }); } - }, 1); + }); }); const onClose = () => { diff --git a/src/utils/promises.ts b/src/utils/promises.ts index a040682..aac2edd 100644 --- a/src/utils/promises.ts +++ b/src/utils/promises.ts @@ -3,9 +3,14 @@ export const delay = (ms: number) => setTimeout(resolve, ms); }); -export const postpone = (fn: () => T, ms: number): Promise => - new Promise((resolve) => { - setTimeout(async () => { - resolve(await fn()); +export const postpone = (fn: () => Promise | T, ms = 1): Promise => + new Promise((resolve, reject) => { + setTimeout(() => { + const r = fn(); + if (r instanceof Promise) { + r.then(resolve).catch(reject); + return; + } + resolve(r); }, ms); }); From 3a7bdea490262f5e5c8dfa6610ecb145a3dce224 Mon Sep 17 00:00:00 2001 From: brusher_ru Date: Fri, 4 Oct 2024 17:07:11 -0300 Subject: [PATCH 2/3] fix: handle changing component states properly while running costly op --- src/screens/UnlockScreen.tsx | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/screens/UnlockScreen.tsx b/src/screens/UnlockScreen.tsx index e2fccaf..cec0c4b 100644 --- a/src/screens/UnlockScreen.tsx +++ b/src/screens/UnlockScreen.tsx @@ -16,6 +16,7 @@ import PasswordInput from '../components/PasswordInput'; import Logo from '../components/welcome/Logo'; import WipeOutAlert from '../components/WipeOutAlert'; import useWallet from '../store/useWallet'; +import { postpone } from '../utils/promises'; type FormValues = { password: string; @@ -37,15 +38,18 @@ function UnlockScreen(): JSX.Element { const submit = handleSubmit(async (data) => { setIsLoading(true); - const success = await unlockWallet(data.password); - if (!success) { - setError('password', { type: 'value', message: 'Invalid password' }); - return; - } - setValue('password', ''); - reset(); - setIsLoading(false); - navigate('/wallet'); + await postpone(async () => { + const success = await unlockWallet(data.password); + if (!success) { + setError('password', { type: 'value', message: 'Invalid password' }); + setIsLoading(false); + return; + } + setValue('password', ''); + reset(); + setIsLoading(false); + navigate('/wallet'); + }); }); const wipeAlert = useDisclosure(); From 0a5af5c682124c64fa15eb05f5e35e8fd6f80e94 Mon Sep 17 00:00:00 2001 From: brusher_ru Date: Fri, 4 Oct 2024 17:08:14 -0300 Subject: [PATCH 3/3] chore: fix linter warning --- src/components/VerifyMessageModal.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/VerifyMessageModal.tsx b/src/components/VerifyMessageModal.tsx index 79e34f1..0e2781f 100644 --- a/src/components/VerifyMessageModal.tsx +++ b/src/components/VerifyMessageModal.tsx @@ -19,7 +19,6 @@ import { import { useVerifyMessage } from '../hooks/useSigning'; import { isSignedMessage } from '../types/message'; -import { SIGNED_MESSAGE_PREFIX } from '../utils/constants'; type VerifyMessageModalProps = { isOpen: boolean;