make fallback retry cache updates a special case

This commit is contained in:
k00b 2024-11-28 11:22:46 -06:00
parent cb028d217c
commit 105f7b07e5
3 changed files with 33 additions and 29 deletions

View File

@ -370,6 +370,29 @@ function useActRetry ({ invoice }) {
invoice.item.root?.bounty === invoice.satsRequested && invoice.item.root?.mine
? payBountyCacheMods
: {}
const update = (cache, { data }) => {
const response = Object.values(data)[0]
if (!response?.invoice) return
cache.modify({
id: `ItemAct:${invoice.itemAct?.id}`,
fields: {
// this is a bit of a hack just to update the reference to the new invoice
invoice: () => cache.writeFragment({
id: `Invoice:${response.invoice.id}`,
fragment: gql`
fragment _ on Invoice {
bolt11
}
`,
data: { bolt11: response.invoice.bolt11 }
})
}
})
paidActionCacheMods?.update?.(cache, { data })
bountyCacheMods?.update?.(cache, { data })
}
return useAct({
query: RETRY_PAID_ACTION,
onPayError: (e, cache, { data }) => {
@ -380,27 +403,8 @@ function useActRetry ({ invoice }) {
paidActionCacheMods?.onPaid?.(cache, { data })
bountyCacheMods?.onPaid?.(cache, { data })
},
update: (cache, { data }) => {
const response = Object.values(data)[0]
if (!response?.invoice) return
cache.modify({
id: `ItemAct:${invoice.itemAct?.id}`,
fields: {
// this is a bit of a hack just to update the reference to the new invoice
invoice: () => cache.writeFragment({
id: `Invoice:${response.invoice.id}`,
fragment: gql`
fragment _ on Invoice {
bolt11
}
`,
data: { bolt11: response.invoice.bolt11 }
})
}
})
paidActionCacheMods?.update?.(cache, { data })
bountyCacheMods?.update?.(cache, { data })
}
update,
updateOnFallback: update
})
}

View File

@ -31,13 +31,13 @@ export function usePaidMutation (mutation,
// innerResult is used to store/control the result of the mutation when innerMutate runs
const [innerResult, setInnerResult] = useState(result)
const waitForPayment = useCallback(async (invoice, { alwaysShowQROnFailure = false, persistOnNavigate = false, waitFor, update }) => {
const waitForPayment = useCallback(async (invoice, { alwaysShowQROnFailure = false, persistOnNavigate = false, waitFor, updateOnFallback }) => {
let walletError
let walletInvoice = invoice
const start = Date.now()
try {
return await waitForWalletPayment(walletInvoice, { waitFor, update })
return await waitForWalletPayment(walletInvoice, { waitFor, updateOnFallback })
} catch (err) {
walletError = null
if (err instanceof WalletError) {
@ -63,7 +63,7 @@ export function usePaidMutation (mutation,
const paymentAttempted = walletError instanceof WalletPaymentError
if (paymentAttempted) {
walletInvoice = await invoiceHelper.retry(walletInvoice)
walletInvoice = await invoiceHelper.retry(walletInvoice, { updateOnFallback })
}
return await waitForQrPayment(walletInvoice, walletError, { persistOnNavigate, waitFor })
}, [waitForWalletPayment, waitForQrPayment, invoiceHelper])
@ -77,7 +77,7 @@ export function usePaidMutation (mutation,
// use the most inner callbacks/options if they exist
const {
onPaid, onPayError, forceWaitForPayment, persistOnNavigate,
update, waitFor = inv => inv?.actionState === 'PAID'
update, waitFor = inv => inv?.actionState === 'PAID', updateOnFallback
} = { ...options, ...innerOptions }
const ourOnCompleted = innerOnCompleted || onCompleted
@ -105,7 +105,7 @@ export function usePaidMutation (mutation,
// onCompleted is called before the invoice is paid for optimistic updates
ourOnCompleted?.(data)
// don't wait to pay the invoice
waitForPayment(invoice, { persistOnNavigate, waitFor, update }).then((invoice) => {
waitForPayment(invoice, { persistOnNavigate, waitFor, updateOnFallback }).then((invoice) => {
// invoice might have been retried during payment
data = {
[dataKey]: {
@ -135,7 +135,7 @@ export function usePaidMutation (mutation,
try {
// wait for the invoice to be paid
// returns the invoice that was paid since it might have been updated via retries
invoice = await waitForPayment(invoice, { alwaysShowQROnFailure: true, persistOnNavigate, waitFor, update })
invoice = await waitForPayment(invoice, { alwaysShowQROnFailure: true, persistOnNavigate, waitFor, updateOnFallback })
if (!response.result) {
// if the mutation didn't return any data, ie pessimistic, we need to fetch it
const { data: { paidAction } } = await getPaidAction({ variables: { invoiceId: parseInt(invoice.id) } })

View File

@ -15,7 +15,7 @@ export function useWalletPayment () {
const sendPayment = useSendPayment()
const invoiceHelper = useInvoice()
return useCallback(async (invoice, { waitFor, update }) => {
return useCallback(async (invoice, { waitFor, updateOnFallback }) => {
let aggregateError = new WalletAggregateError([])
let latestInvoice = invoice
@ -44,7 +44,7 @@ export function useWalletPayment () {
// is there another wallet to try?
const lastAttempt = i === wallets.length - 1
if (!lastAttempt) {
latestInvoice = await invoiceHelper.retry(latestInvoice, { update })
latestInvoice = await invoiceHelper.retry(latestInvoice, { update: updateOnFallback })
}
}