readability improvements
This commit is contained in:
		
							parent
							
								
									f9169c645a
								
							
						
					
					
						commit
						67f6c170aa
					
				@ -144,7 +144,7 @@ export function useWalletLogManager (setLogs) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    if (!wallet || wallet.sendPayment) {
 | 
					    if (!wallet || wallet.sendPayment) {
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        const tag = wallet ? walletTag(wallet) : null
 | 
					        const tag = wallet ? walletTag(wallet.def) : null
 | 
				
			||||||
        if (notSupported) {
 | 
					        if (notSupported) {
 | 
				
			||||||
          console.log('cannot clear wallet logs: indexeddb not supported')
 | 
					          console.log('cannot clear wallet logs: indexeddb not supported')
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
 | 
				
			|||||||
@ -58,7 +58,14 @@ export class WalletAggregateError extends WalletError {
 | 
				
			|||||||
  constructor (errors, invoice) {
 | 
					  constructor (errors, invoice) {
 | 
				
			||||||
    super('WalletAggregateError')
 | 
					    super('WalletAggregateError')
 | 
				
			||||||
    this.name = 'WalletAggregateError'
 | 
					    this.name = 'WalletAggregateError'
 | 
				
			||||||
    this.errors = errors
 | 
					    this.errors = errors.reduce((acc, e) => {
 | 
				
			||||||
 | 
					      if (Array.isArray(e?.errors)) {
 | 
				
			||||||
 | 
					        acc.push(...e.errors)
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        acc.push(e)
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return acc
 | 
				
			||||||
 | 
					    }, [])
 | 
				
			||||||
    this.invoice = invoice
 | 
					    this.invoice = invoice
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -67,11 +74,14 @@ export class WalletPaymentAggregateError extends WalletPaymentError {
 | 
				
			|||||||
  constructor (errors, invoice) {
 | 
					  constructor (errors, invoice) {
 | 
				
			||||||
    super('WalletPaymentAggregateError')
 | 
					    super('WalletPaymentAggregateError')
 | 
				
			||||||
    this.name = 'WalletPaymentAggregateError'
 | 
					    this.name = 'WalletPaymentAggregateError'
 | 
				
			||||||
    this.errors = errors
 | 
					    this.errors = errors.reduce((acc, e) => {
 | 
				
			||||||
 | 
					      if (Array.isArray(e?.errors)) {
 | 
				
			||||||
 | 
					        acc.push(...e.errors)
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        acc.push(e)
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      return acc
 | 
				
			||||||
 | 
					    }, []).filter(e => e instanceof WalletPaymentError)
 | 
				
			||||||
    this.invoice = invoice
 | 
					    this.invoice = invoice
 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (!errors.every(e => e instanceof WalletPaymentError)) {
 | 
					 | 
				
			||||||
      throw new Error('only WalletPaymentError instances are allowed')
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -16,22 +16,21 @@ export function useWalletPayment () {
 | 
				
			|||||||
  const invoiceHelper = useInvoice()
 | 
					  const invoiceHelper = useInvoice()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return useCallback(async (invoice, { waitFor }) => {
 | 
					  return useCallback(async (invoice, { waitFor }) => {
 | 
				
			||||||
    let walletError = new WalletAggregateError([])
 | 
					    let aggregateError = new WalletAggregateError([])
 | 
				
			||||||
    let walletInvoice = invoice
 | 
					    let latestInvoice = invoice
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // throw a special error that caller can handle separately if no payment was attempted
 | 
					    // throw a special error that caller can handle separately if no payment was attempted
 | 
				
			||||||
    const noWalletAvailable = wallets.length === 0
 | 
					    if (wallets.length === 0) {
 | 
				
			||||||
    if (noWalletAvailable) {
 | 
					 | 
				
			||||||
      throw new WalletsNotAvailableError()
 | 
					      throw new WalletsNotAvailableError()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (const [i, wallet] of wallets.entries()) {
 | 
					    for (const [i, wallet] of wallets.entries()) {
 | 
				
			||||||
      const controller = invoiceController(walletInvoice, invoiceHelper.isInvoice)
 | 
					      const controller = invoiceController(latestInvoice, invoiceHelper.isInvoice)
 | 
				
			||||||
      try {
 | 
					      try {
 | 
				
			||||||
        return await new Promise((resolve, reject) => {
 | 
					        return await new Promise((resolve, reject) => {
 | 
				
			||||||
          // can't await wallet payments since we might pay hold invoices and thus payments might not settle immediately.
 | 
					          // can't await wallet payments since we might pay hold invoices and thus payments might not settle immediately.
 | 
				
			||||||
          // that's why we separately check if we received the payment with the invoice controller.
 | 
					          // that's why we separately check if we received the payment with the invoice controller.
 | 
				
			||||||
          sendPayment(wallet, walletInvoice).catch(reject)
 | 
					          sendPayment(wallet, latestInvoice).catch(reject)
 | 
				
			||||||
          controller.wait(waitFor)
 | 
					          controller.wait(waitFor)
 | 
				
			||||||
            .then(resolve)
 | 
					            .then(resolve)
 | 
				
			||||||
            .catch(reject)
 | 
					            .catch(reject)
 | 
				
			||||||
@ -39,14 +38,13 @@ export function useWalletPayment () {
 | 
				
			|||||||
      } catch (err) {
 | 
					      } catch (err) {
 | 
				
			||||||
        // cancel invoice to make sure it cannot be paid later and create new invoice to retry.
 | 
					        // cancel invoice to make sure it cannot be paid later and create new invoice to retry.
 | 
				
			||||||
        // we only need to do this if payment was attempted which is not the case if the wallet is not enabled.
 | 
					        // we only need to do this if payment was attempted which is not the case if the wallet is not enabled.
 | 
				
			||||||
        const paymentAttempt = err instanceof WalletPaymentError
 | 
					        if (err instanceof WalletPaymentError) {
 | 
				
			||||||
        if (paymentAttempt) {
 | 
					          await invoiceHelper.cancel(latestInvoice)
 | 
				
			||||||
          await invoiceHelper.cancel(walletInvoice)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
          // is there another wallet to try?
 | 
					          // is there another wallet to try?
 | 
				
			||||||
          const lastAttempt = i === wallets.length - 1
 | 
					          const lastAttempt = i === wallets.length - 1
 | 
				
			||||||
          if (!lastAttempt) {
 | 
					          if (!lastAttempt) {
 | 
				
			||||||
            walletInvoice = await invoiceHelper.retry(walletInvoice)
 | 
					            latestInvoice = await invoiceHelper.retry(latestInvoice)
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -57,9 +55,8 @@ export function useWalletPayment () {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        // try next wallet if the payment failed because of the wallet
 | 
					        // try next wallet if the payment failed because of the wallet
 | 
				
			||||||
        // and not because it expired or was canceled
 | 
					        // and not because it expired or was canceled
 | 
				
			||||||
        const isWalletError = err instanceof WalletError
 | 
					        if (err instanceof WalletError) {
 | 
				
			||||||
        if (isWalletError) {
 | 
					          aggregateError = new WalletAggregateError([aggregateError, err], latestInvoice)
 | 
				
			||||||
          walletError = new WalletAggregateError([...walletError.errors, err], walletInvoice)
 | 
					 | 
				
			||||||
          continue
 | 
					          continue
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -71,9 +68,7 @@ export function useWalletPayment () {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // if we reach this line, no wallet payment succeeded
 | 
					    // if we reach this line, no wallet payment succeeded
 | 
				
			||||||
    // only return payment errors
 | 
					    throw new WalletPaymentAggregateError([aggregateError], latestInvoice)
 | 
				
			||||||
    const paymentErrors = walletError.errors.filter(e => e instanceof WalletPaymentError)
 | 
					 | 
				
			||||||
    throw new WalletPaymentAggregateError(paymentErrors, walletInvoice)
 | 
					 | 
				
			||||||
  }, [wallets, invoiceHelper, sendPayment])
 | 
					  }, [wallets, invoiceHelper, sendPayment])
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user