diff --git a/src/core/global-store/modules/global-module.ts b/src/core/global-store/modules/global-module.ts index 4a100eb56..59f3e8563 100644 --- a/src/core/global-store/modules/global-module.ts +++ b/src/core/global-store/modules/global-module.ts @@ -4,7 +4,7 @@ import * as ls from "@/utils/local-storage"; import { success } from "@/features/shared"; import i18next from "i18next"; import { getCurrencyRate } from "@/api/misc"; -import { currencySymbol } from "@/utils"; +import { currencySymbol, runWithRetries } from "@/utils"; export function createGlobalState() { return { @@ -96,13 +96,15 @@ export function createGlobalActions(set: (state: Partial) => void, getSta // So We have to wait until full window load and then drop our task to macro-queue // It will help us to validate that all sync and async operations have finished // Including browser extensions - setTimeout(() => { + runWithRetries(() => { if (typeof window !== "undefined" && "hive_keychain" in window) { (window as unknown as any).hive_keychain.requestHandshake(() => set({ hasKeyChain: true }) ); + return true; } - }, 50); + return false; + }); } }; } diff --git a/src/utils/index.ts b/src/utils/index.ts index 3a2c12329..5c90fe5f3 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -36,3 +36,4 @@ export * from "./safe-spread"; export * from "./use-active-user-wallet"; export * from "./delay"; export * from "./rq-infinite-data-utils"; +export * from "./run-with-retries"; diff --git a/src/utils/run-with-retries.ts b/src/utils/run-with-retries.ts new file mode 100644 index 000000000..ff44bdc1b --- /dev/null +++ b/src/utils/run-with-retries.ts @@ -0,0 +1,20 @@ +import { delay } from "@/utils/delay"; + +export async function runWithRetries( + task: () => boolean | Promise, + delayMs = 100, + retries = 3 +) { + await delay(delayMs); + + const result = await task(); + if (result) { + return true; + } + + if (retries > 0) { + return await runWithRetries(task, delayMs, retries - 1); + } + + return true; +}