Simplify wallet status probe
This commit is contained in:
@@ -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 @@
|
||||
|
||||
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(() => {
|
||||
|
||||
Reference in New Issue
Block a user