Skip to content

Commit

Permalink
Added retryable for keychain handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
dkildar committed Nov 14, 2024
1 parent 34f25ce commit 1b58953
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/core/global-store/modules/global-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -96,13 +96,15 @@ export function createGlobalActions(set: (state: Partial<State>) => 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;
});
}
};
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
20 changes: 20 additions & 0 deletions src/utils/run-with-retries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { delay } from "@/utils/delay";

export async function runWithRetries(
task: () => boolean | Promise<boolean>,
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;
}

0 comments on commit 1b58953

Please sign in to comment.