Merge pull request #1557 from stackernews/fix-invoice-waiting
fix invoice waiting
This commit is contained in:
commit
d6916fa3f4
@ -1,4 +1,4 @@
|
|||||||
import { useCallback, useMemo } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { useMe } from './me'
|
import { useMe } from './me'
|
||||||
import { gql, useApolloClient, useMutation } from '@apollo/client'
|
import { gql, useApolloClient, useMutation } from '@apollo/client'
|
||||||
import { useWallet } from '@/wallets/index'
|
import { useWallet } from '@/wallets/index'
|
||||||
@ -60,10 +60,23 @@ export const useInvoice = () => {
|
|||||||
return that(data.invoice)
|
return that(data.invoice)
|
||||||
}, [client])
|
}, [client])
|
||||||
|
|
||||||
const waitController = useMemo(() => {
|
const cancel = useCallback(async ({ hash, hmac }) => {
|
||||||
|
if (!hash || !hmac) {
|
||||||
|
throw new Error('missing hash or hmac')
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('canceling invoice:', hash)
|
||||||
|
const inv = await cancelInvoice({ variables: { hash, hmac } })
|
||||||
|
return inv
|
||||||
|
}, [cancelInvoice])
|
||||||
|
|
||||||
|
return { create, cancel, isInvoice }
|
||||||
|
}
|
||||||
|
|
||||||
|
const invoiceController = (id, isInvoice) => {
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
const signal = controller.signal
|
const signal = controller.signal
|
||||||
controller.wait = async ({ id }, waitFor = inv => inv?.actionState === 'PAID') => {
|
controller.wait = async (waitFor = inv => inv?.actionState === 'PAID') => {
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
const interval = setInterval(async () => {
|
const interval = setInterval(async () => {
|
||||||
try {
|
try {
|
||||||
@ -95,19 +108,6 @@ export const useInvoice = () => {
|
|||||||
controller.stop = () => controller.abort()
|
controller.stop = () => controller.abort()
|
||||||
|
|
||||||
return controller
|
return controller
|
||||||
}, [isInvoice])
|
|
||||||
|
|
||||||
const cancel = useCallback(async ({ hash, hmac }) => {
|
|
||||||
if (!hash || !hmac) {
|
|
||||||
throw new Error('missing hash or hmac')
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('canceling invoice:', hash)
|
|
||||||
const inv = await cancelInvoice({ variables: { hash, hmac } })
|
|
||||||
return inv
|
|
||||||
}, [cancelInvoice])
|
|
||||||
|
|
||||||
return { create, waitUntilPaid: waitController.wait, stopWaiting: waitController.stop, cancel }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useWalletPayment = () => {
|
export const useWalletPayment = () => {
|
||||||
@ -118,12 +118,13 @@ export const useWalletPayment = () => {
|
|||||||
if (!wallet) {
|
if (!wallet) {
|
||||||
throw new NoAttachedWalletError()
|
throw new NoAttachedWalletError()
|
||||||
}
|
}
|
||||||
|
const controller = invoiceController(id, invoice.isInvoice)
|
||||||
try {
|
try {
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
// can't use await here since we might pay JIT invoices and sendPaymentAsync is not supported yet.
|
// can't use await here since we might pay JIT invoices and sendPaymentAsync is not supported yet.
|
||||||
// see https://www.webln.guide/building-lightning-apps/webln-reference/webln.sendpaymentasync
|
// see https://www.webln.guide/building-lightning-apps/webln-reference/webln.sendpaymentasync
|
||||||
wallet.sendPayment(bolt11).catch(reject)
|
wallet.sendPayment(bolt11).catch(reject)
|
||||||
invoice.waitUntilPaid({ id }, waitFor)
|
controller.wait(waitFor)
|
||||||
.then(resolve)
|
.then(resolve)
|
||||||
.catch(reject)
|
.catch(reject)
|
||||||
})
|
})
|
||||||
@ -131,7 +132,7 @@ export const useWalletPayment = () => {
|
|||||||
console.error('payment failed:', err)
|
console.error('payment failed:', err)
|
||||||
throw err
|
throw err
|
||||||
} finally {
|
} finally {
|
||||||
invoice.stopWaiting()
|
controller.stop()
|
||||||
}
|
}
|
||||||
}, [wallet, invoice])
|
}, [wallet, invoice])
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user