diff --git a/packages/shared/lib/core/account/actions/findBalances.ts b/packages/shared/lib/core/account/actions/findBalances.ts index 52524764b22..481ad568d01 100644 --- a/packages/shared/lib/core/account/actions/findBalances.ts +++ b/packages/shared/lib/core/account/actions/findBalances.ts @@ -1,6 +1,8 @@ import { DEFAULT_SYNC_OPTIONS, SearchAlgorithmType } from '@core/account' +import { updateLedgerNanoStatus } from '@core/ledger' import { BALANCE_FINDER_ACCOUNT_RECOVERY_CONFIGURATION, + ProfileType, UnableToFindProfileTypeError, activeProfile, } from '@core/profile' @@ -16,11 +18,13 @@ let searchAccountStartIndex: number let _accountGapLimit: number let _addressGapLimit: number +let profileType: ProfileType + export function initialiseAccountRecoveryConfiguration( algortihmType: SearchAlgorithmType, config?: RecoverAccountsPayload ): void { - const profileType = get(activeProfile)?.type + profileType = get(activeProfile)?.type if (!profileType) { throw new UnableToFindProfileTypeError() } @@ -40,17 +44,31 @@ export async function findBalances( init?: boolean, config?: RecoverAccountsPayload ): Promise { - if (init) { - initialiseAccountRecoveryConfiguration(algortihmType, config) - } - if (algortihmType === SearchAlgorithmType.BFS || algortihmType === SearchAlgorithmType.IDS) { - await breadthSearchAndRecoverAccounts(config) - searchAccountStartIndex += _accountGapLimit - } - if (algortihmType === SearchAlgorithmType.DFS || algortihmType === SearchAlgorithmType.IDS) { - await depthSearchAndRecoverAccounts(config) - // TODO: Improve the address start index, checking which is the last address index found in a account and continuing searching from that index - searchAddressStartIndex += _addressGapLimit + try { + if (init) { + initialiseAccountRecoveryConfiguration(algortihmType, config) + } + if (profileType === ProfileType.Ledger) { + // Note: This is a way to know the ledger is doing heavy work + updateLedgerNanoStatus({ busy: true }) + } + if (algortihmType === SearchAlgorithmType.BFS || algortihmType === SearchAlgorithmType.IDS) { + await breadthSearchAndRecoverAccounts(config) + searchAccountStartIndex += _accountGapLimit + } + if (algortihmType === SearchAlgorithmType.DFS || algortihmType === SearchAlgorithmType.IDS) { + await depthSearchAndRecoverAccounts(config) + // TODO: Improve the address start index, checking which is the last address index found in a account and continuing searching from that index + searchAddressStartIndex += _addressGapLimit + } + } catch (error) { + const message = error?.message ?? error?.error ?? '' + throw new Error(message) + } finally { + if (profileType === ProfileType.Ledger) { + // Note: This is a way to know the ledger is doing heavy work + updateLedgerNanoStatus({ busy: false }) + } } } diff --git a/packages/shared/lib/core/ledger/actions/pollLedgerNanoStatus.ts b/packages/shared/lib/core/ledger/actions/pollLedgerNanoStatus.ts index bf1aba6fecc..da0d8e36d9c 100644 --- a/packages/shared/lib/core/ledger/actions/pollLedgerNanoStatus.ts +++ b/packages/shared/lib/core/ledger/actions/pollLedgerNanoStatus.ts @@ -1,28 +1,36 @@ import { get } from 'svelte/store' - +import { DEFAULT_LEDGER_NANO_STATUS_POLL_INTERVAL } from '../constants' import { deconstructLedgerNanoStatusPollingConfiguration } from '../helpers' import { ILedgerNanoStatusPollingConfiguration } from '../interfaces' -import { isPollingLedgerDeviceStatus } from '../stores' - +import { isPollingLedgerDeviceStatus, ledgerNanoStatus } from '../stores' import { getAndUpdateLedgerNanoStatus } from './getAndUpdateLedgerNanoStatus' -let intervalTimer: ReturnType +let intervalTimer: ReturnType | null export function pollLedgerNanoStatus(config?: ILedgerNanoStatusPollingConfiguration): void { const { pollInterval, profileManager } = deconstructLedgerNanoStatusPollingConfiguration(config) + const defaultPollInterval = pollInterval || DEFAULT_LEDGER_NANO_STATUS_POLL_INTERVAL + const slowedPollInterval = 10 * defaultPollInterval + if (!get(isPollingLedgerDeviceStatus)) { - void getAndUpdateLedgerNanoStatus(profileManager) - intervalTimer = setInterval(() => { - void getAndUpdateLedgerNanoStatus(profileManager) - }, pollInterval) isPollingLedgerDeviceStatus.set(true) + const pollingFunction = async (): Promise => { + await getAndUpdateLedgerNanoStatus(profileManager) + const isLedgerBusy = get(ledgerNanoStatus)?.busy + const currentPollInterval = isLedgerBusy ? slowedPollInterval : defaultPollInterval + intervalTimer = setTimeout(() => void pollingFunction(), currentPollInterval) + } + + void pollingFunction() } } export function stopPollingLedgerNanoStatus(): void { if (get(isPollingLedgerDeviceStatus)) { - clearInterval(intervalTimer) + if (intervalTimer) { + clearInterval(intervalTimer) + } intervalTimer = null isPollingLedgerDeviceStatus.set(false) } diff --git a/packages/shared/lib/core/ledger/stores/ledger-nano-status.store.ts b/packages/shared/lib/core/ledger/stores/ledger-nano-status.store.ts index 69986350d5e..3e28cda3f05 100644 --- a/packages/shared/lib/core/ledger/stores/ledger-nano-status.store.ts +++ b/packages/shared/lib/core/ledger/stores/ledger-nano-status.store.ts @@ -1,18 +1,23 @@ import { LedgerNanoStatus } from '@iota/sdk/out/types' import { writable } from 'svelte/store' -const DEFAULT_LEDGER_STATUS: LedgerNanoStatus = { +interface ExtendedLedgerNanoStatus extends LedgerNanoStatus { + busy?: boolean +} + +const DEFAULT_LEDGER_STATUS: ExtendedLedgerNanoStatus = { app: undefined, blindSigningEnabled: false, bufferSize: undefined, connected: false, device: undefined, locked: false, + busy: false, } -export const ledgerNanoStatus = writable(DEFAULT_LEDGER_STATUS) +export const ledgerNanoStatus = writable(DEFAULT_LEDGER_STATUS) -export function updateLedgerNanoStatus(payload: Partial): void { +export function updateLedgerNanoStatus(payload: Partial): void { return ledgerNanoStatus.update((state) => { if (ledgerNanoStatus) { return { ...state, ...payload }