Fix wallet status when wallet is unreachable (#79) #80

Closed
triesap wants to merge 33 commits from fix/79-wallet-status-unavailable into dev
Showing only changes of commit 39c6a33273 - Show all commits
+16 -47
View File
@@ -1,6 +1,6 @@
<script lang="ts">
import cx from "classnames"
import {LOCALE} from "@welshman/lib"
import {LOCALE, always, call, sleep} from "@welshman/lib"
import {displayRelayUrl, isNWCWallet, fromMsats} from "@welshman/util"
import {session, pubkey, profilesByPubkey} from "@welshman/app"
import DownloadMinimalistic from "@assets/icons/download-minimalistic.svg?dataurl"
@@ -41,49 +41,6 @@
}
}
const withTimeout = async (promise: Promise<unknown>, timeoutMs: number) => {
let timeoutId: ReturnType<typeof setTimeout> | undefined
try {
return await Promise.race([
promise,
new Promise((_, reject) => {
timeoutId = setTimeout(() => reject(new Error("Wallet check timed out")), timeoutMs)
}),
])
} finally {
if (timeoutId) {
clearTimeout(timeoutId)
}
}
}
const checkNwcStatus = async () => {
try {
await withTimeout(getNwcClient().getInfo(), 5000)
return "connected" as const
} catch (error) {
console.warn("NWC wallet unavailable", error)
return "unavailable" as const
}
}
const checkWebLnStatus = async () => {
const webLn = getWebLn()
if (!webLn) {
return "unavailable" as const
}
try {
await withTimeout(webLn.getInfo(), 5000)
return "connected" as const
} catch (error) {
console.warn("WebLN wallet unavailable", error)
return "unavailable" as const
}
}
let walletStatus = $state<"checking" | "connected" | "unavailable">("checking")
let walletStatusRequestId = 0
@@ -106,13 +63,25 @@
hodlbod marked this conversation as resolved
Review

Just do const ok = await Promise.race(getNwcClient().getInfo().then(always("connected")), sleep(5000).then(always("unavailable"))). Replaces 40 lines of code with 4, and it's much easier to follow.

Just do `const ok = await Promise.race(getNwcClient().getInfo().then(always("connected")), sleep(5000).then(always("unavailable")))`. Replaces 40 lines of code with 4, and it's much easier to follow.
walletStatus = "checking"
void (async () => {
const nextStatus = wallet.type === "nwc" ? await checkNwcStatus() : await checkWebLnStatus()
call(async () => {
const webLn = getWebLn()
const nextStatus =
wallet.type === "nwc"
? await Promise.race([
getNwcClient().getInfo().then(always("connected")).catch(always("unavailable")),
sleep(5000).then(always("unavailable")),
])
: webLn
? await Promise.race([
webLn.getInfo().then(always("connected")).catch(always("unavailable")),
sleep(5000).then(always("unavailable")),
])
: "unavailable"
if (requestId === walletStatusRequestId) {
walletStatus = nextStatus
}
})()
})
}
$effect(() => {
1