-
-
Notifications
You must be signed in to change notification settings - Fork 132
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
Comments are created and injected into cache as a result of a paid mutation.
If I understood everything correctly, if the payment method is optimistic, the comment is immediately injected into the cache. However, if the payment method is pessimistic, such as a QR payment, the comment is only injected into the cache after the payment has been confirmed.
Only paid new comments are fetched, but a pessimistic mutation delays cache updates, causing double comments
For anons, edits are based on the payment hash stored in local storage after the payment has been confirmed
stacker.news/components/use-paid-mutation.js
Lines 130 to 145 in c5b50d7
// the action is pessimistic | |
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, 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) } }) | |
// create new data object | |
// ( hmac is only returned on invoice creation so we need to add it back to the data ) | |
data = mergeData({ ...paidAction, invoice: { ...paidAction.invoice, hmac: invoice.hmac } }) | |
// we need to run update functions on mutations now that we have the data | |
update?.(client.cache, { data }) | |
} | |
ourOnCompleted?.(data) | |
onPaid?.(client.cache, { data }) |
stacker.news/components/use-item-submit.js
Lines 76 to 79 in c5b50d7
onCompleted: (data) => { | |
onSuccessfulSubmit?.(data, { resetForm }) | |
paidMutationOptions?.onCompleted?.(data) | |
saveItemInvoiceHmac(data) |
This implies that if we were to skip a cache update when creating a comment if it’s already in the cache, the canEdit
conditions won’t be fulfilled because live comments will inject it before the pessimistic mutation is complete, necessitating a refresh.
Screenshots
anonlivecomments.mp4
Steps To Reproduce
- be anon
- create a comment right when live comments fetching occurs
- comment is duplicated and the fetched one can't be edited.
This requires timing precision or luck, can be reproduced more reliably with a 1 second polling rate in dev.
Expected behavior
Comment is created and live comments won't inject it.
Logs
No response
Device information
No response
Additional context
Reported by @ekzyis
This couldn't occur in the first version of live comments because of multiple steps of deduplication that delayed injection. The permanent fix shouldn't be more steps of deduplication in live comments, maybe we should deduplicate on comment creation if it's a pessimistic mutation