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 invoice.item.root?.bounty === invoice.satsRequested && invoice.item.root?.mine
? payBountyCacheMods ? 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({ return useAct({
query: RETRY_PAID_ACTION, query: RETRY_PAID_ACTION,
onPayError: (e, cache, { data }) => { onPayError: (e, cache, { data }) => {
@ -380,27 +403,8 @@ function useActRetry ({ invoice }) {
paidActionCacheMods?.onPaid?.(cache, { data }) paidActionCacheMods?.onPaid?.(cache, { data })
bountyCacheMods?.onPaid?.(cache, { data }) bountyCacheMods?.onPaid?.(cache, { data })
}, },
update: (cache, { data }) => { update,
const response = Object.values(data)[0] updateOnFallback: update
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 })
}
}) })
} }

View File

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

View File

@ -15,7 +15,7 @@ export function useWalletPayment () {
const sendPayment = useSendPayment() const sendPayment = useSendPayment()
const invoiceHelper = useInvoice() const invoiceHelper = useInvoice()
return useCallback(async (invoice, { waitFor, update }) => { return useCallback(async (invoice, { waitFor, updateOnFallback }) => {
let aggregateError = new WalletAggregateError([]) let aggregateError = new WalletAggregateError([])
let latestInvoice = invoice let latestInvoice = invoice
@ -44,7 +44,7 @@ export function useWalletPayment () {
// 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) {
latestInvoice = await invoiceHelper.retry(latestInvoice, { update }) latestInvoice = await invoiceHelper.retry(latestInvoice, { update: updateOnFallback })
} }
} }