Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

优化代码,修复刷新页面Install Unisat按钮一闪而过 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 23 additions & 25 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,35 @@ function App() {
};

useEffect(() => {

async function checkUnisat() {
let unisat = (window as any).unisat;

for (let i = 1; i < 10 && !unisat; i += 1) {
await new Promise((resolve) => setTimeout(resolve, 100*i));
unisat = (window as any).unisat;
}

if(unisat){
setUnisatInstalled(true);
}else if (!unisat)
return;

unisat.getAccounts().then((accounts: string[]) => {
const checkUnisat = () => {
const { unisat } = window as any;
if (unisat) {
setUnisatInstalled(true);
unisat.getAccounts().then((accounts: string[]) => {
handleAccountsChanged(accounts);
});

unisat.on("accountsChanged", handleAccountsChanged);
unisat.on("networkChanged", handleNetworkChanged);
});
unisat.on("accountsChanged", handleAccountsChanged);
unisat.on("networkChanged", handleNetworkChanged);
}
};

return () => {
unisat.removeListener("accountsChanged", handleAccountsChanged);
unisat.removeListener("networkChanged", handleNetworkChanged);
};
if (document.readyState === 'complete') {
checkUnisat();
} else {
window.addEventListener('load', checkUnisat);
}

checkUnisat().then();
return () => {
const { unisat } = window as any;
if (unisat) {
unisat.removeListener("accountsChanged", handleAccountsChanged);
unisat.removeListener("networkChanged", handleNetworkChanged);
}
window.removeEventListener('load', checkUnisat);
};
}, []);

if (!unisatInstalled) {
if (document.readyState === 'complete'&&!unisatInstalled) {
return (
<div className="App">
<header className="App-header">
Expand Down
Loading