Skip to content

Commit

Permalink
fix silent fails in getWallets
Browse files Browse the repository at this point in the history
  • Loading branch information
pivilartisant committed Jan 3, 2025
1 parent eb1af14 commit 7fb7703
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/walletsManager/walletList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@ export const supportedWallets: WalletInterfaces = [
export async function getWallets(delay = 200): Promise<Wallet[]> {
await wait(delay);

const walletPromises = supportedWallets.map(async (WalletClass) => {
try {
return await WalletClass.createIfInstalled();
} catch (error) {
console.error(`Error initializing wallet ${WalletClass.name}:`, error);
}
return null;
});
// https://github.com/massalabs/wallet-provider/issues/281
const wrapWithTimeout = async (
promise: Promise<Wallet | null>,
timeout: number,
): Promise<Wallet | null> => {
return Promise.race([
promise,
new Promise<null>((_, reject) =>
setTimeout(() => reject(new Error('Operation timed out')), timeout),
),
]).catch(() => {
return null;
});
};

const walletPromises = supportedWallets.map((WalletClass) =>
wrapWithTimeout(WalletClass.createIfInstalled(), 100),
);

const resolvedWallets = await Promise.all(walletPromises);
return resolvedWallets.filter((wallet) => !!wallet);
return resolvedWallets.filter((wallet) => wallet !== null);
}

export async function getWallet(
Expand Down

0 comments on commit 7fb7703

Please sign in to comment.