Skip to content

Commit

Permalink
fix(unisat): memory leak and disconnect wallet on error (#170)
Browse files Browse the repository at this point in the history
* fix(unisat): memory leak

* fix(unisat): disconnect wallet on error
  • Loading branch information
Nanosync authored Nov 24, 2023
1 parent 46148db commit 33eae8b
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions packages/ord-connect/src/components/SelectWalletModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export function SelectWalletModal({
format,
address,
publicKey,
disconnectWallet,
} = useOrdContext();
const [errorMessage, setErrorMessage] = useState<string>("");
const isSupportedDevice = !disableMobile || !isMobileDevice();
Expand All @@ -62,15 +63,17 @@ export function SelectWalletModal({
}
setErrorMessage(err.message ?? err.toString());
console.error(`Error while connecting to ${walletProvider} wallet`, err);

Check warning on line 65 in packages/ord-connect/src/components/SelectWalletModal/index.tsx

View workflow job for this annotation

GitHub Actions / Lint - Typescript and ESLint

Unexpected console statement

Check warning on line 65 in packages/ord-connect/src/components/SelectWalletModal/index.tsx

View workflow job for this annotation

GitHub Actions / Build (Apps & Packages)

Unexpected console statement

Check warning on line 65 in packages/ord-connect/src/components/SelectWalletModal/index.tsx

View workflow job for this annotation

GitHub Actions / NPM

Unexpected console statement
disconnectWallet();
},
[],

Check warning on line 68 in packages/ord-connect/src/components/SelectWalletModal/index.tsx

View workflow job for this annotation

GitHub Actions / Lint - Typescript and ESLint

React Hook useCallback has a missing dependency: 'disconnectWallet'. Either include it or remove the dependency array

Check warning on line 68 in packages/ord-connect/src/components/SelectWalletModal/index.tsx

View workflow job for this annotation

GitHub Actions / Build (Apps & Packages)

React Hook useCallback has a missing dependency: 'disconnectWallet'. Either include it or remove the dependency array

Check warning on line 68 in packages/ord-connect/src/components/SelectWalletModal/index.tsx

View workflow job for this annotation

GitHub Actions / NPM

React Hook useCallback has a missing dependency: 'disconnectWallet'. Either include it or remove the dependency array
);

const onConnectUnisatWallet = async (readOnly?: boolean) => {
const listener = () => {
onConnectUnisatWallet();
};
try {
window.unisat.removeListener("accountsChanged", () =>
onConnectUnisatWallet(),
);
window.unisat.removeListener("accountsChanged", listener);
} catch (_) {
// This will fail on first run, handle it silently
}
Expand All @@ -80,6 +83,7 @@ export function SelectWalletModal({
const unisat = await getUnisatAddresses(network, readOnly);

if (!unisat || unisat.length < 1) {
disconnectWallet();
throw new Error("Unisat via Ordit returned no addresses.");
}

Expand All @@ -99,9 +103,7 @@ export function SelectWalletModal({
payments: unisatWallet.format,
});

window.unisat.addListener("accountsChanged", () =>
onConnectUnisatWallet(),
);
window.unisat.addListener("accountsChanged", listener);
closeModal();
return true;
} catch (err) {
Expand All @@ -116,6 +118,7 @@ export function SelectWalletModal({
// P2SH-P2WPKH = BTC
// Taproot = Ordinals / Inscriptions
if (!xverse || xverse.length < 1) {
disconnectWallet();
throw new Error("Xverse via Ordit returned no addresses.");
}

Expand Down

0 comments on commit 33eae8b

Please sign in to comment.